<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/superpixels-in-imager/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://dahtah.wordpress.com/2017/03/24/superpixels-in-imager/"> R – dahtah</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Superpixels are used in image segmentation as a pre-processing step. Instead of segmenting pixels directly, we first group similar pixels into “super-pixels”, which can then be processed further (and more cheaply).</p>
<p><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://dahtah.files.wordpress.com/2017/03/castle_superpix.png" data-orig-size="1280,868" data-image-title="castle_superpix" data-comments-opened="1" class="wp-image-425" data-recalc-dims="1" data-attachment-id="425" data-permalink="https://dahtah.wordpress.com/2017/03/24/superpixels-in-imager/castle_superpix/" alt="" data-medium-file="https://dahtah.files.wordpress.com/2017/03/castle_superpix.png?w=300&amp;resize=300%2C203#038;h=203" data-large-file="https://dahtah.files.wordpress.com/2017/03/castle_superpix.png?w=575" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://dahtah.files.wordpress.com/2017/03/castle_superpix.png?w=300&resize=300%2C203#038;h=203"><br>
(image from <a target="_blank" rel="nofollow" href="https://commons.wikimedia.org/wiki/Commons:Picture_of_the_Year/2016#/media/File:Broadway_tower_edit.jpg">Wikimedia</a>)</p>
<p>The current version of imager doesn’t implement them, but it turns out that <a target="_blank" rel="nofollow" href="http://ivrl.epfl.ch/research/superpixels">SLIC superpixels</a> are particularly easy to implement. SLIC is essentially k-means applied to pixels, with some bells and whistles.</p>
<p><span></span></p>
<p>We could use k-means to segment images based on colour alone. To get good results on colour segmentation the <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Lab_color_space">CIELAB colour space</a> is appropriate, because it tries to be perceptually uniform.</p>
<pre>
library(tidyverse)
library(imager)
im &lt;- load.image(&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/Aster_Tataricus.JPG/1024px-Aster_Tataricus.JPG&quot;)
#Convert to CIELAB colour space, then create a data.frame with three colour channels as columns
d &lt;- sRGBtoLab(im) %>% as.data.frame(wide=&quot;c&quot;)%>%
    dplyr::select(-x,-y)
#Run k-means with 2 centers
km &lt;- kmeans(d,2)
#Turn cluster index into an image
seg &lt;- as.cimg(km$cluster,dim=c(dim(im)[1:2],1,1))
plot(im,axes=FALSE)
highlight(seg==1)
</pre>
<p><img sizes="(max-width: 575px) 100vw, 575px" data-image-description="" data-orig-file="https://dahtah.files.wordpress.com/2017/03/unnamed-chunk-1-1.png" data-orig-size="1344,960" data-image-title="unnamed-chunk-1-1" data-comments-opened="1" class="wp-image-427" data-recalc-dims="1" data-attachment-id="427" data-permalink="https://dahtah.wordpress.com/2017/03/24/superpixels-in-imager/unnamed-chunk-1-1-2/" alt="" data-medium-file="https://dahtah.files.wordpress.com/2017/03/unnamed-chunk-1-1.png?w=300" data-large-file="https://dahtah.files.wordpress.com/2017/03/unnamed-chunk-1-1.png?w=450#038;h=411" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://dahtah.files.wordpress.com/2017/03/unnamed-chunk-1-1.png?w=450#038;h=411"></p>
<p>We mostly manage to separate the petals from the rest, with a few errors here and there.<br>
SLIC does pretty much the same thing, except we (a) use many more centers and (b) we add pixel coordinates as features in the clustering. The latter ensures that only adjacent pixels get grouped together.</p>
<p>The code below implements SLIC. It’s mostly straightforward:</p>
<pre>
#Compute SLIC superpixels
#im: input image
#nS: number of superpixels
#ratio: determines compactness of superpixels.
#low values will result in pixels with weird shapes
#... further arguments passed to kmeans
slic &lt;- function(im,nS,compactness=1,...)
{
    #If image is in colour, convert to CIELAB
    if (spectrum(im) ==3) im &lt;- sRGBtoLab(im)

    #The pixel coordinates vary over 1...width(im) and 1...height(im)
    #Pixel values can be over a widely different range
    #We need our features to have similar scales, so
    #we compute relative scales of spatial dimensions to colour dimensions
    sc.spat &lt;- (dim(im)[1:2]*.28) %>% max #Scale of spatial dimensions
    sc.col &lt;- imsplit(im,&quot;c&quot;) %>% map_dbl(sd) %>% max

    #Scaling ratio for pixel values
    rat &lt;- (sc.spat/sc.col)/(compactness*10)

    
    X &lt;- as.data.frame(im*rat,wide=&quot;c&quot;) %>% as.matrix
    #Generate initial centers from a grid
    ind &lt;- round(seq(1,nPix(im)/spectrum(im),l=nS))
    #Run k-means
    km &lt;- kmeans(X,X[ind,],...)

    #Return segmentation as image (pixel values index cluster)
    seg &lt;- as.cimg(km$cluster,dim=c(dim(im)[1:2],1,1))
    #Superpixel image: each pixel is given the colour of the superpixel it belongs to
    sp &lt;- map(1:spectrum(im),~ km$centers[km$cluster,2+.]) %>% do.call(c,.) %>% as.cimg(dim=dim(im))
    #Correct for ratio
    sp &lt;- sp/rat
    if (spectrum(im)==3)
    {
        #Convert back to RGB
        sp &lt;- LabtosRGB(sp) 
    }
    list(km=km,seg=seg,sp=sp)
}

</pre>
<p>Use it as follows: </p>
<pre>
#400 superpixels
out &lt;- slic(im,400)
#Superpixels
plot(out$sp,axes=FALSE)
#Segmentation
plot(out$seg,axes=FALSE)
#Show segmentation on original image
(im*add.colour(abs(imlap(out$seg)) == 0)) %>% plot(axes=FALSE)
</pre>
<p><img sizes="(max-width: 575px) 100vw, 575px" data-image-description="" data-orig-file="https://dahtah.files.wordpress.com/2017/03/segmentation.png" data-orig-size="2048,1004" data-image-title="segmentation" data-comments-opened="1" class="wp-image-429" data-recalc-dims="1" data-attachment-id="429" data-permalink="https://dahtah.wordpress.com/2017/03/24/superpixels-in-imager/segmentation/" alt="" data-medium-file="https://dahtah.files.wordpress.com/2017/03/segmentation.png?w=300" data-large-file="https://dahtah.files.wordpress.com/2017/03/segmentation.png?w=450#038;h=282" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://dahtah.files.wordpress.com/2017/03/segmentation.png?w=450#038;h=282"><br>
The next step is to segment the superpixels but I’ll keep that for another time.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dahtah.wordpress.com/423/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/dahtah.wordpress.com/423/" 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>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/03/24/superpixels-in-imager/"> 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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/2FZcvb1jyIM"></a></div>