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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Find Co-ordinates of Contours using OpenCV | Python
- Draw contours on an unstructured triangular grid in Python using Matplotlib
- Ternary contours Plot using Plotly in Python
- Draw a rectangular shape and extract objects using Python's OpenCV
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Draw geometric shapes on images using OpenCV
- Draw a triangle with centroid using OpenCV
- Transition from OpenCV 2 to OpenCV 3.x
- Find Circles and Ellipses in an Image using OpenCV | Python
- Python | Create video using multiple images using OpenCV
- Image resizing using Seam carving using OpenCV in Python
- Draw Cube and Cuboid in Python using Turtle
- How to draw 2-layered and colored spider web in Python using Turtle Module?
- Erosion and Dilation of images using OpenCV in python
- Addition and Blending of images using OpenCV in Python
- Face Detection using Python and OpenCV with webcam
- Negative transformation of an image using Python and OpenCV
- White and black dot detection using OpenCV | Python
- Log transformation of an image using Python and OpenCV
- Display date and time in videos using OpenCV - Python
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.