Updated April 7, 2023
Introduction to OpenCV findContours
The following article provides an outline for OpenCV findContours. OpenCV find contour () is functionality present in the Python coding language that defines the lines that present that enable all the points alongside the boundary for the image that has been provided by the coder that has the same intensity in terms of pixels. These happen to be essentially helpful in terms of analysing the shape of the image provided, in the detection of the size and dimension of the object that has to be detected in the provided image and in the detection of specific objects.
It works essentially well on binary pictures and images, thought at the first application of Sobel edges and thresholding techniques should be implemented. Each of the individual contour is representative of an individual numpy array with coordinates x and y, which represents the boundary point for the object that the user has entered.
Syntax of OpenCV findContour()
Following is the syntax used for application of the OpenCV findContour method:
void cv :: findContours (InputOutputArray image,
OutputArrayOfArrays contours,
OutputArray hierarchy,
int mode,
int method,
Point offset = Point()
)
Parameters of OpenCV findContour():
There are basically 3 arguments available for the method. The first argument essentially refers to the image source that the user has provided, the second argument refers to the mode for contouring retrieval, and the third arguments refer to the contouring method for approximation, on the application of the method, the modified image that has contouring and hierarchy implied on it.
Following are the parameters used for the Open CV findContour() method:
Image | It is the source image that is used which is generated through a single channel of about eight bits. Any pixels that are in the non-zero category designated as 1’s, the pixels with zero categories are designated as 0’s, perforce converting the image into a binary form. Various parameters (such as adaptiveThreshold, inRange, Canny, threshold, etc.) can then be further applied in order to create the binary images using the coloured or grayscale image that is the user. In case the mode is equal to RETR_FLOODFILL or RETR_CCOMP, then the image which is being entered by the user as the source image be a 3- bit integer-based image of label CV_32SC1. |
Contours | Detection of contour – each of the single contour has been stored in the form of multiple points that are vectors. |
Hierarchy | It is an optional parameter that is presented as an output vector. It contains vital information related to the typology of the image which the user has entered. the parameter consists of as many number elements as there is the total number of contours in the image. Let the use of its integral differentiation demonstrate it. for an image with the ith contour, the following would be the contour[i], and the relevant hierarchy for the contour would be hierarchy[i][0] -> hierarchy[i][1] -> hierarchy[i][2] -> and hierarchy[i][3]; present a zero-based indices system in the contour, where each of the previous and better contour is at the hierarchy level as the prior ( technically referring to the first child born to run the parent contour). In a scenario where there is no nested contour, parent contour, previous on next contour, for contour I, its corresponding element with respect to its hierarchy would be a negative value (hierarchy[i]). |
Mode | Mode activated specifically to contour retrieval. |
Method | Mode activated specifically to depict the approximation method for the image contour. |
Offset | It is an optional parameter by using which every contour point can be shifted. It is essentially useful when the contour has been extracted are image ROI, and then further analysis should be done in the context of the whole image. |
How does OpenCV findContour() Work?
- When the computer is made to detect the edges of an input image, it then finds the points where specifically, there is a significant difference notice in the intensity of colouration, then simply those pixels are turned on. A stark differences noticed when the system is instructed to perform contouring.
- Contours are basically an abstract collection of segments and points that correspond to the reflective shapes of the objects that are present in the images that have been processed through the system. as a result of this; it is in our capacity to manipulate the contouring within the programs through which they are being accessed.
- This can be done in multiple ways, such as having a count on the number of contours in an image and then using that to categorize the object shapes, for segmentation of images or cropping objects from the image that is being processed and many more such similar functions.
Example
Given below is the example of OpenCV findContour:
Code:
import numpy as np1
import cv2
img_1 = cv2.imread('EduCBA.png')
print (“The Gray scale image is ” /n)
imgray_1 = cv2.cvtColor(img_1, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray_1, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print("The Total Number of Contours in the Image = ")
//command len used to calculate the number of contours in the image
print (str(len(contours)))
print(contours[0])
cv2.drawContours(img_1, contours, -1,(0,2550,0),3)
cv2.drawContours(imgray_1, contours, -1,(0,255,0),3)
print (“The original image is: “ /n)
cv2.imshow('Image', img_1)
cv2.imshow('Image GRAY', imgray_1)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
The output screen displays the screenshot on the compiling of the above code.
Conclusion – OpenCV findContours
The OpenCV find contour method is essentially useful as it provides for a pre-defined function that can be called without implementation of an entire code and can be modified using the various parameters. It is essentially helpful in terms of analysing the shape of the image provided, in the detection of the size and dimension of the object that has to be detected in the provided image and in the detection of specific objects. This is done in order to categorize the object shapes, for segmentation of images or cropping objects from the image that is being processed and many more such similar functions.
Recommended Articles
We hope that this EDUCBA information on “OpenCV findContours” was beneficial to you. You can view EDUCBA’s recommended articles for more information.