Change language

Find and Draw Paths with OpenCV | python

| | |

OpenCV has findContour () which helps in extracting contours from an image. It works best with binary images, so we must first apply thresholding, Sobel edges, etc.

Below is the code to find outlines —

import cv2

import numpy as np

 
# Let’s upload a simple image with 3 black squares

image = cv2.imread ( C: //Users//gfg//shapes.jpg )

cv2.waitKey ( 0 )

 
# О shades of gray

gray = cv2.cvtColor (image, cv2.COLOR_BGR2GRAY)

 
# Find Canny edges

edged = cv2.Canny (gray, 30 , 200 )

cv2.waitKey ( 0 )

 
# Finding outlines
# Use a copy of the image, for example edged.copy ()
# because findContours changes the image

contours, hierarchy = cv2.findContours (edge d, 

cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

 

cv2.imshow ( ’Canny Edges After Contouring’ , edged)

cv2.waitKey ( 0 )

  

print ( "Number of Contours found =" + str ( len (contours)))

  
# Draw all outlines
# -1 means drawing all contours

cv2.drawContours (image, contours,  - 1 , ( 0 , 255 , 0 ), 3 )

 

cv2.imshow ( ’Contours’ , image)

cv2.waitKey ( 0 )

cv2.destroyAllWindows ()

Output:

We can see that there are three essential arguments in the cv2.findContours () function. The first — original image, second — contour search mode, third — a contour approximation method that outputs the image, contours, and hierarchy. & # 39;  contours & # 39; — this is a Python list of all the contours of the image. Each individual contour is a Numpy (x, y) array of coordinates of the object’s boundary points.

Path approximation method —
Above we can see that the paths are boundaries shapes with the same intensity. It stores the (x, y) coordinates of the shape’s border. But does it store all coordinates? This is set by this contour approximation method. 
If we pass in cv2.CHAIN_APPROX_NONE , all endpoints are preserved. But do we really need all the items? For example, if we have to find the outline of a straight line. We only need the two endpoints of this line. This is what cv2.CHAIN_APPROX_SIMPLE does. It removes all unnecessary points and shrinks the path, thereby saving memory.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method