<?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 Edouard TETOUHE KILIMOU on Medium]]></title>
        <description><![CDATA[Stories by Edouard TETOUHE KILIMOU on Medium]]></description>
        <link>https://medium.com/@edouardkilimou1?source=rss-a5878b32d7ef------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*t-nI5YYsZUVgwaau</url>
            <title>Stories by Edouard TETOUHE KILIMOU on Medium</title>
            <link>https://medium.com/@edouardkilimou1?source=rss-a5878b32d7ef------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 23:51:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@edouardkilimou1/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 Processing:  Segmentation and Objects Counting with Python and Opencv]]></title>
            <link>https://medium.com/analytics-vidhya/images-processing-segmentation-and-objects-counting-in-an-image-with-python-and-opencv-216cd38aca8e?source=rss-a5878b32d7ef------2</link>
            <guid isPermaLink="false">https://medium.com/p/216cd38aca8e</guid>
            <category><![CDATA[segmentation]]></category>
            <category><![CDATA[opencv]]></category>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[opencv-python]]></category>
            <dc:creator><![CDATA[Edouard TETOUHE KILIMOU]]></dc:creator>
            <pubDate>Sat, 19 Oct 2019 16:09:44 GMT</pubDate>
            <atom:updated>2023-03-10T08:07:47.446Z</atom:updated>
            <content:encoded><![CDATA[<h3>Images Processing: Segmentation and Objects Counting with Python and OpenCV</h3><p>Images segmentation is an important step of objects recognizing in computer vision domain. In this article we present some techniques of segmentation. Segmentation ingenuity is based on the choice of techniques among multiple techniques available according to images that we must process. Here we will not go to present all segmentation techniques, we will talk only about techniques that we use in this work. It is important to note that, these techniques are not the only ones that must be used in this case, so you can find some that could efficiently resolve this problem.</p><h3>Goal</h3><p>The image below, represents the summary what we are looking to perform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*S0YKPVa0Csc63upEuHayRQ.png" /><figcaption><strong>Fig 1: Original image (left) and image segmented (right)</strong></figcaption></figure><h4><strong>Tools and resources (images) used</strong></h4><p>Python 3.5, opencv 4.1.0</p><p>Images used are located at <a href="https://photos.app.goo.gl/CGgNoSyvENRzvLmZ8">https://photos.app.goo.gl/CGgNoSyvENRzvLmZ8</a></p><h4><strong>Processing sequence</strong></h4><p>Segmentation process can be parted into 3 parts:</p><p>1 — Pre-processing,</p><p>2 — Processing, and</p><p>3 — Post-segmentation.</p><p><strong>1 — Pre-processing</strong></p><p>We have 4 images to be segmented by the same processing sequence, so we must analyze all these input images characteristics (brightness, contrast, noise, ..etc ) in order to choose techniques that correspond to all. All these treatments that improving the images quality belong to pre-processing step. Let’s take a look on our four input images bellow</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*TlmsDkhX-762jmBby4ughw.jpeg" /><figcaption><strong>Fig 2: First Image</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*hfdLBP_MqTr8WXKiMgTWRw.jpeg" /><figcaption><strong>Fig 3: Second Image</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*KYy5epJKweSZ0Sm6hi4k1w.jpeg" /><figcaption><strong>Fig 4: Third Image</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*vVcZhL61ppxcOvZAcFb7-A.jpeg" /><figcaption><strong>Fig 5: Fourth Image</strong></figcaption></figure><p>First of all, we will convert our colored images to grayscale image, in order to have only one channel image. Even without converting, we can observe difference between these images characteristics. The first image (Fig 2) contains small objects, and some have the same pixels values with the background (same for fifth image Fig 5). This aspect can cause the egdes detecting problem. So to prevent this, we think about image contrast adjustment, so we have to choose among all contrast improving methods (histogram equlization, linear transformation, non-linear transformation ex of gamma correction , Piecewise-linear transformation …etc ), let’s us compare results of two methods implemented in opencv: Gamma correction and Histogram equalization.</p><pre># Convert image in grayscale</pre><pre>gray_im = cv.cvtColor(original, cv.COLOR_BGR2GRAY)<br>plt.subplot(221)<br>plt.title(&#39;Grayscale image&#39;)<br>plt.imshow(gray_im, cmap=&quot;gray&quot;, vmin=0, vmax=255)<br><br># Contrast adjusting with gamma correction y = 1.2<br><br>gray_correct = np.array(255 * (gray_im / 255) ** 1.2 , dtype=&#39;uint8&#39;)<br>plt.subplot(222)<br>plt.title(&#39;Gamma Correction y= 1.2&#39;)<br>plt.imshow(gray_correct, cmap=&quot;gray&quot;, vmin=0, vmax=255)</pre><pre># Contrast adjusting with histogramm equalization</pre><pre>gray_equ = cv.equalizeHist(gray_im)<br>plt.subplot(223)<br>plt.title(&#39;Histogram equilization&#39;)<br>plt.imshow(gray_correct, cmap=&quot;gray&quot;, vmin=0, vmax=255)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*akAlkomfk_fwYpcNAWTIBQ.png" /><figcaption><strong>Fig 6: Contrast methods comparison</strong></figcaption></figure><p>Which one do you think we must choose? According to us, we think Gamma correction gives better result, so we keep this. In our case, at this step, only this treatment can resolve our clearness problem. But note that in some cases you must do multiple processing before resolve clearness problem, so let us go to the next step.</p><p><strong>2 — Processing</strong></p><p>As for previous treatment, here also, we have different segmentation techniques that can be grouped into two parts: edges based techniques and region based techniques.</p><p>Otherwise, we can use threshold techniques to do a segmentation, in this case we must follow other processing techniques in order to get satisfactory result.</p><p>Let us experimented this last approach.</p><p>Here we used adaptive local threshold technique to binarize our grayscale image (contrast corrected grayscale image), note that you can use other technique that you think better for your image. The result looks like bellow.</p><pre># Local adaptative threshold<br><br>thresh = cv.adaptiveThreshold(gray_correct, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 255, 19)<br>thresh = cv.bitwise_not(thresh)plt.subplot(221)<br>plt.title(&#39;Local adapatative Threshold&#39;)<br>plt.imshow(thresh, cmap=&quot;gray&quot;, vmin=0, vmax=255)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*dywqluIwVgmvd1vt7TuHZw.png" /><figcaption>Fig 7: Local Adaptative threshold</figcaption></figure><p>As you can see, on this previous figure (fig 7), after the thresholding, we have many connected regions, this can not help us to count objects in the image cause the counting of objects is due to number of connected regions, so in addition, we have to apply some other techniques before count number of objects in the image.</p><p>We will go to follow erosion and dilatation techniques which will help us to connected nearest regions in order to have one region per object.</p><pre># Dilatation et erosion<br>kernel = np.ones((15,15), np.uint8)<br>img_dilation = cv.dilate(thresh, kernel, iterations=1)<br>img_erode = cv.erode(img_dilation,kernel, iterations=1)</pre><pre># clean all noise after dilatation and erosion</pre><pre>img_erode = cv.medianBlur(img_erode, 7)</pre><pre>plt.subplot(221)<br>plt.title(&#39;Dilatation + erosion&#39;)<br>plt.imshow(img_erode, cmap=&quot;gray&quot;, vmin=0, vmax=255)</pre><p>Let us apply these techniques and appreciate the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*io4iWgZq7_U1_6qj5gmGnA.png" /><figcaption>Fig 8: Erosion and Dilatation</figcaption></figure><p>Now we have one object per connected region, so we can count number of objects in the image. But before do that, let us label connected regions before.</p><pre># Labeling<br><br>ret, labels = cv.connectedComponents(img_erode)<br>label_hue = np.uint8(179 * labels / np.max(labels))<br>blank_ch = 255 * np.ones_like(label_hue)<br>labeled_img = cv.merge([label_hue, blank_ch, blank_ch])<br>labeled_img = cv.cvtColor(labeled_img, cv.COLOR_HSV2BGR)<br>labeled_img[label_hue == 0] = 0<br><br>plt.subplot(222)<br>plt.title(&#39;Objects counted:&#39;+ str(ret-1))<br>plt.imshow(labeled_img)</pre><pre>print(&#39;objects number is:&#39;, ret-1)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*7TSFXBPQSC7s50yGflcj1g.png" /><figcaption>Fig 9: Labeling and objects counting</figcaption></figure><p>In the next article, we will present other segmentation techniques before present you the post-segmentation step. Hope you find the article helpful and please share your thoughtsand feedback in the comment section below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=216cd38aca8e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/images-processing-segmentation-and-objects-counting-in-an-image-with-python-and-opencv-216cd38aca8e">Image Processing:  Segmentation and Objects Counting with Python and Opencv</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>