<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Simarpreet Singh</title>
    <description>The latest articles on Forem by Simarpreet Singh (@simarpreetsingh019).</description>
    <link>https://forem.com/simarpreetsingh019</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F241760%2F19845a90-2010-47e5-bccc-a79ae9499778.jpg</url>
      <title>Forem: Simarpreet Singh</title>
      <link>https://forem.com/simarpreetsingh019</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/simarpreetsingh019"/>
    <language>en</language>
    <item>
      <title>Detecting Geometrical Shapes in an image using OpenCV</title>
      <dc:creator>Simarpreet Singh</dc:creator>
      <pubDate>Sun, 21 Jun 2020 09:15:36 +0000</pubDate>
      <link>https://forem.com/simarpreetsingh019/detecting-geometrical-shapes-in-an-image-using-opencv-4g72</link>
      <guid>https://forem.com/simarpreetsingh019/detecting-geometrical-shapes-in-an-image-using-opencv-4g72</guid>
      <description>&lt;p&gt;Well I was just exploring OpenCV library of python in this quarantine , and going through that, I came across term &lt;br&gt;
Contour.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Contours can be explained simply as a curve joining all the      continuous points (along the boundary), having the same color or &lt;br&gt;
intensity. The contours are a useful tool for shape analysis and  object detection and recognition.And got to learn how we can use it to find geometrical shapes in  an image.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s start how it goes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach&lt;/strong&gt; : The approach we would use to detect the shape of a given polygon will be based on classifying the detected shape on the basis of a number of sides it has. For example, if the detected polynomial has 3 sides, then it could be considered as a triangle, if the polynomial has 4 sides then it could be classified as a square or a rectangle, and so on.&lt;br&gt;
Let’s find how to do it&lt;br&gt;
Importing libraries&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;cv2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Import image and convert to grayscale image.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  3. Applying thresholding on image and then finding contours.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cv2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;imread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'shapes.PNG'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;imgGry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cv2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cvtColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cv2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;COLOR_BGR2GRAY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;thrash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cv2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;imgGry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;240&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cv2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CHAIN_APPROX_NONE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;contours&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cv2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;findContours&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thrash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cv2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RETR_TREE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cv2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CHAIN_APPROX_NONE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;“ &lt;em&gt;ret&lt;/em&gt; ” collects a value, which according to &lt;em&gt;OTSU&lt;/em&gt; method , is the best value for thresholding the image.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thresholding is a technique in &lt;strong&gt;OpenCV&lt;/strong&gt;, which is the assignment of pixel values in relation to the &lt;strong&gt;threshold&lt;/strong&gt; value provided. In thresholding, each pixel value is compared with the threshold value. If the pixel value is smaller than the threshold, it is set to 0, otherwise, it is set to a maximum value (generally 255).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and for “ &lt;em&gt;thrash&lt;/em&gt; ”: its the threshold value of image&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;What is threshold value of an image?&lt;/em&gt;**&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Threshold&lt;/strong&gt; is some fixed value which draws a boundary line between two set of data. Binary (Bi-valued) &lt;strong&gt;Image means&lt;/strong&gt;, only bi or two intensity &lt;strong&gt;values&lt;/strong&gt; can be used to represent the whole image. In image processing generally, we say a image binary when, it consists only black and white pixels.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Next&lt;/em&gt;:&lt;br&gt;
While using cv2.findContour(), we are receiving contours and hierarchy. Actually we got three arrays, first is the image, second is our contours, and one more output which we named as hierarchy.&lt;/p&gt;

&lt;p&gt;Normally we use the &lt;em&gt;cv2.findContours()&lt;/em&gt;** function to detect objects in an image, right ? Sometimes objects are in different locations. But in some cases, some shapes are inside other shapes, just like nested figures. In this case, we call the outer one as &lt;strong&gt;parent&lt;/strong&gt; and inner one as &lt;strong&gt;child&lt;/strong&gt;. This way, contours in an image has some relationship to each other. And we can specify how one contour is connected to each other, like, is it a child of some other contour, or is it a parent, etc. Representation of this relationship is called the &lt;strong&gt;Hierarchy&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Contours&lt;/strong&gt; : &lt;em&gt;Contours&lt;/em&gt;** can be explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity. The &lt;strong&gt;contours&lt;/strong&gt; are a useful tool for shape analysis and object detection and recognition.&lt;/p&gt;

&lt;p&gt;4 . &lt;em&gt;Next step:&lt;/em&gt;**&lt;br&gt;
a)Start the loop in range of contours and iterate through it .&lt;br&gt;
b)Printing the polynomial name according to no. of contours together by using &lt;em&gt;approxPolyDPfunction()&lt;/em&gt;**.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Short overview of functions used in this block:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;approxPolyDP()&lt;/em&gt;** :This function calculates and approximates a polygonal curve with specified precision&lt;/p&gt;

&lt;p&gt;&lt;em&gt;approxPolyDP()&lt;/em&gt;** &lt;em&gt;approximates a contour shape to another shape with less number of vertices depending upon the precision we specify. It is an implementation of &lt;a href="https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm"&gt;Douglas-Peucker&lt;/a&gt; algorithm. Check the wikipedia page for algorithm and demonstration.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;example of how approxpolyDp() works.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D0NwkR-_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1214/0%2AmkGxCvPJZLv2U3fk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D0NwkR-_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1214/0%2AmkGxCvPJZLv2U3fk.jpg" alt="Alt Contour shape" width="607" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;drawContours()&lt;/strong&gt;: Draws the contours outlines or filled color .&lt;br&gt;
&lt;em&gt;To draw the contours, _cv2.drawContours&lt;/em&gt; function is used. It can also be used to draw any shape provided you have its boundary points. Its first argument is source image, second argument is the contours which should be passed as a Python list, third argument is index of contours (useful when drawing individual contour. To draw all contours, pass -1) and remaining arguments are color, thickness etc._&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BoundingRect()&lt;/strong&gt; : It gives the boundary points of the rectangle.&lt;br&gt;
&lt;strong&gt;putText()&lt;/strong&gt; : It puts the text over the image.&lt;/p&gt;

