<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/new-features-in-imager-0-30/"></div></div><div><a data-url="https://www.r-bloggers.com/new-features-in-imager-0-30/" data-counturl="https://www.r-bloggers.com/new-features-in-imager-0-30/" data-count="horizontal" data-text="New features in imager 0.30" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://dahtah.wordpress.com/2016/09/13/new-features-in-imager-0-30/"> R – dahtah</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>imager is an R package for image processing, based on <a target="_blank" href="http://cimg.eu/">CImg</a>. This new release brings many new features, including:</p>
<ul>
<li>Support for <a target="_blank" href="http://dahtah.github.io/imager/parallel.html">automatic parallel processing</a> using OpenMP.</li>
<li>A new S3 class, imlist, which makes it easy to work with image lists</li>
<li>New functions for interactively selecting image regions (grabRect,grabPoint,grabLine)</li>
<li>Experimental support for CImg’s byte-compiled DSL via imeval, patch_summary.</li>
<li>Improved plotting, API consistency, and documentation</li>
</ul>
<p>To get started with imager, see <a target="_blank" href="http://dahtah.github.io/imager/gettingstarted.html">the tutorial</a>. Some of the new features are detailed below the fold.</p>
<p>And now, for your viewing pleasure, the following piece of code downloads a random cat picture, and makes a video of a bouncing kitten:</p>
<pre>
library(rvest)
library(imager)
#Run a search query (returning html content)
search &lt;- read_html(&quot;https://www.google.com/search?site=&amp;tbm=isch&amp;q=cat&quot;)
#Grab all &lt;img> tags, get their &quot;src&quot; attribute, a URL to an image
urls &lt;- search %>% html_nodes(&quot;img&quot;) %>% html_attr(&quot;src&quot;) #Get urls of parrot pictures

#Load the first image, and resize 
sprite &lt;- load.image(urls[1]) %>% resize(150,150)

#We'll use 30 frames
t &lt;- seq(0,1,l=30)

#Equations of motion
xt &lt;- function(t) 250*t
yt &lt;- function(t) 400- 1100*abs(t-.5)
alpha &lt;- function(t) 1-1.8*abs(t-.5)

#An empty frame for our cat
im &lt;- imfill(400,400,val=rep(0,3))

#Let's make our video
vid &lt;- lapply(t,function(t) imdraw(im,sprite,x=xt(t),y=yt(t),opacity=alpha(t))) %>% imappend(&quot;z&quot;)

play(vid,loop=TRUE,normalise=FALSE)

</pre>
<p><img alt="output" class="wp-image-394" src="https://i1.wp.com/dahtah.files.wordpress.com/2016/08/output.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p><span></span></p>
<p> </p>
<p><strong>A new class for image lists</strong></p>
<p>Image lists (S3 class “imlist”) are simply lists of images, but they come with appropriate generics for plotting, converting, etc. For example, calling imhessian automatically produces an image list</p>
<pre>
imhessian(boats) %>% plot
imhessian(boats) %>% display
imhessian(boats) %>% as.data.frame %>% head
</pre>
<p><img alt="imlist" class="wp-image-401" src="https://i0.wp.com/dahtah.files.wordpress.com/2016/09/imlist.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>There’s also a “map_il” function, inspired by the purrr package. It works essentially like “sapply” but returns an image list:</p>
<pre>
#View image at different blur levels
map_il(seq(1,14,l=5),~ isoblur(boats,.),.id=&quot;v&quot;) %>% plot(layout=&quot;row&quot;)
</pre>
<p><img alt="map_il" class="wp-image-400" src="https://i2.wp.com/dahtah.files.wordpress.com/2016/09/map_il.png?resize=456%2C102&ssl=1" data-recalc-dims="1"></p>
<p>To make an image list, run “imlist” on a list of images:</p>
<pre>
list(a= imnoise(10,10),b= boats) %>% imlist
</pre>
<p><strong>Parallel processing</strong></p>
<p>If possible, imager now enables CImg’s parallel processing features. “If possible” means you need a compiler that supports OpenMP, which includes recent versions of gcc and very recent versions of clang (>= 3.7.0). Many image processing primitives (filters, transformations, etc.) will run in parallel on CPU cores. See here for more on parallel computations.</p>
<p><strong>Interactive selection of image regions</strong></p>
<p>It’s often useful to be able to select image regions for further processing. You can now do so interactively and easily using grabRect:</p>
<pre>
r = grabRect(boats)
</pre>
<p>Here “r” will contain the coordinates of the rectangle you selected. If you want the contents of the rectangle itself, run:</p>
<pre>
im = grabRect(boats,coord=FALSE)
</pre>
<p><strong>Experimental support for CImg’s DSL</strong></p>
<p>CImg includes a byte-compiled mini-language that’s well-suited for simple non-linear filters and image generators. It’s documented <a target="_blank" href="http://gmic.eu/reference.shtml#section9">here</a>.<br>
Here’s a simple box filter you can try:</p>
<pre>
boxf &lt;- &quot;v=0;for(iy=y-3,iy&lt;y+3,iy++,for(ix=x-3,ix&lt; x+3,ix++,v+=i(ix,iy)));v&quot; imeval(boats,boxf) %>% plot
</pre>
<p>A more impressive example is due to David Tschumperlé (the creator of CImg): the Julia set</p>
<pre>
julia &lt;-  &quot;
   zr = -1.2 + 2.4*x/w;
   zi = -1.2 + 2.4*y/h;
   for (iter = 0, zr^2+zi^2&lt;=4 &amp;&amp; iter&lt;256, iter++, t = zr^2 - zi^2 + 0.5; (zi *= 2*zr) += 0.2; zr = t ); iter&quot; imfill(500,500) %>% imeval(julia) %>% plot
</pre>
<p><img alt="julia" class="wp-image-399" src="https://i1.wp.com/dahtah.files.wordpress.com/2016/09/julia.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>I’m still not completely sure how useful this is. On the one hand, R is pretty poor at computations that involve looping over every pixel, and CImg’s DSL is much better. On the other, there’s a lot of ways of getting around having to write pixel loops, and I explore some of them <a target="_blank" href="http://dahtah.github.io/imager/canny.html">here</a>. Feedback appreciated.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dahtah.wordpress.com/393/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/dahtah.wordpress.com/393/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/new-features-in-imager-0-30/"></div></div><div><a data-url="https://www.r-bloggers.com/new-features-in-imager-0-30/" data-counturl="https://www.r-bloggers.com/new-features-in-imager-0-30/" data-count="vertical" data-text="New features in imager 0.30" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<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/2016/09/13/new-features-in-imager-0-30/"> 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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/0ciK9bALB7g"></a></div>