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:
- Object Detection
- Shape Detection
Steps for Finding Contours in OpenCV Python
- Read image in OpenCV using imread() function.
- Convert the Colored image to a Grayscale image.
- Find edges using Canny edge detection.
- Find Contours.
- 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 the 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 ]
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.
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 Approximations. 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.