The contours are the boundaries of the object, they are similar to the edges, but we cannot use them as edges. The counter can be seen as the boundaries of the continuous lines around an object. They come in very handy when we want to perform shape analysis and object detection.
Here, in this Python tutorial, we will learn how to find and identifies contours in OpenCV.
Install Python OpenCV library
So let's begin with installing the dependency or OpenCV library.
pip install opencv-python
When you install the
module, it will also install the Python numpy library along. So you do not have to worry about installing the numpy library separately. for this tutorial, we will be using the following
Now open your favorite
Python IDE or Text Editor
and start coding. Let's start with importing the OpenCV module and load the image with
#import module import cv2 as cv #load image image = cv.imread("birds.jpg")
Now convert the
into the grayscale image, because we want to set the color intensity to binary black and white, so finding the edges around the object becomes easy. To convert the image into a greyscale image in OpenCV, we use the
#convert to gray scale gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY) #show grayscale image cv.imshow("Gray Image",gray_image ) cv.waitKey(0)
you will see the following grayscale image.
As you can see that now we have a grayscale image of our birds image. Converting the RGB color image to a black and white image is very important because it helps in finding the proper edges of individual objects.
After converting the image into grayscale, now let's detect edges and the contours of the grayscale image using OpenCV
#detect edges canny = cv.Canny(gray_image, 215, 275) #identify contours contours, hierarchies = cv.findContours(canny,cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
method accepts an image and two thresholds, intensities 215 and 275. The
method finds the number of contours present in the image and returns a tuple of two values
After finding the contours, let's draw the Contours on the original image with the help of
#draw contours on image cv.drawContours(image, contours, -1, (0,0,255), 2)
method accepts the
on which you want to draw the contour, the
list itself, the number of counters
represent all contours), BGR code
(Red), the intensity of contours (
). Now let's display the image with
#show contours outlining on image cv.imshow("Contours",image) #wait till infinity cv.waitKey(0)
The output will be:
In the above example, we draw the Contours on the original image. Now, let's create a black blank image and draw the same Contours on that blank Image.
Draw Contours on the Blank Image
To create a black blank image, we will use the Python NumPy library, so make sure Numpy is installed in your system.
import cv2 as cv import numpy as np image = cv.imread("birds.jpg") #blank matrix blank = np.zeros(image.shape, dtype='uint8') #convert to gray scale gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY) #detect edges canny = cv.Canny(gray_image, 215, 275) #identify contours contours, hierarchies = cv.findContours(canny,cv.RETR_LIST, cv.CHAIN_APPROX_NONE) #draw contours on blank image cv.drawContours(blank, contours, -1, (0, 255,0), 1) #show contours outlining on image cv.imshow("Contours on Blank",blank) #wait till infinity cv.waitKey(0)
In this Python tutorial, we learned how to detect Contours in an Image using the Python OpenCV library. You will be often detecting Contours for image segmentation, shape analysis, and object detection.
Here, in this tutorial, we have used the OpenCV edge detecting technique to detect the Contours of an image. However, there are also other methods to find the Contours in an image, such as hough transform and K-Means segmentation.
People are also reading:
- GitHub API in Python
- Extract Image Metadata in Python
- How to Install PIP on Windows, macOS, and Linux?
- Create Plots in Python
- SYN Flooding Attack in Python
- Python Google Page Ranking
- C++ vs Python
- How to Check if a File or Directory Exists in Python?
- Python Program to Check if a Number is Positive, Negative or 0
- How to Install Python 3 on Windows 10?