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
opencv-python
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
birds.jpg
image.
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
cv.imread()
method.
#import module
import cv2 as cv
#load image
image = cv.imread("birds.jpg")
Now convert the
image
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
cvtColor(image, cv.COLOR_BGR2GRAY)
method.
#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
cv.Canny()
, and
cv.findContours()
method.
#detect edges
canny = cv.Canny(gray_image, 215, 275)
#identify contours
contours, hierarchies = cv.findContours(canny,cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
The
cv.Canny()
method accepts an image and two thresholds, intensities 215 and 275. The
cv.findContours()
method finds the number of contours present in the image and returns a tuple of two values
contours
list and
hierarchies
.
After finding the contours, let's draw the Contours on the original image with the help of
drawContours()
method.
#draw contours on image
cv.drawContours(image, contours, -1, (0,0,255), 2)
The
drawContours()
method accepts the
image
on which you want to draw the contour, the
contours
list itself, the number of counters
(-1
represent all contours), BGR code
(0,0,255)
(Red), the intensity of contours (
2
). Now let's display the image with
cv.imshow()
method
#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)
Output
Conclusion
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?
Leave a Comment on this Post