&lt;p&gt;5 . Show the &lt;em&gt;image and close all windows&lt;/em&gt;**&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;and our Program runs successfully.&lt;br&gt;
Image used :&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zb6vPu19--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/722/0%2A-Z0nGGI02DVLUtfA" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zb6vPu19--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/722/0%2A-Z0nGGI02DVLUtfA" alt="Alt Image used" width="361" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output of code:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OIywJRCv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/728/0%2A7nEs6DoTJqqeFYcD" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OIywJRCv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/728/0%2A7nEs6DoTJqqeFYcD" alt="Alt output of code" width="364" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Whole code available 👇:&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;p&gt;Github link to this repo 👇:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i3JOwpme--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/simarpreetsingh-019"&gt;
        simarpreetsingh-019
      &lt;/a&gt; / &lt;a href="https://github.com/simarpreetsingh-019/openCV-Learning"&gt;
        openCV-Learning
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Contains codes and Mini project made while learning OpenCV
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
openCV-Learning&lt;/h1&gt;
&lt;p&gt;This repository contains some programs that I learned while exploring the library : OpenCV using python&lt;/p&gt;
&lt;h2&gt;
Articles Link&lt;/h2&gt;
&lt;h1&gt;
What is canny edge detection&lt;/h1&gt;
&lt;br&gt;
&lt;a href="https://medium.com/simply-dev/what-is-canny-edge-detection-cfefa272a8d0?source=friends_link&amp;amp;sk=2f0eae7b0ff9eba81998164f0602ece4" rel="nofollow"&gt;https://medium.com/simply-dev/what-is-canny-edge-detection-cfefa272a8d0?source=friends_link&amp;amp;sk=2f0eae7b0ff9eba81998164f0602ece4&lt;/a&gt;
&lt;h1&gt;
Detecting Simple geometrical shapes in an image using OpenCV&lt;/h1&gt;
&lt;br&gt;
&lt;a href="https://medium.com/simply-dev/detecting-geometrical-shapes-in-an-image-using-opencv-bad67c40174f?source=friends_link&amp;amp;sk=81baba0e6d74069cef633bb78f408c08" rel="nofollow"&gt;https://medium.com/simply-dev/detecting-geometrical-shapes-in-an-image-using-opencv-bad67c40174f?source=friends_link&amp;amp;sk=81baba0e6d74069cef633bb78f408c08&lt;/a&gt;
&lt;h2&gt;
Mini projects&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;h3&gt;
Pedestrian motion detection&lt;/h3&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://user-images.githubusercontent.com/47384034/120013364-4844d100-bffe-11eb-802e-2d20b6eecfe5.gif"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oQMItWPA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://user-images.githubusercontent.com/47384034/120013364-4844d100-bffe-11eb-802e-2d20b6eecfe5.gif" alt="pedestrian-detection"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;ol start="2"&gt;
&lt;li&gt;
&lt;h3&gt;
Detecting Geometrical shapes in an image&lt;/h3&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://user-images.githubusercontent.com/47384034/120013730-bee1ce80-bffe-11eb-9e7a-a32966c3d728.JPG"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ksYVbWRG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/47384034/120013730-bee1ce80-bffe-11eb-9e7a-a32966c3d728.JPG" alt="output2"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/simarpreetsingh-019/openCV-Learning"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;Thanks for reading! I hope you enjoyed the article and gained some additional insights. If you did, feel free to leave a clap! Constructive feedback is appreciated. Feel free to reach out to me &lt;a href="https://www.linkedin.com/in/simarpreetsingh019/"&gt;here on linkedin&lt;/a&gt; or &lt;a href="mailto:simarpreetsingh.019@gmail.com"&gt;simarpreetsingh.019@gmail.com&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;That’s all for this time , see you soon with another post.&lt;br&gt;
Simarpreet Singh , signing off.&lt;br&gt;
ਸਤਿ ਸ਼ੀ੍ ਅਕਾਲ 🙏&lt;/p&gt;

&lt;p&gt;Medium article link: &lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/simply-dev/detecting-geometrical-shapes-in-an-image-using-opencv-bad67c40174f" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tYVTk4r8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/fit/c/96/96/1%2Ax-gqjc7uRIlJ88wRQYOmVw.jpeg" alt="Simarpreet Singh"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/simply-dev/detecting-geometrical-shapes-in-an-image-using-opencv-bad67c40174f" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Detecting Geometrical Shapes in an image using OpenCV | by Simarpreet Singh | Simply Dev | Medium&lt;/h2&gt;
      &lt;h3&gt;Simarpreet Singh ・ &lt;time&gt;Jun 21, 2020&lt;/time&gt; ・ 
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ze5yh_2q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/medium_icon-90d5232a5da2369849f285fa499c8005e750a788fdbf34f5844d5f2201aae736.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;References:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_contours/py_table_of_contents_contours/py_table_of_contents_contours.html"&gt;https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_contours/py_table_of_contents_contours/py_table_of_contents_contours.html&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wikipedia&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Python3 #Detecting_Shapes #ImageProcessing #OpenCV&lt;/p&gt;

</description>
      <category>python</category>
      <category>opencv</category>
      <category>machinelearning</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
