<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Elmersly Yassmine on Medium]]></title>
        <description><![CDATA[Stories by Elmersly Yassmine on Medium]]></description>
        <link>https://medium.com/@elmerslyyassmine01?source=rss-552ebfa2d867------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Pg9lfNSpFk-CndNO</url>
            <title>Stories by Elmersly Yassmine on Medium</title>
            <link>https://medium.com/@elmerslyyassmine01?source=rss-552ebfa2d867------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 17:42:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@elmerslyyassmine01/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Image Segmentation using Fuzzy C-Means]]></title>
            <link>https://medium.com/@elmerslyyassmine01/image-segmentation-using-fuzzy-c-means-3572e83be798?source=rss-552ebfa2d867------2</link>
            <guid isPermaLink="false">https://medium.com/p/3572e83be798</guid>
            <dc:creator><![CDATA[Elmersly Yassmine]]></dc:creator>
            <pubDate>Fri, 13 Oct 2023 12:20:40 GMT</pubDate>
            <atom:updated>2023-10-13T12:20:40.111Z</atom:updated>
            <content:encoded><![CDATA[<p>Image segmentation is an image processing operation that aims to collect pixels according to predefined criteria. The pixels are thus grouped into regions, which constitute a paving or partition of the image.</p><p>Segmentation is a process of synthesis aimed at extracting the geometric characteristics of images through the abstraction of color nuances, reflections, transparencies, shadows, textures, and other elements that constitute the incredible richness of visual information.</p><p>Given an image, there are always several possible segmentations. Therefore, a good method of segmentation will be the one that allows for a good interpretation. It must therefore have simplified the image without reducing its content. This includes segmentation based on fuzzy classification.</p><p>Have a cup of coffee and don’t stop until you’re proud!🧐</p><h4>The Fuzzy C-Means algorithm</h4><p>Fuzzy c-means (FCM) is a method of clustering that allows points to be in more than one cluster. It’s also known as “soft K-means” due to its design centered around K-means, which means that fuzzy c-means are a special case of K-means when the probability function used is simply 1 if the data point is closest to a centroid and 0 otherwise.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/919/1*H8W9N1-cRgZWre0-r7eXmw.png" /><figcaption>Image Credits : <a href="https://medium.com/geekculture/fuzzy-c-means-clustering-fcm-algorithm-in-machine-learning-c2e51e586fff">https://medium.com</a></figcaption></figure><p><strong>The process of FCM is:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/619/1*KBFwN4r8xBjhJ6nEZj01ig.png" /><figcaption>Image Credits : <a href="https://www.researchgate.net/figure/Fuzzy-c-means-clustering-algorithm_fig1_326966212/download">https://www.researchgate.net</a></figcaption></figure><h4>Implementing FCM algorithm for image segmentation</h4><p>First, we install all the necessary packages.</p><pre>import numpy as np<br>import cv2<br>from matplotlib import pyplot as plt</pre><pre>class FCM():<br>    def __init__(self, image, image_bit, n_clusters, m, epsilon, max_iter):<br>        #-------------------Check inputs-------------------<br>        if np.ndim(image) != 2:<br>            raise Exception(&quot;&lt;image&gt; needs to be 2D (gray scale image).&quot;)<br>        if n_clusters &lt;= 0 or n_clusters != int(n_clusters):<br>            raise Exception(&quot;&lt;n_clusters&gt; needs to be positive integer.&quot;)<br>        if m &lt; 1:<br>            raise Exception(&quot;&lt;m&gt; needs to be &gt;= 1.&quot;)<br>        if epsilon &lt;= 0:<br>            raise Exception(&quot;&lt;epsilon&gt; needs to be &gt; 0&quot;)<br><br>        self.image = image<br>        self.image_bit = image_bit<br>        self.n_clusters = n_clusters<br>        self.m = m<br>        self.epsilon = epsilon<br>        self.max_iter = max_iter<br><br>        self.shape = image.shape # image shape<br>        self.X = image.flatten().astype(&#39;float&#39;) # flatted image shape: (number of pixels,1) <br>        self.numPixels = image.size<br>       <br>    #--------------------------------------------- <br>    def initial_U(self):<br>        U=np.zeros((self.numPixels, self.n_clusters))<br>        idx = np.arange(self.numPixels)<br>        for ii in range(self.n_clusters):<br>            idxii = idx%self.n_clusters==ii<br>            U[idxii,ii] = 1        <br>        return U<br>    <br>    def update_U(self):<br>        &#39;&#39;&#39;Compute weights&#39;&#39;&#39;<br>        c_mesh,idx_mesh = np.meshgrid(self.C,self.X)<br>        power = 2./(self.m-1)<br>        p1 = abs(idx_mesh-c_mesh)**power<br>        p2 = np.sum((1./abs(idx_mesh-c_mesh))**power,axis=1)<br>        <br>        return 1./(p1*p2[:,None])<br><br>    def update_C(self):<br>        &#39;&#39;&#39;Compute centroid of clusters&#39;&#39;&#39;<br>        numerator = np.dot(self.X,self.U**self.m)<br>        denominator = np.sum(self.U**self.m,axis=0)<br>        return numerator/denominator<br>                       <br>    def form_clusters(self):      <br>        &#39;&#39;&#39;Iterative training&#39;&#39;&#39;        <br>        d = 100<br>        self.U = self.initial_U()<br>        if self.max_iter != -1:<br>            i = 0<br>            while True:             <br>                self.C = self.update_C()<br>                old_u = np.copy(self.U)<br>                self.U = self.update_U()<br>                d = np.sum(abs(self.U - old_u))<br>                print(&quot;Iteration %d : cost = %f&quot; %(i, d))<br><br>                if d &lt; self.epsilon or i &gt; self.max_iter:<br>                    break<br>                i+=1<br>        else:<br>            i = 0<br>            while d &gt; self.epsilon:<br>                self.C = self.update_C()<br>                old_u = np.copy(self.U)<br>                self.U = self.update_U()<br>                d = np.sum(abs(self.U - old_u))<br>                print(&quot;Iteration %d : cost = %f&quot; %(i, d))<br><br>                if d &lt; self.epsilon or i &gt; self.max_iter:<br>                    break<br>                i+=1<br>        self.segmentImage()<br><br><br>    def deFuzzify(self):<br>        return np.argmax(self.U, axis = 1)<br><br>    def segmentImage(self):<br>        &#39;&#39;&#39;Segment image based on max weights&#39;&#39;&#39;<br><br>        result = self.deFuzzify()<br>        self.result = result.reshape(self.shape).astype(&#39;int&#39;)<br><br>        return self.result</pre><pre># Load image<br>from skimage import io, color<br>#image = color.rgb2lab(plt.imread(&#39;img1.png&#39;))<br>image = plt.imread(&#39;mdb303.png&#39;)<br><br># Initialize FCM parameters<br>image_bit = 8  # number of bits per pixel in the image<br>n_clusters = 5  # number of clusters/segments to create<br>m = 2  # fuzziness parameter<br>epsilon = 0.01  # convergence threshold<br>max_iter = 100  # maximum number of iterations</pre><pre>io.imshow(image)</pre><p><em>&lt;matplotlib.image.AxesImage at 0x2003c8c8f48&gt;</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/298/1*4bqnPqHWv_-PoVyG5mIpzg.png" /></figure><pre>fcm = FCM(image, image_bit, n_clusters, m, epsilon, max_iter)</pre><pre>fcm.form_clusters()</pre><pre>Output exceeds the size limit. Open the full output data in a text editor<br>Iteration 0 : cost = 1677721.293920<br>Iteration 1 : cost = 164.642309<br>Iteration 2 : cost = 656.162835<br>Iteration 3 : cost = 2648.368513<br>Iteration 4 : cost = 10780.573006<br>Iteration 5 : cost = 40052.623581<br>Iteration 6 : cost = 150051.623556<br>Iteration 7 : cost = 554012.878454<br>Iteration 8 : cost = 728887.534017<br>Iteration 9 : cost = 226260.551605<br>Iteration 10 : cost = 156287.923325<br>Iteration 11 : cost = 124689.451677<br>Iteration 12 : cost = 92047.388180<br>Iteration 13 : cost = 48877.294465<br>Iteration 14 : cost = 23806.419433<br>Iteration 15 : cost = 13701.683378<br>Iteration 16 : cost = 9085.915623<br>Iteration 17 : cost = 6527.204534<br>Iteration 18 : cost = 4998.791530<br>Iteration 19 : cost = 4031.634534<br>Iteration 20 : cost = 3418.772156<br>Iteration 21 : cost = 3069.846312<br>Iteration 22 : cost = 2752.815746<br>Iteration 23 : cost = 2456.310397<br>Iteration 24 : cost = 2183.920682<br>...<br>Iteration 98 : cost = 0.215776<br>Iteration 99 : cost = 0.190426<br>Iteration 100 : cost = 0.168056<br>Iteration 101 : cost = 0.148316<br><br>&lt;matplotlib.image.AxesImage at 0x2003c8c8f48&gt;</pre><pre>segmented_image = fcm.result</pre><pre>plt.imshow(segmented_image, cmap=&#39;BrBG&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/271/1*qrurrb_hHbf3hNdvVBSGHQ.png" /></figure><p>Thank you for sticking around until the end! ❤️</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3572e83be798" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>