Updated April 10, 2023
Introduction to OpenCV drawcontours
Given an image, the line drawn along the boundary of the image by joining all the points forming the boundary of the image having the same intensity are called contours in OpenCV. Contours are used to perform analysis of images like analysis of shapes, detection of size, detection of object etc. And in order to extract contours from a given image we have a function in OpenCV called findcontours() function using which the shapes present in the images can be found and we have a function in OpenCV called drawcontours() function to draw the contours in the image which returns an image with the contours drawn on it.
Syntax to define drawcontours() function in OpenCV:
drawcontours(source_image, contours, contours_ID, contour_color, contour_thickness)
Where,
- source_image is the image on which the contours must be drawn.
- contours are the contours extracted from a given image using findcontours() function.
- contour_ID is the parameter that determines the contour to be drawn and a negative contour_ID value indicates that all the contours must be drawn.
- contour_color represents the contours color.
- contour_thickness represents the thickness of the lines forming the contours.
Working of drawcontours() Function in OpenCV
- In order to perform analysis of images like analysis of shapes, detection of size, detection of object etc., we make use of contours in OpenCV.
- Contours are the points around the boundary of a given image formed by joining them together into lines.
- The contours in a given image can be extracted by using a function called findcontours() function in OpenCV.
- The findcontours() function returns the number of contours in a given image.
- To draw the contours in a given image, we make use of a function called drawcontours() function.
- The drawcontours() function takes the contours extracted by using findcontours() function and draws the contours in the given image.
- The drawcontours() function returns the image with contours drawn on it.
Examples of OpenCV drawcontours
Given below are the examples of OpenCV drawcontours:
Example #1
OpenCV program in python to demonstrate drawcontours() function to draw contours in the given image by finding the contours using findcontours() function and then display the image with contours drawn on it as the output on the screen.
Code:
#importing the module cv2
import cv2
#reading the image on which contours must be drawn
imageread = cv2.imread('C:/Users/admin/Desktop/educba.jpg')
#converting the image to gray image using cvtColor() function
imagegray = cv2.cvtColor(imageread, cv2.COLOR_BGR2GRAY)
#finding the edges in the image using canny() function
imageedges = cv2.Canny(imagegray, 10, 100)
#finding the contours in the image using findcontours() function
contours, hierarchy = cv2.findContours(imageedges,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#drawing the contours in the image using drawContours() function
cv2.drawContours(imageread, contours, -1, (0, 0, 255), 6)
cv2.drawContours(imageread, contours, -1, (0, 0, 255), 6)
#displaying the image with contours as the output on the screen
cv2.imshow('Image_With_Contours', imageread)
cv2.waitKey(0)
Output:
In the above program, we are importing the module cv2. Then we are reading the image on which contours must be drawn, using imread() function. Then we are making use of cvtColor() function to covert the given image to gray image. Then we are making use of canny() function to determine the edges in the image. Then we are making use of findContours() function to extract the contours from the given image. Then we are making use of drawContours() function to draw the contours in the image and display it as the output on the screen.
Example #2
OpenCV program in python to demonstrate drawcontours() function to draw contours in the given image by finding the contours using findcontours() function and then display the image with contours drawn on it as the output on the screen.
Code:
#importing the module cv2
import cv2
#reading the image on which contours must be drawn
imageread = cv2.imread('C:/Users/admin/Desktop/logo.png')
#converting the image to gray image using cvtColor() function
imagegray = cv2.cvtColor(imageread, cv2.COLOR_BGR2GRAY)
#finding the edges in the image using canny() function
imageedges = cv2.Canny(imagegray, 10, 100)
#finding the contours in the image using findcontours() function
contours, hierarchy = cv2.findContours(imageedges,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#drawing the contours in the image using drawContours() function
cv2.drawContours(imageread, contours, -1, (0, 255, 0), 6)
cv2.drawContours(imageread, contours, -1, (0, 255, 0), 6)
#displaying the image with contours as the output on the screen
cv2.imshow('Image_With_Contours', imageread)
cv2.waitKey(0)
Output:
In the above program, we are importing the module cv2. Then we are reading the image on which contours must be drawn, using imread() function. Then we are making use of cvtColor() function to covert the given image to gray image. Then we are making use of canny() function to determine the edges in the image. Then we are making use of findContours() function to extract the contours from the given image. Then we are making use of drawContours() function to draw the contours in the image and display it as the output on the screen.
Example #3
OpenCV program in python to demonstrate drawcontours() function to draw contours in the given image by finding the contours using findcontours() function and then display the image with contours drawn on it as the output on the screen.
Code:
#importing the module cv2
import cv2
#reading the image on which contours must be drawn
imageread = cv2.imread('C:/Users/admin/Desktop/tree.jpg')
#converting the image to gray image using cvtColor() function
imagegray = cv2.cvtColor(imageread, cv2.COLOR_BGR2GRAY)
#finding the edges in the image using canny() function
imageedges = cv2.Canny(imagegray, 10, 100)
#finding the contours in the image using findcontours() function
contours, hierarchy = cv2.findContours(imageedges,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#drawing the contours in the image using drawContours() function
cv2.drawContours(imageread, contours, -1, (255, 0, 0), 6)
cv2.drawContours(imageread, contours, -1, (255, 0, 0), 6)
#displaying the image with contours as the output on the screen
cv2.imshow('Image_With_Contours', imageread)
cv2.waitKey(0)
Output:
In the above program, we are importing the module cv2. Then we are reading the image on which contours must be drawn, using imread() function. Then we are making use of cvtColor() function to covert the given image to gray image. Then we are making use of canny() function to determine the edges in the image. Then we are making use of findContours() function to extract the contours from the given image. Then we are making use of drawContours() function to draw the contours in the image and display it as the output on the screen.
Recommended Articles
We hope that this EDUCBA information on “OpenCV drawcontours” was beneficial to you. You can view EDUCBA’s recommended articles for more information.