Contours are defined as the line joining all the points along the boundary of an image that are having the same intensity. Contours come handy in shape analysis, finding the size of the object of interest, and object detection.
findContour() function that helps in extracting the contours from the image. It works best on binary images, so we should first apply thresholding techniques, Sobel edges, etc.
Below is the code for finding contours –
We see that there are three essential arguments in
cv2.findContours() function. First one is source image, second is contour retrieval mode, third is contour approximation method and it outputs the image, contours, and hierarchy. ‘contours‘ is a Python list of all the contours in the image. Each individual contour is a Numpy array of (x, y) coordinates of boundary points of the object.
Contours Approximation Method –
Above, we see that contours are the boundaries of a shape with the same intensity. It stores the (x, y) coordinates of the boundary of a shape. But does it store all the coordinates? That is specified by this contour approximation method.
If we pass
cv2.CHAIN_APPROX_NONE, all the boundary points are stored. But actually, do we need all the points? For eg, if we have to find the contour of a straight line. We need just two endpoints of that line. This is what
cv2.CHAIN_APPROX_SIMPLE does. It removes all redundant points and compresses the contour, thereby saving memory.
- Find Co-ordinates of Contours using OpenCV | Python
- Python | Draw rectangular shape and extract objects using OpenCV
- Draw a triangle with centroid using OpenCV
- Draw geometric shapes on images using OpenCV
- Find Circles and Ellipses in an Image using OpenCV | Python
- Python PIL | ImageDraw.Draw.arc()
- Python PIL | ImageDraw.Draw.chord()
- Python PIL | ImageDraw.Draw.ellipse()
- Python PIL | ImageDraw.Draw.multiline_textsize()
- Python PIL | ImageDraw.Draw.text()
- Python PIL | ImageDraw.Draw.line()
- Python PIL | ImageDraw.Draw.rectangle()
- Python PIL | ImageDraw.Draw.pieslice()
- Python PIL |ImageDraw.Draw.multiline_text()
- Python PIL | ImageDraw.Draw.polygon() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.