<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/stippling-and-tsp-art-in-r-emulating-stipplegen/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://dahtah.wordpress.com/2017/07/24/stippling-and-tsp-art-in-r-emulating-stipplegen/"> R – dahtah</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Stippling is the creation of a pattern simulating varying degrees of solidity or shading by using small dots (<a rel="nofollow" href="https://en.wikipedia.org/wiki/Stippling" target="_blank">Wikipedia</a>).<a rel="nofollow" href="http://wiki.evilmadscientist.com/StippleGen" target="_blank">StippleGen</a> is a piece of software that renders images using stipple patterns, which I discovered on <a rel="nofollow" href="https://xianblog.wordpress.com/2017/07/21/and-the-travelling-salesman-is/" target="_blank">Xi’an’s blog</a> a couple days ago.</p>
<div data-shortcode="caption" class="wp-caption"><img data-orig-file="https://dahtah.files.wordpress.com/2017/07/sketch.png?w=456" data-orig-size="480,480" data-medium-file="https://dahtah.files.wordpress.com/2017/07/sketch.png?w=456?w=300" data-attachment-id="439" data-image-meta="{" data-image-description="" src="https://dahtah.files.wordpress.com/2017/07/sketch.png?w=456" data-permalink="https://dahtah.wordpress.com/2017/07/24/stippling-and-tsp-art-in-r-emulating-stipplegen/sketch/" data-large-file="https://dahtah.files.wordpress.com/2017/07/sketch.png?w=456?w=480" alt="" data-image-title="sketch" sizes="(max-width: 480px) 100vw, 480px" data-comments-opened="1" class="wp-image-439" data-recalc-dims="1">
<p class="wp-caption-text">Stippled version of a sketch by Paul Cézanne, rendered in R</p>
</div>
<p>
<span></span></p>
<p>StippleGen uses an algorithm by Adrian Secord (<a rel="nofollow" href="http://mrl.nyu.edu/~ajsecord/npar2002/npar2002_ajsecord_preprint.pdf" target="_blank">described here</a>) that turns out to be related to a problem in spatial statistics, specifically how to mess with high-order statistics of point processes while controlling density. The algorithm is a variant of k-means and is extremely easy to implement in R.</p>
<pre>

library(imager)
library(dplyr)
library(purrr)

stipple &lt;- function(im,nPoints=1e3,gamma=2,nSteps=10)
{
    dens &lt;- (1-im)^gamma
    xy &lt;- sample(nPix(im),nPoints,replace=TRUE,prob=dens) %>% coord.index(im,.) %>% select(x,y)
    for (ind in 1:nSteps)
    {
        xy &lt;- cvt(xy,dens)
        plot(im); points(xy,col=&quot;red&quot;)
    }
    xy
}

plot.stipple &lt;- function(im,out,cex=.25)
{
    g &lt;- imgradient(im,&quot;xy&quot;) %>% map(~ interp(.,out))
    plot(out,ylim=c(height(im),1),cex=cex,pch=19,axes=FALSE,xlab=&quot;&quot;,ylab=&quot;&quot;)
}

##Compute Voronoi diagram of point set xy,
##and return center of mass of each cell (with density given by image im)
cvt &lt;- function(xy,im)
{
    voronoi(xy,width(im),height(im)) %>% as.data.frame %>%
        mutate(vim=c(im)) %>%
        group_by(value) %>%
        dplyr::summarise(x=weighted.mean(x,w=vim),y=weighted.mean(y,w=vim)) %>%
        select(x,y) %>%
        filter(x %inr% c(1,width(im)),y %inr% c(1,height(im)))
}

##Compute Voronoi diagram for points xy over image of size (w,h)
##Uses a distance transform followed by watershed 
voronoi &lt;- function(xy,w,h)
{
    v &lt;- imfill(w,h)
    ind &lt;- round(xy) %>% index.coord(v,.)
    v[ind] &lt;- seq_along(ind)
    d &lt;- distance_transform(v>0,1)
    watershed(v,-d,fill_lines=FALSE)
}

#image from original paper
im &lt;- load.image(&quot;http://dahtah.github.io/imager/images/stippling_leaves.png&quot;)
out &lt;- stipple(im,1e4,nSteps=5,gamma=1.5)
plot.stipple(im,out)

</pre>
<p><img data-orig-file="https://dahtah.files.wordpress.com/2017/07/stipple.png?w=456" data-orig-size="480,480" data-medium-file="https://dahtah.files.wordpress.com/2017/07/stipple.png?w=456?w=300" data-attachment-id="437" data-image-meta="{" data-image-description="" src="https://dahtah.files.wordpress.com/2017/07/stipple.png?w=456" data-permalink="https://dahtah.wordpress.com/2017/07/24/stippling-and-tsp-art-in-r-emulating-stipplegen/stipple/" data-large-file="https://dahtah.files.wordpress.com/2017/07/stipple.png?w=456?w=480" alt="" data-image-title="stipple" sizes="(max-width: 480px) 100vw, 480px" data-comments-opened="1" class="wp-image-437" data-recalc-dims="1"></p>
<p>TSP art is a variant where you solve a TSP problem to connect all the dots.</p>
<pre>
library(TSP)
##im is the original image (used only for its dimensions)
##out is the output of the stipple function (dot positions)
draw.tsp &lt;- function(im,out)
{
    tour &lt;- out %>% ETSP %>% solve_TSP
    plot(out[tour,],type=&quot;l&quot;,ylim=c(height(im),1),axes=FALSE,xlab=&quot;&quot;,ylab=&quot;&quot;)
}

##Be careful, this is memory-heavy (also, slow)
out &lt;- stipple(im,4e3,gamma=1.5)
draw.tsp(im,out)
</pre>
<p><img data-orig-file="https://dahtah.files.wordpress.com/2017/07/tsp_art.png?w=456" data-orig-size="480,480" data-medium-file="https://dahtah.files.wordpress.com/2017/07/tsp_art.png?w=456?w=300" data-attachment-id="442" data-image-meta="{" data-image-description="" src="https://dahtah.files.wordpress.com/2017/07/tsp_art.png?w=456" data-permalink="https://dahtah.wordpress.com/2017/07/24/stippling-and-tsp-art-in-r-emulating-stipplegen/tsp_art/" data-large-file="https://dahtah.files.wordpress.com/2017/07/tsp_art.png?w=456?w=480" alt="" data-image-title="tsp_art" sizes="(max-width: 480px) 100vw, 480px" data-comments-opened="1" class="wp-image-442" data-recalc-dims="1"></p>
<p>I’ve written <a rel="nofollow" href="http://dahtah.github.io/imager/stippling.html" target="_blank">a more detailed explanation</a> on the imager website, with other variants like stippling with line segments, and a mosaic filter.</p>
<p> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dahtah.wordpress.com/436/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/comments/dahtah.wordpress.com/436/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="1"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://dahtah.wordpress.com/2017/07/24/stippling-and-tsp-art-in-r-emulating-stipplegen/"> R – dahtah</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/URUAuD6gXXk" height="1"></a></div>