Contour Detection using Python OpenCV
In this article, you will learn contour detection of an image using Python OpenCV.
Contours are defined as the lines joining all continuous points along the boundary of an image that have the same intensity and color. Contours are basically used in shape analysis, finding the size of the object of interest, object detection, motion detection, and recognition. Contours can do a bit more than just identify edges. The algorithm does really find edges of images but also puts them in a hierarchy.
Required Modules for Contour Detection
We can easily find and draw contours in images using Python OpenCV.
- OpenCV (cv2)
- Matplotlib
We hope the above modules are installed in your system. If you do not have it installed, you can install it using the pip tool-
pip install opencv-contrib-python matplotlib
Detecting and Drawing Contour Code Explanation
Here, we have explained the contour detection and draw code line by line.
First, we load the image using the OpenCV cv2.imread() function.
image = cv2.imread("house.jpg")
Next, we convert the imported image to grayscale. The cv2.cvtColor() method is used to convert an image from one color-space to another. It specifies the type of conversion. We need to convert to grayscale, so we will use cv2.COLOR_BGR2GRAY in the second parameter.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
It is necessary to create a binary image before contour detection. A binary image has each pixel either in black or white. In OpenCV, finding the contour in this binary image is like finding a white object in a black background. It gives us better accuracy.
So, we will create a binary threshold image. It disables the pixels having a value of less than 255 and enables the pixels having a value of more than 255.
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
Next, we will find the contour from the generated binary threshold image. OpenCV provides the cv2.findContours() function to create contours. It accepts three parameters, input image, hierarchy type, and the contour approximation type. The cv2.RETR_TREE gets the entire hierarchy of contours in the image and establishes a relationship between them. The cv2.CHAIN_APPROX_SIMPLE removes all redundant points and compresses the contour, thereby saving memory.
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
The cv2.drawContours() method is used to draw all contours.
image = cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
Complete Code: OpenCV Contour Detection and Drawing
import cv2
from matplotlib import pyplot as plt
# loading the image
image = cv2.imread("house.jpg")
# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# create a binary thresholded image
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
# contours from the thresholded image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# draw all contours
image = cv2.drawContours(image, contours, -1, (255, 0, 0), 4)
plt.subplot(121),plt.imshow(binary, cmap="gray")
plt.subplot(122),plt.imshow(image)
plt.show()
Output of the above code:
Example2: OpenCV Contour Detection and Drawing
import cv2
from matplotlib import pyplot as plt
# loading the image
image = cv2.imread("salad.jpg")
# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# create a binary thresholded image
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
# contours from the thresholded image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# draw all contours
image = cv2.drawContours(image, contours, -1, (255, 0, 0), 4)
plt.subplot(121),plt.imshow(binary, cmap="gray")
plt.subplot(122),plt.imshow(image)
plt.show()
Output of the above code:
Related Articles
Capture a video in Python OpenCV and saveOpenCV Logical Operators
Arithmetic Operations on Images using Python OpenCV
OpenCV color detection
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples
Draw different shapes on image using Python OpenCV
How to convert MySQL query result to JSON in Python
How to display data from MongoDB in HTML table using Python