<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/image-convolution-in-r-using-magick/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ropensci.org/technotes/2017/11/02/image-convolve/"> rOpenSci - open tools for open science</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Release 1.4 of the <a rel="nofollow" href="https://cran.r-project.org/web/packages/magick/vignettes/intro.html" target="_blank">magick package</a> introduces<br>
a new feature called <a rel="nofollow" href="https://e.n.wikipedia.org/wiki/Kernel_(image_processing)#Convolution" target="_blank">image convolution</a> that<br>
was requested by Thomas L. Pedersen. In this post we explain what this is all about.</p>
<h2>Kernel Matrix</h2>
<p>The new <code>image_convolve()</code> function applies a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Kernel_(image_processing)" target="_blank">kernel</a> over the image. Kernel convolution means that each pixel value is recalculated using the <em>weighted neighborhood sum</em> defined in the kernel matrix. For example lets look at this simple kernel:</p>
<pre><code>library(magick)

kern &lt;- matrix(0, ncol = 3, nrow = 3)
kern[1, 2] &lt;- 0.25
kern[2, c(1, 3)] &lt;- 0.25
kern[3, 2] &lt;- 0.25
kern
##      [,1] [,2] [,3]
## [1,] 0.00 0.25 0.00
## [2,] 0.25 0.00 0.25
## [3,] 0.00 0.25 0.00
</code></pre>
<p>This kernel changes each pixel to the mean of its horizontal and vertical neighboring pixels, which results in a slight blurring effect in the right-hand image below:</p>
<pre><code>img &lt;- image_read('logo:')
img_blurred &lt;- image_convolve(img, kern)
image_append(c(img, img_blurred))
</code></pre>
<p><img alt="image_appended" src="https://i0.wp.com/i.imgur.com/Y6xByUL.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Standard Kernels</h2>
<p>Many operations in <code>magick</code> such as blurring, sharpening, and edge detection are<br>
actually special cases of image convolution. The benefit of explicitly using<br>
<code>image_convolve()</code> is more control. For example, we can blur an image and then blend<br>
it together with the original image in one step by mixing a blurring kernel with the<br>
unit kernel:</p>
<pre><code>img %>% image_convolve('Gaussian:0x5', scaling = '60,40%')
</code></pre>
<p><img alt="mixed" src="https://i0.wp.com/i.imgur.com/6Vf6c2hl.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The above requires a bit of explanation. ImageMagick defines several common<br>
<a rel="nofollow" href="http://www.imagemagick.org/Usage/convolve/" target="_blank">standard kernels</a> such as the<br>
gaussian kernel. Most of the standard kernels take one or more parameters,<br>
e.g. the example above used a gaussian kernel with 0 <em>radius</em> and 5 <em>sigma</em>.</p>
<p>In addition, <code>scaling</code> argument defines the magnitude of the kernel, and possibly<br>
how much of the original picture should be mixed in. Here we mix 60% of the<br>
blurring with 40% of the original picture in order to get a diffused lightning effect.</p>
<h2>Edge Detection</h2>
<p>Another area where kernels are of use is in edge detection. A simple example of<br>
a direction-aware edge detection kernel is the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Sobel_operator" target="_blank"><em>Sobel</em></a> kernel.<br>
As can be seen below, vertical edges are detected while horizontals are not.</p>
<pre><code>img %>% image_convolve('Sobel') %>% image_negate()
</code></pre>
<p><img alt="edges" src="https://i2.wp.com/i.imgur.com/i8ndfCu.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Something less apparent is that the result of the edge detection is truncated.<br>
Edge detection kernels can result in negative color values which get truncated to zero.<br>
To combat this it is possible to add a <code>bias</code> to the result. Often you’ll end up with<br>
scaling the kernel to 50% and adding 50% bias to move the midpoint of the result to 50%<br>
grey:</p>
<pre><code>img %>% image_convolve('Sobel', scaling = '50%', bias = '50%')
</code></pre>
<p><img alt="50pct" src="https://i0.wp.com/i.imgur.com/llUawrg.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Sharpening</h2>
<p>ImageMagick has many more edge detection kernels, some of which are insensitive to<br>
the direction of the edge. To emulate a classic high-pass filter from photoshop use<br>
<a rel="nofollow" href="https://en.wikipedia.org/wiki/Difference_of_Gaussians" target="_blank">difference of gaussians</a> kernel:</p>
<pre><code>img %>% image_convolve('DoG:0,0,2') %>% image_negate()
</code></pre>
<p><img alt="dog" src="https://i2.wp.com/i.imgur.com/o5kODpc.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>As with the blurring, the original image can be blended in with the transformed one, effectively sharpening the image along edges.</p>
<pre><code>img %>% image_convolve('DoG:0,0,2', scaling = '100, 100%')
</code></pre>
<p><img alt="combination" src="https://i1.wp.com/i.imgur.com/MtcMSn7.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The <a rel="nofollow" href="http://www.imagemagick.org/Usage/convolve/" target="_blank">ImageMagick documentation</a> has more examples of convolve with various avaiable kernels.</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://ropensci.org/technotes/2017/11/02/image-convolve/"> rOpenSci - open tools for open science</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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/YqtV9z28Kcg" height="1"></a></div>