Contours OpenCV Python | Approximation, Contour Sorting & Hierarchy

Contours OpenCV

What is Image Segmentation? What are Contours in OpenCV Python? In this blog we will learn about Both Image Segmentation and Contours in OpenCV Python.

Let’s learn Contours in OpenCV Python!

What are Contours?

Contours are continuous lines or curves that bound or cover the full boundary of an object in an image.

Contours are very important in:

Steps for Finding Contours in OpenCV Python

  1. Read image in OpenCV using imread() function.
  2. Convert the Colored image to Grayscale image.
  3. Find edges using Canny edge detection.
  4. Find Contours.
  5. Draw all Contours on the image.

Contours OpenCV Python Algorithm

Loading the image using imread() function and converting it to grayscale can be done by you. If you are facing difficulties in these steps must read basic opencv operations.

Canny Edge Detection

edge = cv.Canny(gray,30,200)

Canny Edge Detection helps us in finding edges of an image.

Make a copy of the canny edge detected image, as Finding Contours may modify the image.

edge_copy = edge.copy()

Find Contours in OpenCV Python

contours, hierarchy = cv.findContours(edge_copy, RETR_EXTERNAL,cv.CHAIN_APPROX_NONE)

This step finds the Contours in the edge detected image.

You can display the contours on the edge detected image, using imshow() function present in OpenCV.

findContours() function returns an array/list of Contours.

You can also know how many Contours are there in an image.

print(“no. Of contours =” + str(len(contours)))

Draw all Contours in OpenCV Python

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

This step is used to draw all the Contours on the image.

Note: Use -1 in the third parameter to draw all the contours in the list.

Display Contours Image

Display the image on which the Contours are drawn using imshow() function in OpenCV.

Approximation Methods in Contours OpenCV Python

  • cv.CHAIN_APPROX_NONE –》 This method stores all the boundary points, but we do not need necessarily need all the boundary points. If the points form a straight line, we only need the start and ending points of that line.
  • CV.CHAIN_APPROX_SIMPLE –》 This method instead only provides these start and endpoints of bounding contours, thus resulting in much efficient storage of contour information.

Hierarchy in Contours OpenCV Python

  • CV.RETR_LIST – Retrieves all the Contours.
  • CV.RETR_EXTERNAL – Retrieves external or outer Contours only.
  • CV.RETR_COMP – Retrieves all in a 2-level hierarchy.
  • CV.RETR_TREE – Retrieves all in the full hierarchy.

Hierarchy is stored in the following format:

[ Next, Previous, First Child, Parent ]

Contours Sorting

Sorting in Contours is quite useful when doing image processing.

Contour Sorting by Area

This can assist in Object Recognition ( Using Contour area ).

  • Eliminate small Contours that may be noise.
  • Extract the largest contours.

sorted-contour = sorted(contours, key=cv.contourArea,reverse=True)

Sorting by Spatial position

  • Sort characters left to right.
  • Process images in a specific order.

Contours Approximation

CV.approxPolyDP(contour,Approximation accuracy,closed)

Parameters of the function are:

  • Contour– This is the individual contour we wish to approximate.
  • Approximation accuracy– Important parameter in determining the accuracy of the Approximations. Small values give precise Approximations, large values give more generic Approximation. A good rule of thumb is less than 5% of the contour perimeter.
  • Closed – A boolean value that states whether the Approximation contour should be open or closed.

Contours OpenCV Documentation

Learn more about Contours in OpenCV Official Documentation.

How to find Contours in OpenCV Python?

contours, hierarchy = cv.findContours(edge_copy, RETR_EXTERNAL,cv.CHAIN_APPROX_NONE)

findContours() function returns an array/list of Contours.

How to sort Contours in OpenCV Python?

Contour Sorting by Area
1. sorted-contour = sorted(contours, key=cv.contourArea,reverse=True)
Sorting by Spatial position
1. Sort characters left to right.
2. Process images in a specific order.

Types of Hierarchy in Contours OpenCV Python.

Hierarchy in Contours
1. CV.RETR_LIST – Retrieves all the Contours.
2. CV.RETR_EXTERNAL – Retrieves external or outer Contours only.
3. CV.RETR_COMP – Retrieves all in a 2-level hierarchy.
4. CV.RETR_TREE – Retrieves all in the full hierarchy.

How to Draw Contours in OpenCV Python?

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

What is Contour in OpenCV Python?

Contours are continuous lines or curves that bound or cover the full boundary of an object in an image.

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *