<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-segmentation-based-on-superpixels-and-clustering/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://mlampros.github.io/mlampros.github.io/2018/11/09/Image_Segmentation_Superpixels_Clustering/"> mlampros</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this blog post, I’ll explain the new functionality of the OpenImageR package, <a rel="nofollow" href="https://ivrl.epfl.ch/research-2/research-current/research-superpixels" target="_blank">SLIC and SLICO superpixels</a> (Simple Linear Iterative Clustering) and their applicability based on <a rel="nofollow" href="https://pdfs.semanticscholar.org/6533/654973054b742e725fd433265700c07b48a2.pdf" target="_blank">an IJSR article</a>. The author of the article uses superpixel (SLIC) and Clustering (Affinity Propagation) to perform image segmentation. The article was reproduced (and extended with Kmeans) using the latest versions of the <a rel="nofollow" href="https://github.com/mlampros/OpenImageR" target="_blank">OpenImageR</a> and <a rel="nofollow" href="https://github.com/mlampros/ClusterR" target="_blank">ClusterR</a> packages.</p>
<p></p>
<h3>Superpixels</h3>
<p>“Introduced by Ren and Malik in 2003, superpixels group pixels similar in color and other low-level properties. In this respect, superpixels address two problems inherent to the processing of digital images: firstly, pixels are merely a result of discretization; and secondly, the high number of pixels in large images prevents many algorithms from being computationally feasible. Ren and Malik introduce superpixels as more natural entities – grouping pixels which perceptually belong together while heavily reducing the number of primitives for subsequent algorithms.” You can read more about superpixels in <a rel="nofollow" href="https://arxiv.org/pdf/1612.01601.pdf" target="_blank">this arxiv article</a>.</p>
<p></p>
<p>The <em>superpixels()</em> function of the <em>OpenImageR</em> package is based on the open source C++ code of the <a rel="nofollow" href="https://ivrl.epfl.ch/research-2/research-current/research-superpixels/" target="_blank">IMAGE AND VISUAL REPRESENTATION LAB IVRL</a> and allows users to receive <em>labels</em> and <em>output slic images</em>. The following code chunk illustrates how this can be done using either the “slic” or the “slico” method,</p>
<p></p>
<div>
<div>
<pre><code><span>
</span><span>library</span><span>(</span><span>OpenImageR</span><span>)</span><span>
</span><span>path</span><span> </span><span>=</span><span> </span><span>system.file</span><span>(</span><span>&quot;tmp_images&quot;</span><span>,</span><span> </span><span>&quot;slic_im.png&quot;</span><span>,</span><span> </span><span>package</span><span> </span><span>=</span><span> </span><span>&quot;OpenImageR&quot;</span><span>)</span><span>
</span><span>im</span><span> </span><span>=</span><span> </span><span>readImage</span><span>(</span><span>path</span><span>)</span><span>
</span><span>#--------------</span><span>
</span><span># &quot;slic&quot; method</span><span>
</span><span>#--------------</span><span>
</span><span>res_slic</span><span> </span><span>=</span><span> </span><span>superpixels</span><span>(</span><span>input_image</span><span> </span><span>=</span><span> </span><span>im</span><span>,</span><span>
</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;slic&quot;</span><span>,</span><span>
</span><span>superpixel</span><span> </span><span>=</span><span> </span><span>200</span><span>,</span><span> </span><span>compactness</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span>
</span><span>return_slic_data</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>return_labels</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>write_slic</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>str</span><span>(</span><span>res_slic</span><span>)</span><span>
</span><span>List</span><span> </span><span>of</span><span> </span><span>2</span><span>
</span><span>$</span><span> </span><span>slic_data</span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>360</span><span>,</span><span> </span><span>1</span><span>:</span><span>360</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>...</span><span>
</span><span>$</span><span> </span><span>labels</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>360</span><span>,</span><span> </span><span>1</span><span>:</span><span>360</span><span>]</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>...</span><span>
</span><span>#---------------</span><span>
</span><span># &quot;slico&quot; method [ the &quot;slico&quot; method does not require the &quot;compactness&quot; parameter ]</span><span>
</span><span>#---------------</span><span>
</span><span>res_slico</span><span> </span><span>=</span><span> </span><span>superpixels</span><span>(</span><span>input_image</span><span> </span><span>=</span><span> </span><span>im</span><span>,</span><span>
</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;slico&quot;</span><span>,</span><span>
</span><span>superpixel</span><span> </span><span>=</span><span> </span><span>200</span><span>,</span><span> </span><span>return_slic_data</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>return_labels</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>write_slic</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>str</span><span>(</span><span>res_slico</span><span>)</span><span>
</span><span>List</span><span> </span><span>of</span><span> </span><span>2</span><span>
</span><span>$</span><span> </span><span>slic_data</span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>360</span><span>,</span><span> </span><span>1</span><span>:</span><span>360</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>255</span><span> </span><span>...</span><span>
</span><span>$</span><span> </span><span>labels</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>360</span><span>,</span><span> </span><span>1</span><span>:</span><span>360</span><span>]</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>...</span><span>
</span></code></pre>
</div>
</div>
<p></p>
<div>
<div>
<pre><code><span>
</span><span>#-----------------------------</span><span>
</span><span># plot both &quot;slic&quot; and &quot;slico&quot;</span><span>
</span><span>#-----------------------------</span><span>
</span><span>par</span><span>(</span><span>mfrow</span><span>=</span><span>c</span><span>(</span><span>1</span><span>,</span><span>2</span><span>),</span><span> </span><span>mar</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.2</span><span>,</span><span> </span><span>0.2</span><span>,</span><span> </span><span>0.2</span><span>,</span><span> </span><span>0.2</span><span>))</span><span>
</span><span>plot_slic</span><span> </span><span>=</span><span> </span><span>OpenImageR</span><span>::</span><span>NormalizeObject</span><span>(</span><span>res_slic</span><span>$</span><span>slic_data</span><span>)</span><span>
</span><span>plot_slic</span><span> </span><span>=</span><span> </span><span>grDevices</span><span>::</span><span>as.raster</span><span>(</span><span>plot_slic</span><span>)</span><span>
</span><span>graphics</span><span>::</span><span>plot</span><span>(</span><span>plot_slic</span><span>)</span><span>
</span><span>plot_slico</span><span> </span><span>=</span><span> </span><span>OpenImageR</span><span>::</span><span>NormalizeObject</span><span>(</span><span>res_slico</span><span>$</span><span>slic_data</span><span>)</span><span>
</span><span>plot_slico</span><span> </span><span>=</span><span> </span><span>grDevices</span><span>::</span><span>as.raster</span><span>(</span><span>plot_slico</span><span>)</span><span>
</span><span>graphics</span><span>::</span><span>plot</span><span>(</span><span>plot_slico</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p></p>
<p><img alt="Alt text" src="https://i1.wp.com/mlampros.github.io/images/slic_vs_slico.png?w=456" data-recalc-dims="1"></p>
<p></p>
<p>According to the <a rel="nofollow" href="https://ivrl.epfl.ch/research-2/research-current/research-superpixels/" target="_blank">authors of the slic method</a>, “<em>SLIC uses the same compactness parameter (chosen by user) for all superpixels in the image. If the image is smooth in certain regions but highly textured in others, SLIC produces smooth regular-sized superpixels in the smooth regions and highly irregular superpixels in the textured regions. So, it becomes tricky choosing the right parameter for each image. SLICO does away with this problem completely. The user no longer has to set the compactness parameter or try different values of it. SLICO adaptively chooses the compactness parameter for each superpixel differently. This generates regular shaped superpixels in both textured and non textured regions alike. The improvement comes with hardly any compromise on the computational efficiency – SLICO continues to be as fast as SLIC</em>.”</p>
<p></p>
<p>More information about the parameters of the <em>superpixels()</em> function can be found in the package documentation.</p>
<p></p>
<h3>Image segmentation</h3>
<p>“In computer vision, image segmentation is the process of partitioning a digital image into multiple segments (sets of pixels, also known as super-pixels). The goal of segmentation is to simplify and/or change the representation of an image into something that is more meaningful and easier to analyze. Image segmentation is typically used to locate objects and boundaries (lines, curves, etc.) in images. More precisely, image segmentation is the process of assigning a label to every pixel in an image such that pixels with the same label share certain characteristics.” You can read more about image segmentation in this <a rel="nofollow" href="https://en.wikipedia.org/wiki/Image_segmentation" target="_blank">wikipedia article</a>.</p>
<p></p>
<h3>Affinity Propagation</h3>
<p>“Affinity propagation is a new algorithm that takes as input measures of similarity between pairs of data points and simultaneously considers all data points as potential exemplars. Real-valued messages are exchanged between data points until a high-quality set of exemplars and corresponding clusters gradually emerges. We have used affinity propagation to solve a variety of clustering problems and we found that it uniformly found clusters with much lower error than those found by other methods, and it did so in less than one-hundredth the amount of time. Because of its simplicity, general applicability, and performance, we believe affinity propagation will prove to be of broad value in science and engineering.” The Affinity Propagation algorithm is explained in detail in the <a rel="nofollow" href="https://www.psi.toronto.edu/index.php?q=affinity%20propagation" target="_blank">authors web page</a>.</p>
<p></p>
<h3>Image segmentation based on superpixels (SLIC, SLICO) and Affinity Propagation (AP)</h3>
<p>The <a rel="nofollow" href="https://pdfs.semanticscholar.org/6533/654973054b742e725fd433265700c07b48a2.pdf" target="_blank">author of the article</a>, that I mentioned earlier, uses a method named <strong>SLICAP</strong> (SLIC + AP) to perform image segmentation. The methodology is the following:</p>
<ul>
<li>First, the SLICAP technique uses the SLIC superpixel algorithm to form an over-segmentation of an image</li>
<li>Then, a similarity is constructed based on the features of superpixels</li>
<li>Finally, the AP algorithm clusters these superpixels with the similarities obtained</li>
</ul>
<p></p>
<p>The advantages of this method compared to other methods are the following:</p>
<ul>
<li>similarities are not computed based on all data (image), but on the resulted superpixels (reduction of the computational burden)</li>
<li>the user does not have to specify the number of clusters (AP algorithm) as is the case with other clustering methods such as Kmeans</li>
</ul>
<p></p>
<p>The author of the article utilizes,</p>
<ul>
<li>the <a rel="nofollow" href="https://en.wikipedia.org/wiki/CIELAB_color_space" target="_blank">CIELAB colour space</a> which is designed to be perceptually uniform with respect to human color vision</li>
<li>the <em>negative Euclidean distance</em> (although the author uses 3 different similarity measures (page 1526), I’ll use the first one in my code because it gives better results)</li>
<li><em>weights for the 3 colour channels</em> (wL, wA, wB) because they keep balance so as to be consistent with human perception</li>
<li>a <em>colorradius</em> to adjust the number of clusters. If its value is low, the number of targets would increase, which leads to more detailed segmentation results</li>
<li>the <a rel="nofollow" href="https://www2.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/resources.html" target="_blank">Berkeley Segmentation Database (BSD)</a> (it can be downloaded also from my <a rel="nofollow" href="https://github.com/mlampros/DataSets" target="_blank">Github repository</a>)</li>
</ul>
<p></p>
<p>I bundled the code in an R package, <a rel="nofollow" href="https://github.com/mlampros/SuperpixelImageSegmentation" target="_blank">SuperpixelImageSegmentation</a>, which can be downloaded from Github (consult the <a rel="nofollow" href="https://github.com/mlampros/SuperpixelImageSegmentation/blob/master/README.md" target="_blank">README.md file</a> of the package for more information). The following code snippet first reads the input image and then performs image segmentation based on SLIC superpixels and AP clustering,</p>
<p></p>
<div>
<div>
<pre><code><span>
</span><span>library</span><span>(</span><span>SuperpixelImageSegmentation</span><span>)</span><span>
</span><span>path</span><span> </span><span>=</span><span> </span><span>system.file</span><span>(</span><span>&quot;images&quot;</span><span>,</span><span> </span><span>&quot;BSR_bsds500_image.jpg&quot;</span><span>,</span><span> </span><span>package</span><span> </span><span>=</span><span> </span><span>&quot;SuperpixelImageSegmentation&quot;</span><span>)</span><span>
</span><span>im</span><span> </span><span>=</span><span> </span><span>OpenImageR</span><span>::</span><span>readImage</span><span>(</span><span>path</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p></p>
<p><img alt="Alt text" src="https://i2.wp.com/mlampros.github.io/images/BSR_bsds500_image.jpg?w=456" data-recalc-dims="1"></p>
<p></p>
<div>
<div>
<pre><code><span>
</span><span>init</span><span> </span><span>=</span><span> </span><span>Image_Segmentation</span><span>$</span><span>new</span><span>()</span><span>
</span><span>spx</span><span> </span><span>=</span><span> </span><span>init</span><span>$</span><span>spixel_segmentation</span><span>(</span><span>input_image</span><span> </span><span>=</span><span> </span><span>im</span><span>,</span><span> </span><span>superpixel</span><span> </span><span>=</span><span> </span><span>600</span><span>,</span><span> </span><span>AP_data</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>use_median</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>sim_wL</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>sim_wA</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>sim_wB</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span>
</span><span>sim_color_radius</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>Sequential</span><span> </span><span>computation</span><span> </span><span>starts</span><span> </span><span>...</span><span>
</span><span>WARNING</span><span>:</span><span> </span><span>The</span><span> </span><span>input</span><span> </span><span>data</span><span> </span><span>has</span><span> </span><span>values</span><span> </span><span>between</span><span> </span><span>0.000000</span><span> </span><span>and</span><span> </span><span>1.000000</span><span>.</span><span> </span><span>The</span><span> </span><span>image</span><span>-</span><span>data</span><span> </span><span>will</span><span> </span><span>be</span><span> </span><span>multiplied</span><span> </span><span>by</span><span> </span><span>the</span><span> </span><span>value</span><span>:</span><span> </span><span>255</span><span>!</span><span>
</span><span>The</span><span> </span><span>super</span><span>-</span><span>pixel</span><span> </span><span>slic</span><span> </span><span>method</span><span> </span><span>as</span><span> </span><span>pre</span><span>-</span><span>processing</span><span> </span><span>step</span><span> </span><span>was</span><span> </span><span>used</span><span> </span><span>/</span><span> </span><span>completed</span><span>!</span><span>
</span><span>The</span><span> </span><span>similarity</span><span> </span><span>matrix</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>super</span><span>-</span><span>pixels</span><span> </span><span>was</span><span> </span><span>computed</span><span>!</span><span>
</span><span>It</span><span> </span><span>took</span><span> </span><span>179</span><span> </span><span>iterations</span><span> </span><span>for</span><span> </span><span>affinity</span><span> </span><span>propagation</span><span> </span><span>to</span><span> </span><span>complete</span><span>!</span><span>
</span><span>6</span><span> </span><span>clusters</span><span> </span><span>were</span><span> </span><span>chosen</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>super</span><span>-</span><span>pixels</span><span> </span><span>and</span><span> </span><span>affinity</span><span> </span><span>propagation</span><span>!</span><span>
</span><span>Image</span><span> </span><span>data</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>Affinity</span><span> </span><span>Propagation</span><span> </span><span>clustering</span><span> </span><span>(</span><span>'AP_image_data'</span><span>)</span><span> </span><span>will</span><span> </span><span>be</span><span> </span><span>returned</span><span>!</span><span>
</span><span>Elapsed</span><span> </span><span>time</span><span>:</span><span> </span><span>0</span><span> </span><span>hours</span><span> </span><span>and</span><span> </span><span>0</span><span> </span><span>minutes</span><span> </span><span>and</span><span> </span><span>2</span><span> </span><span>seconds.</span><span> </span></code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>str</span><span>(</span><span>spx</span><span>)</span><span>
</span><span>List</span><span> </span><span>of</span><span> </span><span>4</span><span>
</span><span>$</span><span> </span><span>KMeans_image_data</span><span>:</span><span> </span><span>num</span><span>[</span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>]</span><span> </span><span>$</span><span> </span><span>masks</span><span> </span><span>:</span><span> </span><span>list</span><span>()</span><span>
</span><span>..</span><span>-</span><span> </span><span>attr</span><span>(</span><span>*</span><span>,</span><span> </span><span>&quot;dim&quot;</span><span>)</span><span>=</span><span> </span><span>int</span><span> </span><span>[</span><span>1</span><span>:</span><span>2</span><span>]</span><span> </span><span>0</span><span> </span><span>0</span><span>
</span><span>$</span><span> </span><span>centr</span><span> </span><span>:</span><span> </span><span>num</span><span>[</span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>]</span><span> </span><span>$</span><span> </span><span>AP_image_data</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>1</span><span> </span><span>1</span><span> </span><span>1</span><span> </span><span>1</span><span> </span><span>1</span><span> </span><span>1</span><span> </span><span>1</span><span> </span><span>1</span><span> </span><span>1</span><span> </span><span>1</span><span> </span><span>...</span><span>
</span></code></pre>
</div>
</div>
<p></p>
<p>By using a <em>colorradius</em> of 10 (the author uses by default 20) I receive the following output,</p>
<p></p>
<div>
<div>
<pre><code><span>
</span><span>OpenImageR</span><span>::</span><span>imageShow</span><span>(</span><span>spx</span><span>$</span><span>AP_image_data</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p></p>
<p><img alt="Alt text" src="https://i2.wp.com/mlampros.github.io/images/BSR_bsds500_segmentation.png?w=456" data-recalc-dims="1"></p>
<p></p>
<p>Decreasing the number of superpixels (from 600 to 200) or modifying the colorradius (from 10 to 20) will affect the quality of the output image, but it will also reduce the computation time.</p>
<p></p>
<h3>Superpixels, AP and Kmeans (or Mini-Batch-Kmeans)</h3>
<p>As a follow-up to the author’s method, one might take advantage of the automatic way the AP algorithm determines the number of clusters in order to perform vector quantization using kmeans (this will give qualitative better results at the cost of increasing computation time). I included additional parameters in the <em>spixel_segmentation()</em> method of the <em>Image_Segmentation</em> R6-class for this purpose and I’ll use the following image, which appears also in the article,</p>
<p></p>
<div>
<div>
<pre><code><span>
</span><span>path_km</span><span> </span><span>=</span><span> </span><span>system.file</span><span>(</span><span>&quot;images&quot;</span><span>,</span><span> </span><span>&quot;airplane.jpg&quot;</span><span>,</span><span> </span><span>package</span><span> </span><span>=</span><span> </span><span>&quot;SuperpixelImageSegmentation&quot;</span><span>)</span><span>
</span><span>im_km</span><span> </span><span>=</span><span> </span><span>OpenImageR</span><span>::</span><span>readImage</span><span>(</span><span>path_km</span><span>)</span><span>
</span><span>OpenImageR</span><span>::</span><span>imageShow</span><span>(</span><span>im_km</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p></p>
<p><img alt="Alt text" src="https://i1.wp.com/mlampros.github.io/images/airplane.jpg?w=456" data-recalc-dims="1"></p>
<p></p>
<div>
<div>
<pre><code><span>
</span><span>spx_km</span><span> </span><span>=</span><span> </span><span>init</span><span>$</span><span>spixel_segmentation</span><span>(</span><span>input_image</span><span> </span><span>=</span><span> </span><span>im_km</span><span>,</span><span> </span><span>superpixel</span><span> </span><span>=</span><span> </span><span>600</span><span>,</span><span> </span><span>AP_data</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>use_median</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>sim_wL</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>sim_wA</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>sim_wB</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span>
</span><span>sim_color_radius</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>kmeans_method</span><span> </span><span>=</span><span> </span><span>&quot;kmeans&quot;</span><span>,</span><span>
</span><span>kmeans_initializer</span><span> </span><span>=</span><span> </span><span>&quot;kmeans++&quot;</span><span>,</span><span>
</span><span>kmeans_num_init</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>kmeans_max_iters</span><span> </span><span>=</span><span> </span><span>100</span><span>,</span><span>
</span><span>verbose</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>Sequential</span><span> </span><span>computation</span><span> </span><span>starts</span><span> </span><span>...</span><span>
</span><span>WARNING</span><span>:</span><span> </span><span>The</span><span> </span><span>input</span><span> </span><span>data</span><span> </span><span>has</span><span> </span><span>values</span><span> </span><span>between</span><span> </span><span>0.000000</span><span> </span><span>and</span><span> </span><span>1.000000</span><span>.</span><span> </span><span>The</span><span> </span><span>image</span><span>-</span><span>data</span><span> </span><span>will</span><span> </span><span>be</span><span> </span><span>multiplied</span><span> </span><span>by</span><span> </span><span>the</span><span> </span><span>value</span><span>:</span><span> </span><span>255</span><span>!</span><span>
</span><span>The</span><span> </span><span>super</span><span>-</span><span>pixel</span><span> </span><span>slic</span><span> </span><span>method</span><span> </span><span>as</span><span> </span><span>pre</span><span>-</span><span>processing</span><span> </span><span>step</span><span> </span><span>was</span><span> </span><span>used</span><span> </span><span>/</span><span> </span><span>completed</span><span>!</span><span>
</span><span>The</span><span> </span><span>similarity</span><span> </span><span>matrix</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>super</span><span>-</span><span>pixels</span><span> </span><span>was</span><span> </span><span>computed</span><span>!</span><span>
</span><span>It</span><span> </span><span>took</span><span> </span><span>146</span><span> </span><span>iterations</span><span> </span><span>for</span><span> </span><span>affinity</span><span> </span><span>propagation</span><span> </span><span>to</span><span> </span><span>complete</span><span>!</span><span>
</span><span>10</span><span> </span><span>clusters</span><span> </span><span>were</span><span> </span><span>chosen</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>super</span><span>-</span><span>pixels</span><span> </span><span>and</span><span> </span><span>affinity</span><span> </span><span>propagation</span><span>!</span><span>
</span><span>Image</span><span> </span><span>data</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>Affinity</span><span> </span><span>Propagation</span><span> </span><span>clustering</span><span> </span><span>(</span><span>'AP_image_data'</span><span>)</span><span> </span><span>will</span><span> </span><span>be</span><span> </span><span>returned</span><span>!</span><span>
</span><span>The</span><span> </span><span>kmeans</span><span> </span><span>algorithm</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>the</span><span> </span><span>number</span><span> </span><span>of</span><span> </span><span>affinity</span><span>-</span><span>propagation</span><span>-</span><span>clusters</span><span> </span><span>was</span><span> </span><span>completed</span><span>!</span><span>
</span><span>Pre</span><span>-</span><span>processing</span><span> </span><span>of</span><span> </span><span>the</span><span> </span><span>kmeans</span><span>-</span><span>output</span><span>-</span><span>image</span><span> </span><span>is</span><span> </span><span>completed</span><span>!</span><span>
</span><span>Elapsed</span><span> </span><span>time</span><span>:</span><span> </span><span>0</span><span> </span><span>hours</span><span> </span><span>and</span><span> </span><span>0</span><span> </span><span>minutes</span><span> </span><span>and</span><span> </span><span>8</span><span> </span><span>seconds.</span><span> </span><span>str</span><span>(</span><span>spx_km</span><span>)</span><span>
</span><span>List</span><span> </span><span>of</span><span> </span><span>4</span><span>
</span><span>$</span><span> </span><span>KMeans_image_data</span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0.1</span><span> </span><span>0.1</span><span> </span><span>0.26</span><span> </span><span>0.389</span><span> </span><span>0.432</span><span> </span><span>...</span><span>
</span><span>$</span><span> </span><span>masks</span><span> </span><span>:</span><span>List</span><span> </span><span>of</span><span> </span><span>10</span><span>
</span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span>[</span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>]</span><span> </span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span>[</span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>]</span><span> </span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span>[</span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>]</span><span> </span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span>[</span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>]</span><span> </span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span>[</span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>]</span><span> </span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span>[</span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>]</span><span> </span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span>[</span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>]</span><span> </span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span>[</span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>]</span><span> </span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span>[</span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>]</span><span> </span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span>[</span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>,</span><span> </span><span>0</span><span> </span><span>]</span><span> </span><span>..</span><span>-</span><span> </span><span>attr</span><span>(</span><span>*</span><span>,</span><span> </span><span>&quot;dim&quot;</span><span>)</span><span>=</span><span> </span><span>int</span><span> </span><span>[</span><span>1</span><span>:</span><span>2</span><span>]</span><span> </span><span>10</span><span> </span><span>1</span><span>
</span><span>$</span><span> </span><span>centr</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>10</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0.432</span><span> </span><span>0.251</span><span> </span><span>0.863</span><span> </span><span>0.552</span><span> </span><span>0.26</span><span> </span><span>...</span><span>
</span><span>$</span><span> </span><span>AP_image_data</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0.404</span><span> </span><span>0.404</span><span> </span><span>0.404</span><span> </span><span>0.404</span><span> </span><span>0.404</span><span> </span><span>...</span><span>
</span></code></pre>
</div>
</div>
<p></p>
<p>The following image shows the output based on <em>Superpixels + AP</em>,</p>
<p></p>
<div>
<div>
<pre><code><span>
</span><span>OpenImageR</span><span>::</span><span>imageShow</span><span>(</span><span>spx_km</span><span>$</span><span>AP_image_data</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p></p>
<p><img alt="Alt text" src="https://i0.wp.com/mlampros.github.io/images/segment_AP.png?w=456" data-recalc-dims="1"></p>
<p></p>
<p>and the following based on <em>Superpixels + AP + Kmeans (vector quantization)</em>,</p>
<p></p>
<div>
<div>
<pre><code><span>
</span><span>OpenImageR</span><span>::</span><span>imageShow</span><span>(</span><span>spx_km</span><span>$</span><span>KMeans_image_data</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p></p>
<p><img alt="Alt text" src="https://i1.wp.com/mlampros.github.io/images/segment_kmeans.png?w=456" data-recalc-dims="1"></p>
<p></p>
<p>The <em>spixel_segmentation()</em> takes as method parameter the <em>Mini-Batch-Kmeans</em> too, which can decrease the computation time at the cost of a reduced image quality. However, the advantage of using <em>Superpixels + AP + Kmeans (or Mini-Batch-Kmeans)</em> <em>lies in the fact that the user does not have to specify the number of clusters beforehand</em>,</p>
<p></p>
<div>
<div>
<pre><code><span>
</span><span>spx_mbkm</span><span> </span><span>=</span><span> </span><span>init</span><span>$</span><span>spixel_segmentation</span><span>(</span><span>input_image</span><span> </span><span>=</span><span> </span><span>im_km</span><span>,</span><span> </span><span>superpixel</span><span> </span><span>=</span><span> </span><span>600</span><span>,</span><span> </span><span>AP_data</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>use_median</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>sim_wL</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>sim_wA</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>sim_wB</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span>
</span><span>sim_color_radius</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>kmeans_method</span><span> </span><span>=</span><span> </span><span>&quot;mini_batch_kmeans&quot;</span><span>,</span><span>
</span><span>kmeans_initializer</span><span> </span><span>=</span><span> </span><span>&quot;kmeans++&quot;</span><span>,</span><span>
</span><span>kmeans_num_init</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>kmeans_max_iters</span><span> </span><span>=</span><span> </span><span>100</span><span>,</span><span>
</span><span>minib_kmeans_batch</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>minib_kmeans_init_fraction</span><span> </span><span>=</span><span> </span><span>0.75</span><span>,</span><span>
</span><span>verbose</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>Sequential</span><span> </span><span>computation</span><span> </span><span>starts</span><span> </span><span>...</span><span>
</span><span>WARNING</span><span>:</span><span> </span><span>The</span><span> </span><span>input</span><span> </span><span>data</span><span> </span><span>has</span><span> </span><span>values</span><span> </span><span>between</span><span> </span><span>0.000000</span><span> </span><span>and</span><span> </span><span>1.000000</span><span>.</span><span> </span><span>The</span><span> </span><span>image</span><span>-</span><span>data</span><span> </span><span>will</span><span> </span><span>be</span><span> </span><span>multiplied</span><span> </span><span>by</span><span> </span><span>the</span><span> </span><span>value</span><span>:</span><span> </span><span>255</span><span>!</span><span>
</span><span>The</span><span> </span><span>super</span><span>-</span><span>pixel</span><span> </span><span>slic</span><span> </span><span>method</span><span> </span><span>as</span><span> </span><span>pre</span><span>-</span><span>processing</span><span> </span><span>step</span><span> </span><span>was</span><span> </span><span>used</span><span> </span><span>/</span><span> </span><span>completed</span><span>!</span><span>
</span><span>The</span><span> </span><span>similarity</span><span> </span><span>matrix</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>super</span><span>-</span><span>pixels</span><span> </span><span>was</span><span> </span><span>computed</span><span>!</span><span>
</span><span>It</span><span> </span><span>took</span><span> </span><span>146</span><span> </span><span>iterations</span><span> </span><span>for</span><span> </span><span>affinity</span><span> </span><span>propagation</span><span> </span><span>to</span><span> </span><span>complete</span><span>!</span><span>
</span><span>10</span><span> </span><span>clusters</span><span> </span><span>were</span><span> </span><span>chosen</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>super</span><span>-</span><span>pixels</span><span> </span><span>and</span><span> </span><span>affinity</span><span> </span><span>propagation</span><span>!</span><span>
</span><span>Image</span><span> </span><span>data</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>Affinity</span><span> </span><span>Propagation</span><span> </span><span>clustering</span><span> </span><span>(</span><span>'AP_image_data'</span><span>)</span><span> </span><span>will</span><span> </span><span>be</span><span> </span><span>returned</span><span>!</span><span>
</span><span>The</span><span> </span><span>mini</span><span>-</span><span>batch</span><span>-</span><span>kmeans</span><span> </span><span>algorithm</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>the</span><span> </span><span>number</span><span> </span><span>of</span><span> </span><span>affinity</span><span>-</span><span>propagation</span><span>-</span><span>clusters</span><span> </span><span>was</span><span> </span><span>completed</span><span>!</span><span>
</span><span>Pre</span><span>-</span><span>processing</span><span> </span><span>of</span><span> </span><span>the</span><span> </span><span>mini</span><span>-</span><span>batch</span><span>-</span><span>kmeans</span><span>-</span><span>output</span><span>-</span><span>image</span><span> </span><span>is</span><span> </span><span>completed</span><span>!</span><span>
</span><span>Elapsed</span><span> </span><span>time</span><span>:</span><span> </span><span>0</span><span> </span><span>hours</span><span> </span><span>and</span><span> </span><span>0</span><span> </span><span>minutes</span><span> </span><span>and</span><span> </span><span>3</span><span> </span><span>seconds.</span><span> </span></code></pre>
</div>
</div>
<p></p>
<p>The following image is based on <em>Superpixels + AP + Mini-Batch-Kmeans</em>,</p>
<p></p>
<div>
<div>
<pre><code><span>
</span><span>OpenImageR</span><span>::</span><span>imageShow</span><span>(</span><span>spx_mbkm</span><span>$</span><span>KMeans_image_data</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p></p>
<p><img alt="Alt text" src="https://i1.wp.com/mlampros.github.io/images/segment_mbkm.png?w=456" data-recalc-dims="1"></p>
<p></p>
<p>Further parameters which can be adjusted in the <em>spixel_segmentation()</em> method are:</p>
<ul>
<li><em>method</em> (“slic”, “slico”)</li>
<li><em>colour_type</em> (“RGB”, “LAB” or “HSV”)</li>
</ul>
<p>More details can be found in the <a rel="nofollow" href="https://github.com/mlampros/SuperpixelImageSegmentation" target="_blank">SuperpixelImageSegmentation</a> R package documentation.</p>
<p></p>
<h3>Image Masks</h3>
<p>The <em>spixel_masks_show()</em> method in combination with the <em>adjust_centroids_and_return_masks</em> parameter (set to TRUE), allows users to also plot the masks belonging to each separate cluster,</p>
<p></p>
<div>
<div>
<pre><code><span>
</span><span>spx_masks</span><span> </span><span>=</span><span> </span><span>init</span><span>$</span><span>spixel_segmentation</span><span>(</span><span>input_image</span><span> </span><span>=</span><span> </span><span>im_km</span><span>,</span><span> </span><span>superpixel</span><span> </span><span>=</span><span> </span><span>600</span><span>,</span><span> </span><span>AP_data</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>use_median</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>sim_wL</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>sim_wA</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>sim_wB</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span>
</span><span>sim_color_radius</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>kmeans_method</span><span> </span><span>=</span><span> </span><span>&quot;kmeans&quot;</span><span>,</span><span>
</span><span>kmeans_initializer</span><span> </span><span>=</span><span> </span><span>&quot;kmeans++&quot;</span><span>,</span><span>
</span><span>adjust_centroids_and_return_masks</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>verbose</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>Sequential</span><span> </span><span>computation</span><span> </span><span>starts</span><span> </span><span>...</span><span>
</span><span>WARNING</span><span>:</span><span> </span><span>The</span><span> </span><span>input</span><span> </span><span>data</span><span> </span><span>has</span><span> </span><span>values</span><span> </span><span>between</span><span> </span><span>0.000000</span><span> </span><span>and</span><span> </span><span>1.000000</span><span>.</span><span> </span><span>The</span><span> </span><span>image</span><span>-</span><span>data</span><span> </span><span>will</span><span> </span><span>be</span><span> </span><span>multiplied</span><span> </span><span>by</span><span> </span><span>the</span><span> </span><span>value</span><span>:</span><span> </span><span>255</span><span>!</span><span>
</span><span>The</span><span> </span><span>super</span><span>-</span><span>pixel</span><span> </span><span>slic</span><span> </span><span>method</span><span> </span><span>as</span><span> </span><span>pre</span><span>-</span><span>processing</span><span> </span><span>step</span><span> </span><span>was</span><span> </span><span>used</span><span> </span><span>/</span><span> </span><span>completed</span><span>!</span><span>
</span><span>The</span><span> </span><span>similarity</span><span> </span><span>matrix</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>super</span><span>-</span><span>pixels</span><span> </span><span>was</span><span> </span><span>computed</span><span>!</span><span>
</span><span>It</span><span> </span><span>took</span><span> </span><span>146</span><span> </span><span>iterations</span><span> </span><span>for</span><span> </span><span>affinity</span><span> </span><span>propagation</span><span> </span><span>to</span><span> </span><span>complete</span><span>!</span><span>
</span><span>10</span><span> </span><span>clusters</span><span> </span><span>were</span><span> </span><span>chosen</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>super</span><span>-</span><span>pixels</span><span> </span><span>and</span><span> </span><span>affinity</span><span> </span><span>propagation</span><span>!</span><span>
</span><span>Image</span><span> </span><span>data</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>Affinity</span><span> </span><span>Propagation</span><span> </span><span>clustering</span><span> </span><span>(</span><span>'AP_image_data'</span><span>)</span><span> </span><span>will</span><span> </span><span>be</span><span> </span><span>returned</span><span>!</span><span>
</span><span>The</span><span> </span><span>kmeans</span><span> </span><span>algorithm</span><span> </span><span>based</span><span> </span><span>on</span><span> </span><span>the</span><span> </span><span>number</span><span> </span><span>of</span><span> </span><span>affinity</span><span>-</span><span>propagation</span><span>-</span><span>clusters</span><span> </span><span>was</span><span> </span><span>completed</span><span>!</span><span>
</span><span>NOTE</span><span>:</span><span> </span><span>The</span><span> </span><span>'KMeans_image_data'</span><span> </span><span>will</span><span> </span><span>be</span><span> </span><span>returned</span><span> </span><span>in</span><span> </span><span>black</span><span> </span><span>&amp;</span><span> </span><span>white</span><span> </span><span>colour</span><span> </span><span>due</span><span> </span><span>to</span><span> </span><span>the</span><span> </span><span>fact</span><span> </span><span>that</span><span> </span><span>the</span><span> </span><span>'adjust_centroids_and_return_masks'</span><span> </span><span>parameter</span><span> </span><span>was</span><span> </span><span>set</span><span> </span><span>to</span><span> </span><span>TRUE</span><span>!</span><span>
</span><span>Pre</span><span>-</span><span>processing</span><span> </span><span>of</span><span> </span><span>the</span><span> </span><span>kmeans</span><span>-</span><span>output</span><span>-</span><span>image</span><span> </span><span>is</span><span> </span><span>completed</span><span>!</span><span>
</span><span>The</span><span> </span><span>centroids</span><span> </span><span>were</span><span> </span><span>adjusted</span><span> </span><span>and</span><span> </span><span>the</span><span> </span><span>image</span><span>-</span><span>masks</span><span> </span><span>will</span><span> </span><span>be</span><span> </span><span>returned</span><span>!</span><span>
</span><span>Elapsed</span><span> </span><span>time</span><span>:</span><span> </span><span>0</span><span> </span><span>hours</span><span> </span><span>and</span><span> </span><span>0</span><span> </span><span>minutes</span><span> </span><span>and</span><span> </span><span>8</span><span> </span><span>seconds.</span><span> </span><span>str</span><span>(</span><span>spx_masks</span><span>)</span><span>
</span><span>List</span><span> </span><span>of</span><span> </span><span>4</span><span>
</span><span>$</span><span> </span><span>KMeans_image_data</span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>5</span><span> </span><span>5</span><span> </span><span>4</span><span> </span><span>7</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>7</span><span> </span><span>7</span><span> </span><span>7</span><span> </span><span>7</span><span> </span><span>...</span><span>
</span><span>$</span><span> </span><span>masks</span><span> </span><span>:</span><span>List</span><span> </span><span>of</span><span> </span><span>10</span><span>
</span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0.404</span><span> </span><span>...</span><span>
</span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>...</span><span>
</span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>...</span><span>
</span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>...</span><span>
</span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0.294</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>...</span><span>
</span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0.0784</span><span> </span><span>0.1216</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>...</span><span>
</span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>...</span><span>
</span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0.392</span><span> </span><span>0</span><span> </span><span>...</span><span>
</span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>...</span><span>
</span><span>..</span><span>$</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>...</span><span>
</span><span>..</span><span>-</span><span> </span><span>attr</span><span>(</span><span>*</span><span>,</span><span> </span><span>&quot;dim&quot;</span><span>)</span><span>=</span><span> </span><span>int</span><span> </span><span>[</span><span>1</span><span>:</span><span>2</span><span>]</span><span> </span><span>10</span><span> </span><span>1</span><span>
</span><span>$</span><span> </span><span>centr</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>10</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0</span><span> </span><span>1</span><span> </span><span>2</span><span> </span><span>3</span><span> </span><span>4</span><span> </span><span>5</span><span> </span><span>6</span><span> </span><span>7</span><span> </span><span>8</span><span> </span><span>9</span><span> </span><span>...</span><span>
</span><span>$</span><span> </span><span>AP_image_data</span><span> </span><span>:</span><span> </span><span>num</span><span> </span><span>[</span><span>1</span><span>:</span><span>321</span><span>,</span><span> </span><span>1</span><span>:</span><span>481</span><span>,</span><span> </span><span>1</span><span>:</span><span>3</span><span>]</span><span> </span><span>0.404</span><span> </span><span>0.404</span><span> </span><span>0.404</span><span> </span><span>0.404</span><span> </span><span>0.404</span><span> </span><span>...</span><span>
</span></code></pre>
</div>
</div>
<p></p>
<p>the <em>display_all</em> parameter enables plotting of masks either all at once or sequentially,</p>
<div>
<div>
<pre><code><span>
</span><span>init</span><span>$</span><span>spixel_masks_show</span><span>(</span><span>display_all</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p></p>
<p><img alt="Alt text" src="https://i1.wp.com/mlampros.github.io/images/im_masks.png?w=456" data-recalc-dims="1"></p>

<p><strong>References :</strong></p>
<ul>
<li><a rel="nofollow" href="https://pdfs.semanticscholar.org/6533/654973054b742e725fd433265700c07b48a2.pdf" target="_blank">Image Segmentation using SLIC Superpixels and Affinity Propagation Clustering, Bao Zhou, 2013</a></li>
<li><a rel="nofollow" href="https://en.wikipedia.org/wiki/Image_segmentation" target="_blank">Image segmentation, wikipedia</a></li>
<li><a rel="nofollow" href="https://ivrl.epfl.ch/research-2/research-current/research-superpixels/" target="_blank">SLIC Superpixels</a></li>
<li><a rel="nofollow" href="https://arxiv.org/pdf/1612.01601.pdf" target="_blank">Superpixels: An Evaluation of the State-of-the-Art, David Stutz, Alexander Hermans, Bastian Leibe, 2016</a></li>
<li><a rel="nofollow" href="https://www.psi.toronto.edu/index.php?q=affinity%20propagation" target="_blank">Affinity Propagation</a></li>
<li><a rel="nofollow" href="https://www.psi.toronto.edu/affinitypropagation/FreyDueckScience07.pdf" target="_blank">Clustering by Passing Messages Between Data Points, Brendan J. Frey and Delbert Dueck, University of Toronto, Science 315, 972-976, February 2007</a></li>
<li><a rel="nofollow" href="https://en.wikipedia.org/wiki/CIELAB_color_space" target="_blank">CIELAB colour space</a></li>
<li><a rel="nofollow" href="https://www2.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/resources.html" target="_blank">Berkeley Segmentation Database (BSD)</a></li>
<li><a rel="nofollow" href="https://github.com/mlampros/OpenImageR" target="_blank">OpenImageR package</a></li>
<li><a rel="nofollow" href="https://github.com/mlampros/ClusterR" target="_blank">ClusterR package</a></li>
<li><a rel="nofollow" href="https://github.com/mlampros/SuperpixelImageSegmentation" target="_blank">SuperpixelImageSegmentation package</a></li>
</ul>
<p></p>
<p><strong>Issues :</strong></p>
<p>The updated versions of <em>OpenImageR</em>, <em>ClusterR</em> and <em>SuperpixelImageSegmentation</em> packages can be found in my <a rel="nofollow" href="https://github.com/mlampros/" target="_blank">Github account</a>. To report bugs/issues please use the following links: <a rel="nofollow" href="https://github.com/mlampros/OpenImageR/issues" target="_blank">https://github.com/mlampros/OpenImageR/issues</a>, <a rel="nofollow" href="https://github.com/mlampros/ClusterR/issues" target="_blank">https://github.com/mlampros/ClusterR/issues</a> and <a rel="nofollow" href="https://github.com/mlampros/SuperpixelImageSegmentation/issues" target="_blank">https://github.com/mlampros/SuperpixelImageSegmentation/issues</a>.</p>
<p></p>
<p><strong>Citation :</strong></p>
<p>If you use the R / Rcpp code of the SuperpixelImageSegmentation in your own research I would appreciate it if you add:</p>
<p></p>
<p>Lampros Mouselimis, Image Segmentation using Superpixels, Affinity Propagation and Kmeans Clustering, (2018), GitHub repository, https://github.com/mlampros/SuperpixelImageSegmentation, <a rel="nofollow" href="https://zenodo.org/badge/latestdoi/156761915" target="_blank"><img alt="DOI" src="https://zenodo.org/badge/156761915.svg"></a></p>
<p></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="http://mlampros.github.io/mlampros.github.io/2018/11/09/Image_Segmentation_Superpixels_Clustering/"> mlampros</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/1PG8ivIjoyQ" height="1"></a></div>