<?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 Aids on Medium]]></title>
        <description><![CDATA[Stories by Aids on Medium]]></description>
        <link>https://medium.com/@perez-aids?source=rss-dbb4fa1fc346------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GGEWNGehPjvX5_s4yWj8vQ.jpeg</url>
            <title>Stories by Aids on Medium</title>
            <link>https://medium.com/@perez-aids?source=rss-dbb4fa1fc346------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 13:19:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@perez-aids/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[Introduction to Image Processing — Part 10: Sudoku Solver]]></title>
            <link>https://medium.com/analytics-vidhya/introduction-to-image-processing-part-10-sudoku-solver-4530f6c85d6c?source=rss-dbb4fa1fc346------2</link>
            <guid isPermaLink="false">https://medium.com/p/4530f6c85d6c</guid>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[sudoku-solver]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Aids]]></dc:creator>
            <pubDate>Wed, 03 Feb 2021 01:46:54 GMT</pubDate>
            <atom:updated>2021-02-05T16:49:31.237Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction to Image Processing — Part 10: Sudoku Solver</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aCGhBmY9EgIKspE4j28-tQ.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@black-ice-551383?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Black ice</strong></a> from <a href="https://www.pexels.com/photo/lots-of-numbers-1314543/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Pexels</strong></a></figcaption></figure><p>Let’s try to use again the image processing techniques that we have discussed in a more practical and innovative way. This time, we will use image processing to read Sudoku from an image and enter the output into a Sudoku solver algorithm. To do this, it will require us to do three main tasks:</p><p>1. Image Processing</p><p>2. Digit Recognition</p><p>3. Solving the Sudoku.</p><h3><strong>Image Processing</strong></h3><p>Let’s start with a simple photo of a Sudoku illustrated in Figure 1. The first step is to detect and extract the digits. We do this by the usual image binarization and using connected components (i.e., regionprops) to detect the digits.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/321/1*epQTGKGzLLJBxqVbsx58uA.png" /><figcaption>Figure 1. Sudoku image</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*9z9H-O91KI4NakckUma2Eg.png" /><figcaption>Figure 2. Detected digits</figcaption></figure><p>Using the <em>centroid</em> property of the region props, we can map out the location of the digits from the Sudoku grid</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3af44bc400156a86479a764e9397cb3/href">https://medium.com/media/c3af44bc400156a86479a764e9397cb3/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/683/1*oQSMLLH1UGcfMF75ykq67Q.png" /><figcaption>Figure 3. Sample regionprops table and derived features (i.e., x_coor, y_coor)</figcaption></figure><p>With the <em>bbox</em> property, we can extract the digits from the source image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/1*Qwpz_V5-rq6PQ6qVvu1-FA.png" /><figcaption>Figure 4. Extracted digits</figcaption></figure><h3><strong>Digit Recognition</strong></h3><p>We can train a machine learning model to classify the extracted digits to their correct numeric values. We won’t discuss the full details of the model training, but it was trained using the MNIST dataset, and Figure 5 shows the summary of the Convolutional Neural Network (CNN) used. The model has achieved an accuracy of 99%.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*WtxyMdlXD34gp20oToPlCA.png" /><figcaption>Figure 5. Summary of CNN architecture</figcaption></figure><p>After the digits are correctly recognized, we can then reconstruct the Sudoku image into a Numpy array form, which will be our input to our Sudoku solver. The mapping of each digit was based on the derived features <em>x_coor </em>and<em> y_coor.</em> Missing coordinates in a 9x9 array were assigned 0 values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/303/1*acfl-N0J-6bfr1xn3F38UA.png" /><figcaption>Figure 6. Reconstructed Sudoku array</figcaption></figure><p><strong>Solving the Sudoku</strong></p><p>Surprisingly, Python already has a <a href="https://pypi.org/project/py-sudoku/">library </a>for solving Sudoku puzzles, so we don’t need to create the algorithm itself. We simply need to input the reconstructed Sudoku, and it will return the completed Sudoku puzzle.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d15c3f30cffd3a1d44eff6aea08f199/href">https://medium.com/media/6d15c3f30cffd3a1d44eff6aea08f199/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*xo7b4ixnN24LOrUNbsITpA.png" /><figcaption>Figure 7. Solved Sudoku</figcaption></figure><p>And there! That ‘s how we can use image processing in trying to solve a Sudoku from a Sudoku image automatically.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4530f6c85d6c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/introduction-to-image-processing-part-10-sudoku-solver-4530f6c85d6c">Introduction to Image Processing — Part 10: Sudoku Solver</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>
        <item>
            <title><![CDATA[Introduction to Image Processing — Part 9: Leaf Classification with Machine Learning]]></title>
            <link>https://medium.com/swlh/introduction-to-image-processing-part-9-leaf-classification-with-machine-learning-7ad4babbada2?source=rss-dbb4fa1fc346------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ad4babbada2</guid>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Aids]]></dc:creator>
            <pubDate>Wed, 03 Feb 2021 01:46:06 GMT</pubDate>
            <atom:updated>2021-02-05T14:24:32.410Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction to Image Processing — Part 9: Leaf Classification with Machine Learning</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DRK9w0tnDBEGW7xZZlrk2g.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@kadri-vosumae-249986?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Kadri Vosumae</strong></a> from <a href="https://www.pexels.com/photo/yellow-dry-maple-756903/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Pexels</strong></a></figcaption></figure><p>Now that we are done tackling different image processing techniques, it’s time for us to put them to more practical use. In this post, we will apply the image processing methods we have discussed to prepare our input images and train a classifier model using the <em>leaves</em> dataset.</p><h3><strong>Dataset</strong></h3><p>The data set that we will use composed of 27 photos of different leaves in grayscale format. These leaves can be classified into five, which are labeled from A to E.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1005/1*6_PQpBZ5WytNESyHYWVCjQ.png" /><figcaption>Figure 1. Leaves dataset</figcaption></figure><h3><strong>Data Cleaning</strong></h3><p>Each photo was cleaned using different image segmentation techniques and morphological operations. Connected components were then used to detect the leaves from each photo. Figure 2 shows the detected leaves as indicated by the red bounding box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*nxKWUiolIjhoaAwWSSkoXg.png" /><figcaption>Figure 2. Cleaned images with object detection</figcaption></figure><h3><strong>Machine Learning</strong></h3><p>For the baseline, we will use the 1.25*PCC (Proportional Chance Criterion), which is computed to be 25.06%, as shown in Figure 3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*AafpiEBTWSN6C9JA4rdnMw.png" /><figcaption>Figure 3. 1.25*PCC baseline</figcaption></figure><p>As discussed in the Object Detection post, <em>regionprops </em>gives us several object properties that we can use as input in our machine learning (ML) algorithm. For this task, we will use the properties <em>eccentricity, area, perimeter, extent, </em>and <em>convex_area</em>. Other properties are listed in this<a href="https://github.com/scikit-image/scikit-image/blob/v0.18.0/skimage/measure/_regionprops.py#L705-L864"> GitHub repository</a>.</p><p>Several ML algorithms were trained using the processed images. The actual implementation of these models will not be discussed in this post, but the summary of test accuracies is shown in Table 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*TaJNXzA6UWlSMlAlzUDn9w.png" /><figcaption>Table 1. Model accuracies</figcaption></figure><p>Out of the eight models trained, <em>Random Forest </em>gave the best accuracy of about 94%, which is 72% higher than our baseline, 25%.</p><p>Even though we already have good classification accuracy, this could still be improved using more advanced ML algorithms such as Neural Networks and by exploring other regionprops properties as input features.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ad4babbada2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/introduction-to-image-processing-part-9-leaf-classification-with-machine-learning-7ad4babbada2">Introduction to Image Processing — Part 9: Leaf Classification with Machine Learning</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Image Processing — Part 8: Template Matching]]></title>
            <link>https://perez-aids.medium.com/introduction-to-image-processing-part-8-template-matching-7b901a3aee31?source=rss-dbb4fa1fc346------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b901a3aee31</guid>
            <category><![CDATA[template-matching]]></category>
            <category><![CDATA[image-processing]]></category>
            <dc:creator><![CDATA[Aids]]></dc:creator>
            <pubDate>Wed, 03 Feb 2021 01:45:36 GMT</pubDate>
            <atom:updated>2021-02-03T01:45:36.929Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction to Image Processing — Part 8: Template Matching</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KBOhRcmd_U7zSB9OZYwmzw.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@cottonbro?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>cottonbro</strong></a> from <a href="https://www.pexels.com/photo/white-printer-paper-on-brown-wooden-table-4614118/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Pexels</strong></a></figcaption></figure><p>In part 4 of this series, we have discussed two objection detection methods: <em>Blob detection</em> and <em>Connected components</em>. These methods detect all the objects in our image based on the evident highlighted blobs or pixels after some initial preprocessing methods like image binarization. In this part, We will discuss another object detection method, which is <em>Template Matching.</em></p><h3><strong>Template Matching</strong></h3><p>This method uses a reference image known as template and slides it across the entire source image one pixel at a time to determine the most similar objects. It will produce another image or matrix where pixel values correspond to how similar our template is to the source image. Thus, when we try to view the output image, the pixel values of the matched objects will be peaking or will be highlighted.</p><p>Let’s take, for example, the source image and selected template illustrated in Figure 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*7rUJuVvrdFq4Kdyp9D58tw.png" /><figcaption>Figure 1. Source image and template</figcaption></figure><p>The source image is in grayscale for simplicity, but the method could also work in colored images. Let’s now try to detect other airplanes in the image that would match our template.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7a6501f0ec81fe8753970e96676d5ca/href">https://medium.com/media/a7a6501f0ec81fe8753970e96676d5ca/href</a></iframe><p>As mentioned previously, the resulting image or matrix will highlight the objects that match our template. This is evident in Figure 2, where the lighter spots or pixels are those that are most similar to our template. To better see the matched objects, let’s put a bounding box around them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*01I0ctzrzsyXssVxOAMNew.png" /><figcaption>Figure 2. Resulting image of template matching</figcaption></figure><p>As mentioned previously, the resulting image or matrix will highlight the objects that match our template. This is evident in Figure 2, where the lighter spots or pixels are those that are most similar to our template. To better see the matched objects, let’s put a bounding box around them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7287e8998866920a1403686442e3b837/href">https://medium.com/media/7287e8998866920a1403686442e3b837/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uuUDWIy9QHYGcE9CGTeSsw.png" /><figcaption>Figure 3. Matched objects based on thresholds</figcaption></figure><p>Because the pixel values of the resulting image correspond to their correlation with our template, the number of matched objects will depend on the threshold that we pick. The lower the threshold, the more objects it will consider a match.</p><p>The advantage of using template matching instead of the general object detection methods previously discussed is that we can specifically choose a particular object or template from the source image and let the algorithm find similar items. However, there are three main drawbacks to this method. Template matching using cross-correlation is NOT scale-invariant, NOT rotational-invariant, and NOT intensity-invariant. This means that changes in the shape, size, orientation, and color of the template or target objects will affect its capacity to detect the desired objects. SIFT (Scale-Invariant Feature Transform), a more advanced template matching algorithm, is available in OpenCV, but it is a paid feature.</p><h3><strong>References</strong></h3><p>[1] B. Borja. “Lecture 9 — Special Topics in Image Processing Part 2 (Supplementary Notebook)”. IIP 2020.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b901a3aee31" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Image Processing — Part 7: Homography]]></title>
            <link>https://perez-aids.medium.com/introduction-to-image-processing-part-7-homography-4af34ce9f93d?source=rss-dbb4fa1fc346------2</link>
            <guid isPermaLink="false">https://medium.com/p/4af34ce9f93d</guid>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[homography]]></category>
            <dc:creator><![CDATA[Aids]]></dc:creator>
            <pubDate>Wed, 03 Feb 2021 01:44:11 GMT</pubDate>
            <atom:updated>2021-02-03T01:44:11.491Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction to Image Processing — Part 7: Homography</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*coBZo7Lrjxn0kPd5nugyvQ.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@cottonbro?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>cottonbro</strong></a> from <a href="https://www.pexels.com/photo/man-holding-black-dslr-camera-3584932/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Pexels</strong></a></figcaption></figure><p>In photography and film, a photo or video can be shot at different angles. It could be high, low, straight or over the shoulder, or bird’s eye view, depending on what the person wants to achieve. Because of this, we might encounter scenarios when we need to transform these images into a particular form as part of image correction, enhancement, or other data preprocessing tasks. So in this post, we will discuss how we can manipulate an image using <em>homography.</em></p><h3><strong>Homography Matrix</strong></h3><p><strong>Homography </strong>is the mapping between two planar projections of an image. Homography on a 2D Euclidean space is represented by a 3x3 transformation matrix, and these transformations can be used to do perspective correction, projection manipulation, image rotation, scaling, and image stitching. [1,2] Figure 1 illustrates how a simple plane can be transformed using homography.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*dKGN5N1AOzZP1g4AusWrSw.png" /><figcaption>Figure 1. Homography transformations</figcaption></figure><p>Let’s now try to apply it in an actual image. Suppose we want to have a top-down view of a particular image. This would fall under the projective transformation, and the <a href="https://scikit-image.org/docs/stable/auto_examples/transform/plot_transform_types.html"><em>scikit-image</em> </a>library actually has modules to perform this and other types of transformations. However, for projective transformations, the source and destination coordinates are part of the parameters that we need to feed into the function.</p><p>Figure 2 shows the image that we wanted to transform and the estimated coordinates of the corners of the court, which will serve as our source.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aee23a304966bcbf7b2780037380cdc6/href">https://medium.com/media/aee23a304966bcbf7b2780037380cdc6/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*c5Rubz--kd4RTj47xQCu0w.png" /><figcaption>Figure 2. Image of volleyball court with plotted coordinates.</figcaption></figure><p>The following codes below will perform the projective transformation for our image. For demonstration purposes, the destination or target coordinates used are highly estimated and does not consider the actual court size ratio.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab8f4295841a6ee1b0e97c501c43b7a1/href">https://medium.com/media/ab8f4295841a6ee1b0e97c501c43b7a1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*aEeAaYVFginiSbuwQSoxBw.png" /><figcaption>Figure 3. Transformed image</figcaption></figure><p>From this top-down view image of the volleyball court, we can then do further image processing such as object detection and player position marking. This could be done for each frame when the ball is on a team’s side, and it could be useful in analyzing the team’s defense pattern and gameplay tactics.</p><p>We could perform other homography transformations using the scikit-image library, but it won’t be covered in this post.</p><h3><strong>References</strong></h3><p>[1] B. Borja. “Lecture 7 — Special Topics in Image Processing”. IIP 2020.</p><p>[2] “scikit-image 0.19.0.dev0 docs,” scikit-image, [Online]. Available: <a href="https://scikit-image.org/docs/dev/">https://scikit-image.org/docs/dev/.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4af34ce9f93d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Image Processing — Part 6: Image Segmentation 2]]></title>
            <link>https://medium.com/swlh/introduction-to-image-processing-part-6-image-segmentation-2-3099c7bca29b?source=rss-dbb4fa1fc346------2</link>
            <guid isPermaLink="false">https://medium.com/p/3099c7bca29b</guid>
            <category><![CDATA[rg-chromaticity]]></category>
            <category><![CDATA[image-difference]]></category>
            <category><![CDATA[image-segmentation]]></category>
            <category><![CDATA[image-processing]]></category>
            <dc:creator><![CDATA[Aids]]></dc:creator>
            <pubDate>Mon, 01 Feb 2021 13:04:42 GMT</pubDate>
            <atom:updated>2021-02-05T14:23:58.711Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction to Image Processing — Part 6: Image Segmentation 2</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1UHZAgitB4CYniTbtx3zWg.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@markusspiske?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Markus Spiske</strong></a> from <a href="https://www.pexels.com/photo/one-black-chess-piece-separated-from-red-pawn-chess-pieces-1679618/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Pexels</strong></a></figcaption></figure><p>In the previous post, We discussed how to segment objects in our image using thresholding, Otsu’s method, and color segmentation. These segmentation techniques highly rely on the threshold values we set or are automatically set in Otsu’s method. However, there may be times when we need to segment the image using colors that are not that obvious (e.g., skin color), or we simply cannot get the right threshold and color channels or spaces to use.</p><p>This part of the series will discuss other segmentation methods: <em>Chromaticity Segmentation </em>and <em>Image Differencing</em>.</p><h3><strong>Chromaticity Segmentation</strong></h3><p>This segmentation utilized the RG Chromaticity space, a color representation that disregards the colors’ intensity value. Figure 1 shows the formula for computing the RG Chromaticity of a pixel and a 2D representation of the RG color space.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/823/1*ppB3gbSvR8M4E8w7xX_Juw.png" /><figcaption>Figure 1. RG Chromaticity equation and color space</figcaption></figure><p>If you notice, we are only using or plotting the R (red) and G (green) colors, as the name suggests. This is primarily because we can simply compute the B (blue) value since RG Chromaticity’s formula is always equal to 1.</p><p>For this segmentation, we will only be using the parametric type of this method. First, we need to identify a reference patch of the object that we want to segment, then take the RG Chromaticity of it. Let’s take, for example, the image and reference patch in Figure 2. Suppose we want to segment only the green bean bag from the image.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70b02501f35b5bcb1f024e737bfd2b8c/href">https://medium.com/media/70b02501f35b5bcb1f024e737bfd2b8c/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5SFaeiOQWfWT2Zj66cFr3Q.png" /><figcaption>Figure 2. Source image and reference patch</figcaption></figure><p>Now let’s compute for the RG Chromaticity value of the reference patch, then let’s see how it looks like in the RG color space and histogram.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb50158b14d3abc9df380f8d73b3d453/href">https://medium.com/media/fb50158b14d3abc9df380f8d73b3d453/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*21LBpyA93MTGuBT0pQqfYw.png" /><figcaption>Figure 3. Color space and Histogram of RG Chromaticity</figcaption></figure><p>Since we are going to use <strong>parametric segmentation, </strong>we need to fit a Gaussian probability distribution using this mask. This distribution will determine the pixels that belong to our object of interest (i.e., green bean bag). [1] Let us now prepare the parameters needed (i.e., mean and standard deviation) and our Gaussian function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96a0e6ccc413518ee077cd54f84503b9/href">https://medium.com/media/96a0e6ccc413518ee077cd54f84503b9/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/805/1*Gqvcqj4m4qfj0IABBe_cmA.png" /><figcaption>Figure 4. Gaussian distribution of R and G reference patch</figcaption></figure><p>Using this distribution, we now create a mask for each R and G Chromaticity and use it to segment our object of interest.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18fc2d8af2e1da0ef7658f3ca91b5ca3/href">https://medium.com/media/18fc2d8af2e1da0ef7658f3ca91b5ca3/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*EDFY8MS2Gp2yIRlc7J1xxw.png" /><figcaption>Figure 5. Result of segmentation using the R and G masks</figcaption></figure><p>As shown in Figure 5, we segmented our object of interest by using both the R and G masks derived from our Gaussian distribution function. We can then fully segment our object by specifying an adequate threshold.</p><h3><strong>Image Differencing</strong></h3><p>Suppose you are given two similar pictures, as shown in Figure 6. Can you spot the difference between the two? I bet you can, but it will take some time before you can identify them all. Given our image processing knowledge, we can lighten the load of manually identifying the difference by literally getting the difference of these two photos.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*QKoLaGIPSKzy_F9i_pBfCw.png" /><figcaption>Figure 6. Similar photos</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89645a8acdf1c46a9918fffebfec4823/href">https://medium.com/media/89645a8acdf1c46a9918fffebfec4823/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/363/1*A4WscCB1YD_OnPTj0SgD6A.png" /><figcaption>Figure 7. The difference between the two similar photos</figcaption></figure><p>By subtracting the grayscaled format of the two photos, the result will highlight the difference between the two photos, and now we can quickly identify them.</p><p>Image differencing is used in particular tasks such as motion detection, speed detection, intruder alarm, or any functions that involve monitoring changes in the state of an image or video.</p><h3><strong>References</strong></h3><p>[1] B. Borja. “Lecture 6 — Image Segmentation Part 2”. IIP 2020</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3099c7bca29b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/introduction-to-image-processing-part-6-image-segmentation-2-3099c7bca29b">Introduction to Image Processing — Part 6: Image Segmentation 2</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Image Processing — Part 5: Image Segmentation 1]]></title>
            <link>https://medium.com/swlh/introduction-to-image-processing-part-5-image-segmentation-1-99f93d9f7a5e?source=rss-dbb4fa1fc346------2</link>
            <guid isPermaLink="false">https://medium.com/p/99f93d9f7a5e</guid>
            <category><![CDATA[image-segmentation]]></category>
            <category><![CDATA[color-segmentation]]></category>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[otsus-method]]></category>
            <dc:creator><![CDATA[Aids]]></dc:creator>
            <pubDate>Mon, 01 Feb 2021 13:01:52 GMT</pubDate>
            <atom:updated>2021-02-07T07:48:06.018Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction to Image Processing — Part 5: Image Segmentation 1</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UsO-akXM9jycx5kXUaDUwg.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@frostroomhead?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Rodion Kutsaiev</strong></a> from <a href="https://www.pexels.com/photo/photo-of-raspberries-and-blackberry-on-pink-background-3429783/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Pexels</strong></a></figcaption></figure><p>In the previous post, we have discussed how we can detect all objects in an image. However, it is not always the case that we would like to extract all the objects in an image. Sometimes, we only want to get particular objects of interest. For the first part of this post, we will discuss three methods: <em>Thresholding, Otsu’s Method, </em>and <em>Color Segmentation.</em></p><h3><strong>Thresholding</strong></h3><p>This method is a trial-and-error method to find the adequate threshold value to segment an image from the whole photo. [1] We have already done this in the previous post, where we binarize the image by selecting the right threshold value and use it on a grayscale image.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a7af1c87f1743da9a6ab2e1fbdb239c/href">https://medium.com/media/6a7af1c87f1743da9a6ab2e1fbdb239c/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*6WrKmaZ6IBDhudyszk7hEw.png" /><figcaption>Figure 1. Result of Thresholding method</figcaption></figure><p><strong>Otsu’s Method</strong></p><p>In the thresholding method, we manually set the threshold value to 0.46. Otsu’s method gives us a way to automatically find the adequate threshold value to segment our object of interest. This method assumes that the image is composed of a background and a foreground and works by minimizing or maximizing the intra-class variance. [1]</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2584ddf863dca932b6c88e2896cfadf1/href">https://medium.com/media/2584ddf863dca932b6c88e2896cfadf1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*eLXyhynagRBjmB21IbbfMg.png" /><figcaption>Figure 2. Result of Otsu’s method</figcaption></figure><p>The threshold value output of the function is ~0.5209, and this is what we used to binarize our image. It can be observed that the one we created using the thresholding method has less noise. The main advantage of using Otsu’s method is that we don’t have to do trial-and-error to find the value, and we can clean the few noises using some of the methods discussed in the previous posts.</p><h4><strong>Color Segmentation</strong></h4><p>The methods above will work if we only have one object or if we are going to get all the objects in the image. But what if we only want to get all the strawberries in a picture of mixed fruits, as illustrated in the image below. Thresholding and Otsu’s methods won’t simply work in this scenario.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/471/1*lEhRNW45XoR-VCw9HxxCqg.png" /><figcaption>Figure 3. Fruits image</figcaption></figure><p>To solve this problem, we will be utilizing the image’s color properties. In this image, however, looking at the RGB channel would not suffice because other fruits are “red-ish” in color like the apples. With this, it would be more suitable to use the HSV color space.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/927d7a059e6667b4fe47a3e2a12f6dc9/href">https://medium.com/media/927d7a059e6667b4fe47a3e2a12f6dc9/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*ljGicflKKheAJtt98_qHow.png" /><figcaption>Figure 4. HSV space of the Fruits image</figcaption></figure><p>From the HSV color space values, we select which ones we could use to create the mask that would segment our object on interest using specified threshold values. For this example, we would be utilizing the hue and saturation color spaces.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f513921999e1dccaf7bfb8af3fd7e6b4/href">https://medium.com/media/f513921999e1dccaf7bfb8af3fd7e6b4/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/983/1*WNFwXv--sl2LKz5K456E8w.png" /><figcaption>Figure 5. Result of Color segmentation</figcaption></figure><p><em>Voila! </em>We have successfully segmented all the strawberries from the image. You can repeat the steps to segment other fruits. The main challenge here would be determining the right color channel or space and threshold values to use because they would be different for every fruit.</p><p>We have discussed in this post three methods of image segmentation. We will explore other methods in the next part.</p><h3><strong>References</strong></h3><p>[1] B. Borja. “Lecture 5 – Image Segmentation Part 1”. IIP 2020.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99f93d9f7a5e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/introduction-to-image-processing-part-5-image-segmentation-1-99f93d9f7a5e">Introduction to Image Processing — Part 5: Image Segmentation 1</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Image Processing — Part 4: Object Detection]]></title>
            <link>https://medium.com/swlh/introduction-to-image-processing-part-4-object-detection-619e2558d1f2?source=rss-dbb4fa1fc346------2</link>
            <guid isPermaLink="false">https://medium.com/p/619e2558d1f2</guid>
            <category><![CDATA[blob-detection]]></category>
            <category><![CDATA[object-detection]]></category>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[connected-components]]></category>
            <dc:creator><![CDATA[Aids]]></dc:creator>
            <pubDate>Mon, 01 Feb 2021 13:00:52 GMT</pubDate>
            <atom:updated>2021-02-05T14:38:50.177Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction to Image Processing — Part 4: Object Detection</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z3gvIxbXUkuTaaHkYM5h9A.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@seurafrancis99?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Francis Seura</strong></a> from <a href="https://www.pexels.com/photo/man-looking-in-binoculars-during-sunset-802412/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Pexels</strong></a></figcaption></figure><p>In the previous posts, we focused on enhancing and cleaning our images, like removing some noise and other objects or artifacts in preparation for the next image processing tasks. Now in this post, let’s try to do the more exciting stuff starting with object detection and labeling. We are going to use two main methods: <em>Blob detection </em>and <em>Connected components</em>.</p><p>For this post, let’s try to segment and count the number of candies we see in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/419/1*gHz64sRQMvygx5FhXId1XA.png" /><figcaption>Figure 1. Picture of candies</figcaption></figure><h3><strong>Blob Detection</strong></h3><p>Blob detection tries to detect objects of interest or blobs in the image by identifying bright objects in a dark background or vice versa particularly, circular objects. There three ways in which we can perform this: [1]</p><ol><li>Laplacian of Gaussian (LoG) — takes the laplacian of a gaussian smoothed image</li><li>Difference of Gaussian (DoG) — takes the difference of two gaussian smoothed image</li><li>Determinant of Hessian (DoH) — takes maximum in the matrix of the determinant of Hessian</li></ol><p>The first step we are going to do is to binarized our image.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/463679275f213f3a66493c7017563b53/href">https://medium.com/media/463679275f213f3a66493c7017563b53/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*YAgvoRylWCih60rAQ-5rQw.png" /><figcaption>Figure 2. Binarized image</figcaption></figure><p>We now have a binarized image of our candies. However, if you notice, our binarized image has many disconnected and overlapping components that we need to fix first using morphological operations.</p><p>Aside from the ones we have discussed in the previous part, we can use for this task another morphological operation <em>area_closing, </em>which removes dark structures from the image and connects our objects, coupled with <em>median_filter </em>to remove noise given a defined threshold. [2]</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b350f6efc89faf81a3736f95f4966e1/href">https://medium.com/media/0b350f6efc89faf81a3736f95f4966e1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*hlj-VNixf2XcjNTvXg9B_g.png" /><figcaption>Figure 3. Enhanced Binarized Image</figcaption></figure><p>As shown in the figure above, our binarized image is now smoother, cleaner and each object is much more recognizable. We can then do our blob detection.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d970ca39c646892d885a662437057d10/href">https://medium.com/media/d970ca39c646892d885a662437057d10/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/997/1*sZOnWrdqdUW-guaWS589wg.png" /><figcaption>Figure 4. Results of blob detection methods</figcaption></figure><p>We can play around with different parameter values to get better results. But wait! If I manually count the candies in the original image, I only arrive at 21. How come we are getting hundreds? The closest to the actual count is the <em>Difference of Gaussian</em>, which detected 25 objects; however, you can notice that several objects were detected in one single blob.</p><p>This is probably because our objects of interest are not circular, and the methods try to capture every blob that is circular or almost circular in shape. Maybe the other method that we are going to discuss would be better for this task.</p><h3>Connected Components</h3><p>Detecting object of interest using connected components or pixels helps us detect irregularly shaped objects with varying sizes [1]. Using the same cleaned binary image, we now use <em>label</em> and <em>regionprops</em> to label and get the region properties of each object detected. Some of the properties of regionprops are:</p><ol><li>area</li><li>perimeter</li><li>bbox — bounding box dimensions</li><li>bbox_area — area of the bounding box</li><li>centroid — coordinate of the centroid</li><li>convex_image — convex hull of the blob</li><li>convex_area — an area of the convex hull</li><li>eccentricity — measure how it fits into an ellipse (0) for circles</li><li>major_axis_length — length of the major moment of the ellipse fitted</li><li>minor_axis_length — length of the minor moment of the ellipse fitted</li></ol><p>We won’t always use all of these properties, but we can use the bbox property to create a bounding box on the detected objects for this task.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd30c47c6617ccbbd1699641d1b1f56c/href">https://medium.com/media/cd30c47c6617ccbbd1699641d1b1f56c/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/389/1*Xn7efRkg278V6aAKwnSoGw.png" /><figcaption>Figure 5. Detected objects using connected components</figcaption></figure><p>Using connected components, we have detected a total of 18 objects, three objects than the actual count. This is because some objects are not totally separated; thus, they are considered one object.</p><p>Using blob detection and connected components have their advantages and disadvantages. However, based on the two methods we have discussed, using connected components is better, especially when dealing with non-circular objects, but ensuring that objects of interest are adequately cleaned and separated.</p><h3><strong>References</strong></h3><p>[1] B. Borja. “Lecture 4 — Blob Detection and Connected Components”. IIP 2020.</p><p>[2] “scikit-image 0.19.0.dev0 docs,” scikit-image, [Online]. Available: <a href="https://scikit-image.org/docs/dev/">https://scikit-image.org/docs/dev/.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=619e2558d1f2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/introduction-to-image-processing-part-4-object-detection-619e2558d1f2">Introduction to Image Processing — Part 4: Object Detection</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Image Processing — Part 3: Spatial Filtering and Morphological Operations]]></title>
            <link>https://perez-aids.medium.com/introduction-to-image-processing-part-3-spatial-filtering-and-morphological-operations-faefc15238c9?source=rss-dbb4fa1fc346------2</link>
            <guid isPermaLink="false">https://medium.com/p/faefc15238c9</guid>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[morphological-operations]]></category>
            <category><![CDATA[spatial-filtering]]></category>
            <dc:creator><![CDATA[Aids]]></dc:creator>
            <pubDate>Sun, 31 Jan 2021 16:27:50 GMT</pubDate>
            <atom:updated>2021-01-31T16:27:50.849Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction to Image Processing — Part 3: Spatial Filtering and Morphological Operations</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JhO7lGuWrNzr0WOsilS-aA.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@rathaphon-nanthapreecha-1516060?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Rathaphon Nanthapreecha</strong></a> from <a href="https://www.pexels.com/photo/woman-in-white-long-sleeve-shirt-and-black-pants-standing-beside-brown-steel-wall-3713901/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Pexels</strong></a></figcaption></figure><p>Ever wonder how your favorite photo editing tool or native photo manipulation in your smartphones do simple effects such as blurring, sharpening, embossing, and outlining? We will have an idea of how they do in this part, but more importantly, we will discuss the methods we can use to detect and clean our objects of interest from an image. Specifically, we will go through <em>Spatial Filters </em>and <em>Morphological Operations</em>.</p><h3><strong>Spatial Filters</strong></h3><p>Spatial filtering is the process of assigning the value of a pixel based on its neighbors. The filters or masks, which are also known as <strong>kernels</strong>, used in the process are small matrices run in the entire image through a convolution process<em>. </em>[1]</p><p>In machine learning (ML), particularly in artificial neural networks (ANN), spatial filtering is used to extract features by applying different kernels to the image [2]. A list of possible spatial filters we can use are shown in Table 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/428/1*pPMfx57hY-XYWqnGUUBC0w.png" /><figcaption>Table 1. Kernels [3]</figcaption></figure><p>Now, let’s try using it on our favorite photo, Lenna, in a grayscale image.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6bf9f4fc4ced5613dc2299e35efb8a5/href">https://medium.com/media/d6bf9f4fc4ced5613dc2299e35efb8a5/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mx_BNPe6Nlf8PFJx1n3PhQ.png" /><figcaption>Figure 1. Lenna using different spatial filters</figcaption></figure><p>We can see from the result the output image significantly varies, depending on the filter we use. In the <em>Edge</em> filter, for example, the image only highlighted the edges of the image, while the <em>Sharpen </em>and <em>Blur </em>filters, from the name itself, sharpens and blurs the original image to a certain degree.</p><h3><strong>Morphological Operations</strong></h3><p>Morphological operations transform the image’s pixel value based on its neighbors. Similar to Spatial filtering, it also uses smaller matrices, also known as <strong>structuring elements. </strong>[1]<strong> </strong>These operations can be used to further enhance and clean our image by removing, enlarging, or connecting segmented parts in our image.</p><p>Now, let’s try to do morphological operations in the set of images below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p62nfxMWlINuo8-dIcwyOw.png" /><figcaption>Figure 2. Sample images</figcaption></figure><p>Our goal in each image would be to:</p><ol><li>Image 1 — remove the straight lines</li><li>Image 2 — remove the horizontal lines</li><li>Image 3 — connect the gaps</li></ol><p>The first thing we need to do is to build our structuring elements that would match our objects of interest.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6246bd4493d220a9f899971b7a4c570/href">https://medium.com/media/e6246bd4493d220a9f899971b7a4c570/href</a></iframe><p>The <a href="https://scikit-image.org/docs/dev/api/skimage.morphology.html"><em>scikit-image library</em></a><em> </em>contains a lot of morphological operations we can choose from. For this task,<em> </em>we will only use four operations: <em>erosion, dilation, opening, </em>and<em> closing.</em> Because we may need to do several iterations on a specific morphological operation, it would be convenient to put them in a function that would handle the iterations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2fda533502f0222227aa6c3e905d95a/href">https://medium.com/media/b2fda533502f0222227aa6c3e905d95a/href</a></iframe><p>By playing around with different structuring elements and morphological operations, we now have the resulting images, as shown in Figure 3.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6504a6793a61521fab79a2b34af6964b/href">https://medium.com/media/6504a6793a61521fab79a2b34af6964b/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aQefDDApHZ-m7wDz5xFmlQ.png" /><figcaption>Figure 3. Sample images after morphological operations</figcaption></figure><p>Removing and connecting several elements are not that perfect. In the resulting <em>Image 1, </em>you will notice that the circles are a bit smaller than the original image. <em>Image 3,</em> on the other hand, still contains disconnected elements, particularly at the intersection. These outputs can even be further improved by exploring other combinations of structuring elements and morphological operations.</p><p>The stuff that we have discussed here could help prepare our images for the next steps of image processing tasks, such as blob detection and image segmentation, which will be the next topic of this series.</p><h3><strong>References</strong></h3><p>[1] B. Borja, “Lecture 3 — Filtering and Morphological Operations”, IIP 2020</p><p>[2] V. Powell, “Image Kernels,” Setosa, [Online]. Available: <a href="https://setosa.io/ev/image-kernels/">https://setosa.io/ev/image-kernels/</a>.</p><p>[3] “Kernel (image processing),” Wikipedia, [Online]. Available: <a href="https://en.wikipedia.org/wiki/Kernel_(image_processing).">https://en.wikipedia.org/wiki/Kernel_(image_processing).</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=faefc15238c9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Image Processing — Part 2: Image Enhancement]]></title>
            <link>https://perez-aids.medium.com/introduction-to-image-processing-part-2-image-enhancement-1135a2198793?source=rss-dbb4fa1fc346------2</link>
            <guid isPermaLink="false">https://medium.com/p/1135a2198793</guid>
            <category><![CDATA[white-balance]]></category>
            <category><![CDATA[fourier-transform]]></category>
            <category><![CDATA[histogram-equalization]]></category>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[image-enhancement]]></category>
            <dc:creator><![CDATA[Aids]]></dc:creator>
            <pubDate>Sun, 31 Jan 2021 13:11:25 GMT</pubDate>
            <atom:updated>2021-01-31T13:15:17.613Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction to Image Processing — Part 2: Image Enhancement</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yYAEERQW1NVDZVruYsQNHg.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@lanophotography?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>lalesh aldarwish</strong></a> from <a href="https://www.pexels.com/photo/table-lamp-on-nightstand-242827/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Pexels</strong></a></figcaption></figure><p>There are times when we don’t always get that <em>Insta-</em>worthy pictures that we like due to external and natural factors such as bad lighting. Sometimes, we can manually manipulate the source of the lighting, change angles, or tweak some settings in our camera to get the desired shot. However, that’s not always the case. There might also be instances when we have to work with photographs captured by old cameras and can’t help having these noises that degrade the quality of our image.</p><p>For this post, we will discuss how we can enhance these images by manipulating the pixel values to remove image artifacts, improve the color, and balance our pictures’ exposure.</p><h3><strong>Removing Unwanted Artifacts</strong></h3><p>Let’s look at our sample image below. This is a moon crater photo, and you may notice the repeating horizontal line patterns across the entire picture. We want to remove these lines to have a better quality image. But how to do it? We can try blurring these lines but with the expense of the sharpness of our image. Since this is a repeating pattern, we can take advantage of using <em>Fourier transform.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/477/1*eakYegxZBUGvD5conIZScQ.png" /><figcaption>Figure 1. Lunar crater</figcaption></figure><p>A <strong>Fourier transform </strong>is an operation that transforms a signal from the time/spatial domain into the frequency domain. We won’t discuss the technical aspects of Fourier transform here, but let’s see how we can use it to remove these unwanted image artifacts.</p><p>First, we need to get the Fourier transform of the image and identify the periodic patterns and artifacts. We do this by using the following lines of codes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33b828e03b2aa5845f8b1bbe79721d1d/href">https://medium.com/media/33b828e03b2aa5845f8b1bbe79721d1d/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mUJ8pShOktQSMyjaifXn7g.png" /><figcaption>Figure 2. Fourier transform</figcaption></figure><p>The second image (right) in Figure 2 shows the original image (left) in the Fourier space. If you notice, the white lines in the original image are causing the vertical line at the middle of the Fourier transform. We need to mask the vertical line with a small value, except the DC component or the <em>dot </em>at the center of the Fourier transform, to remove the horizontal white lines in the original image. After that, we then convert the modified Fourier image back to the spatial domain.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/095441a709ba1337024475aee6ee8600/href">https://medium.com/media/095441a709ba1337024475aee6ee8600/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/964/1*72KfSSu5H9-ONLrfkH_ggA.png" /><figcaption>Figure 3. Modified Fourier transform</figcaption></figure><p>In Figure 3, we can see that we now have a better quality image after modifying the image in the Fourier space compared to the original one.</p><h3><strong>Color Correction</strong></h3><h3><strong>1. White Balancing</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*gvy4otsBAcnVSPZRdb7rig.png" /><figcaption>Figure 4. Room with bean bags</figcaption></figure><p>White balancing color correction method is done by making white or neutral colored regions in person appear as white in the image. We will use three algorithms under this method: <em>White-patch, Gray-world, </em>and <em>Ground-truth </em>algorithms. Let’s try to correct the bluish image shown in Figure 4.</p><p><strong>1.1.</strong> <strong>White-patch Algorithm</strong></p><p>This algorithm assumes that the color white is the maximum value of each RGB channel (255,255,255). We normalize each channel by dividing each channel by its maximum. The caveat here is that each color channel may yield 255 as its maximum value even if there is an overcast. To resolve this, we only get the <em>nth </em>percentile of the maximum values per channel. In this example we use the 95th percentile.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b446c34ab7d6d2a798df36c8153e6704/href">https://medium.com/media/b446c34ab7d6d2a798df36c8153e6704/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OP-JA50G3yFFJIs58iMQWw.png" /><figcaption>Figure 5. Result of White-patch algorithm</figcaption></figure><p><strong>1.2.</strong> <strong>Gray-world Algorithm</strong></p><p>With reference to the name itself, the Gray-world algorithm assumes that pixels are gray on average and that the mean of each color channel is the same. We correct or normalize the image using the average value of each pixel.</p><p>It is also important to note that some images have a fourth channel, the alpha or <em>opacity. </em>To ensure that it will not affect our color collection methodologies, we either disregard it or set the fourth channel back to its maximum value, which is 255.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0069c79f7598b9396a6891babffba239/href">https://medium.com/media/0069c79f7598b9396a6891babffba239/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aOMI2ATO85bgAplZ6PrbyA.png" /><figcaption>Figure 6. Result of Gray-world algorithm</figcaption></figure><p><strong>1.3. Ground-truth algorithm</strong></p><p>In this algorithm, we select a region or patch on our image, which will serve as our “true” value. After that, we then normalize the image either by (1) dividing each channel with the maximum values or (2) match the mean value of each color channel of the original image and the mean value of each channel of our “true” white-patch.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8703d9642fe2af023109894366d19b0/href">https://medium.com/media/c8703d9642fe2af023109894366d19b0/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*3HZ2IR5x_98fJU3hWuoZwA.png" /><figcaption>Figure 7. Result of Ground-truth algorithm</figcaption></figure><p>Although there have been differences in the resulting images using different white balancing algorithms, we can agree that other colors have become more vibrant in all resulting images compared with the original image.</p><p><strong>2.</strong> <strong>Histogram Manipulation</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*kRfVMVXkzk4iYskemPWkDw.png" /><figcaption>Figure 8. Dark image</figcaption></figure><p>Not all images can be corrected using white balancing methods. Take, for example, the poorly-lit photo in Figure 8. If you try to enhance the image using the methods presented above, it will make almost no difference from the original image. Another way to color correct an image is to manipulate the image’s intensity histogram.</p><p><strong>2.1. Histogram Equalization</strong></p><p>If we plot the histogram of the intensity value of the image above, as shown in Figure 9, we can see that most pixels are of low-intensity values. To correct this, we need to manipulate the image’s intensity histogram to match a specific target distribution. For this example, we set our target distribution to be uniform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y9psCOhtjHIrAtnuSv9OLg.png" /><figcaption>Figure 9. Intensity Histogram and Target CDF</figcaption></figure><p>First, we need to compute the percentile of each intensity value of the actual image CDF and then replace it with the corresponding percentile intensity value of the target CDF. Let’s try to do it first on a gray-scaled image.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf5399caedb80dd7677a505a3c73b8e8/href">https://medium.com/media/cf5399caedb80dd7677a505a3c73b8e8/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PRJqolVU21CEbPVFHXd8qQ.png" /><figcaption>Figure 10. Histogram equalization result</figcaption></figure><p><em>Voila! </em>The resulting image is now much better, and we can now see some of the details that we cannot see in the original one because it is too dark. The target distribution need not always be uniform. We can use other distributions such as the Gaussian distribution. If we want it to be colored, we need to redo the process in all three color channels, as shown in the figure below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*F55eSNyhzow2Ea1XivcoOA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YwTOhzDg1Y0393gRLOpQrA.png" /><figcaption>Figure 11. Histogram equalization on RGB channels</figcaption></figure><p><strong>2.2. Contrast Stretching</strong></p><p>Another and simpler way of manipulating the intensity histogram is to rescale the intensities within a range of percentiles. In this example, we will use the 2nd and the 90th percentile.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cfd8d3ce0e7dda26386c9dfac34037ea/href">https://medium.com/media/cfd8d3ce0e7dda26386c9dfac34037ea/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*GYK2PVtcK30HkU2ShCQeqg.png" /><figcaption>Figure 12. Contrast Stretching result</figcaption></figure><p>And that’s it for image enhancements. The next part of this series will be filtering and morphological operations.</p><h3><strong>References</strong></h3><p>[1] B. Borja,”Lecture 2 — Image Enhancements”, IIP 2020</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1135a2198793" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Image Processing — Part 1: Fundamentals]]></title>
            <link>https://perez-aids.medium.com/introduction-to-image-processing-part-1-fundamentals-579cc414cebe?source=rss-dbb4fa1fc346------2</link>
            <guid isPermaLink="false">https://medium.com/p/579cc414cebe</guid>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[fundamentals]]></category>
            <dc:creator><![CDATA[Aids]]></dc:creator>
            <pubDate>Sun, 31 Jan 2021 10:46:11 GMT</pubDate>
            <atom:updated>2021-01-31T10:46:11.403Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction to Image Processing — Part 1: Fundamentals</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dRJgUzD7CgJ-VX6zbAEESw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@the_roaming_platypus?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">timJ</a> on <a href="https://unsplash.com/s/photos/enhanced?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p><strong>Image Processing </strong>includes methods used to transform and enhance images depending on the context to be solved. It is part of a much broader field, <em>Computer Vision, </em>which deals with processing and analyzing input image and video data to mimic the human visual system. We can use image processing to manipulate a single image for a specific use or as part of data preprocessing methods in preparation for machine learning (ML) model training.</p><p>This series of image processing posts will show the cool stuff we can do with our images. For the <em>vloggers </em>and avid <em>socmed </em>users out there, you will have an idea of how Instagram<em> </em>and Snapchat filters were implemented. For the image editors, you will know the math behind some of the commonly used <em>Photoshop</em> functions you are using.</p><h3><strong>Representation of Digital Image</strong></h3><p>For part 1 of this series, we will discuss some of the fundamentals of image processing, and let’s start with how an image is represented digitally. A digital image is represented using pixels. As seen in Figure 1, the pear-shaped blob is represented using different number of pixels, and notice how it differs if we use a bigger or small number of pixels.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/356/1*qwcgByUPLF6VdqUhKTTJsA.png" /><figcaption>Figure 1. Representing image using pixels</figcaption></figure><h3><strong>Sampling</strong></h3><p>Sampling in the context of image processing involves taking the value of the image at regular spatial intervals. In layman’s terms, it simply answers the question, <em>“How many cells (pixels) do we want to represent our image?”. </em>By this time, I think you must meet Lenna since we will be seeing her a lot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*tvZLuGmbFJktjdk9KCI6YA.png" /><figcaption>Figure 2. Sampling with Lenna</figcaption></figure><p>Different sample sizes were used to represent the image of Lenna, as can be seen in Figure 2, where <em>N </em>is the number of pixels per side of the image (e.g., <em>N=</em>55, 55x55 pixels). See that the higher the number of pixels we use, the better the resolution and the more information we get.</p><p>It is not always the case, however, that the higher the number, the better. In <em>N=</em>110 and<em> N</em>=220 for example, notice that there is not much difference between the two pictures, but the number of pixels is doubled. In this case, and if we are talking about using this as an input to machine learning model, then we better use the 110 pixels to conserve our resources.</p><p>If you find Lenna pretty, get to know more about here in this <a href="https://en.wikipedia.org/wiki/Lenna">link</a>.[1]</p><h3><strong>Quantization</strong></h3><p>Quantization involves discretizing the intensity values of the analog image. It is the amount of information per pixel or the image representation per pixel. Let’s take Lenna again, for example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*frUOrSs9R78AiuGPu3A_dg.png" /><figcaption>Figure 3. Quantization with Lenna</figcaption></figure><p>As shown in Figure 3, <em>k</em> represents the intensity value or also known as its bit depth. In simple terms, it is the number of colors with which we can represent the image. It is evident that the more colors we use, the better the representation of our image will be.</p><p>Just like sampling, higher does not always mean better. It depends on the context or the problem that we are trying to solve. For example, if we only have a black and white image (which is technically 1s and 0s), we don’t need an 8-bit representation of image like in RGB because it will only be a waste of resources.</p><h3><strong>Image Types</strong></h3><p>Going back to Quantization, we can represent images in different values per pixel. Figure 4 shows the three image types, with respect to pixel value, where we can represent Lenna. <strong>Binary Images</strong><em>, </em>as mentioned earlier, are represented using 1s and 0s. <strong>Grayscale</strong><em>, </em>on the other hand, is represented from 0 to 255. Lastly, <strong>Colored</strong><em> </em>(colormap) in this context is also represented in 0 to 255 values but using three color channels — <em>Red, Green, Blue</em>, or more commonly known as <strong>RGB </strong>(see Figure 5).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*Q0ysYFreN5p0SbWJSZ98eg.png" /><figcaption>Figure 4. Image types with Lenna</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*mkSNt5StEuP1kVgOlI0bUg.png" /><figcaption>Figure 5. RGB color channels</figcaption></figure><p>In the next parts of this series, we will know when and why we need to use a particular image type.</p><h3><strong>Color Spaces</strong></h3><p>As mentioned in the <em>Image Types</em> section, colored images can be represented using the <strong>RGB </strong>color channels. And since we are using three separate color channels, we can extract and represent the images per channel as illustrated in Figure 6.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*EdGlu56XMy_Gsm0Vb5U_0g.png" /><figcaption>Figure 6. RGB with Lenna</figcaption></figure><p>Representing images using different color channels is useful, particularly in segmentation tasks. For example, we want to isolate apples and bananas from a picture of fruits. We could isolate these elements by determining the RBG values for red (apple) and yellow (bananas).</p><p>But what if we want to segment an apple from a picture of apples and strawberries? This could be a bit tricky since apples and strawberries are both red generally. This is where <strong>HSV </strong>color space will come in handy. HSV stands for <em>Hue, Saturation, and Value, </em>where [2]:</p><p><strong>Hue: </strong>dominant wavelength or the color itself (e.g., red)</p><p><strong>Saturation : </strong>brilliance and intensity of a color</p><p><strong>Value : </strong>lightness or darkness of a color</p><p>Figure 7 shows the picture of Lenna in HSV space.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*_vNQ9BevuXj7QiKYl1OT9Q.png" /><figcaption>Figure 7. HSV with Lenna</figcaption></figure><p>So even if apples and strawberries are both red in nature, they may have different levels of saturation and value, which we can use to separate them from one another.</p><p>Image segmentation will be further discussed in the next parts of this series.</p><h3>References</h3><p>[1] “Lenna,” Wikipedia, [Online]. Available: https://en.wikipedia.org/wiki/Lenna.</p><p>[2] “Hue, Value, Saturation,” Learn, [Online]. Available: <a href="http://learn.leighcotnoir.com/artspeak/elements-color/hue-value-saturation/">http://learn.leighcotnoir.com/artspeak/elements-color/hue-value-saturation/.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=579cc414cebe" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>