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

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically