Updated April 19, 2023
Introduction to OpenCV ApproxPolyDP
The process of approximation of a shape of contour to another shape consisting of a lesser number of vertices in such a way that the distance between the contours of shapes is equal to the specified precision or lesser than the specified precision is called approximation of a shape of the contour. And a built-in function in OpenCV is used to approximate the shape of polygonal curves to the specified precision called approxPolyDP() function and approxPolyDP() function returns the approximated contour whose shape is the same as the input curve and approximation of a shape of the contour is widely used in the area of robotics to classify the patterns and analysis of scenes.
The syntax to define approxPolyDP() function in OpenCV is as follows:
approxPolyDP(input_curve, epsilon, closed)
where input_curve represents the input polygon whose contour must be approximated with specified precision,
epsilon represents the maximum distance between the approximation of a shape contour of the input polygon and the original input polygon and
closed is a Boolean value whose value is true if the approximated curve is closed or the value is false if the approximated curve is not closed.
Working of approxPolyDP() function in OpenCV
- The process of approximating the shape of a contour of a given polygon to the shape of the original polygon to the specified precision is called approximation of a shape of the contour.
- We make use of a function in OpenCV called approxPolyDP() function to perform an approximation of a shape of a contour.
- The image of a polygon whose shape of a contour must be approximated is read using the imread() function.
- Then the input image is converted into a grayscale image.
- Then thresholding function is applied on the grayscale image to convert it into a binary image.
- Then the contours present in the image are determined using findContours() function.
- Then for each of the contours determined in the image, the approxPolyDP() function is applied to determine the shape of the polygons present in the image.
- Then the determined shape of the contours is drawn on the image using drawContours() function and then displayed as the output of the program.
Examples of OpenCV ApproxPolyDP
Here are the follwoing examples mention below
Example #1
OpenCV program in python to determine the shape of the polygons in a given image by using approxPolyDP() function name the detected shapes and then display the resulting image as the output on the screen:
Code:
#importing the module cv2
import cv2
#reading the image whose shape is to be detected using imread() function
imageread = cv2.imread('C:/Users/admin/Desktop/Images/triangle.png')
#converting the input image to grayscale image using cvtColor() function
imagegray = cv2.cvtColor(imageread, cv2.COLOR_BGR2GRAY)
#using threshold() function to convert the grayscale image to binary image
_, imagethreshold = cv2.threshold(imagegray, 245, 255, cv2.THRESH_BINARY_INV)
#finding the contours in the given image using findContours() function
imagecontours, _ = cv2.findContours(imagethreshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#for each of the contours detected, the shape of the contours is approximated using approxPolyDP() function and the contours are drawn in the image using drawContours() function
for count in imagecontours:
epsilon = 0.01 * cv2.arcLength(count, True)
approximations = cv2.approxPolyDP(count, epsilon, True)
cv2.drawContours(imageread, [approximations], 0, (0), 3)
#the name of the detected shapes are written on the image
i, j = approximations[0][0]
if len(approximations) == 3:
cv2.putText(imageread, "Triangle", (i, j), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
elif len(approximations) == 4:
cv2.putText(imageread, "Rectangle", (i, j), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
elif len(approximations) == 5:
cv2.putText(imageread, "Pentagon", (i, j), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
elif 6 < len(approximations) < 15:
cv2.putText(imageread, "Ellipse", (i, j), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
else:
cv2.putText(imageread, "Circle", (i, j), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
#displaying the resulting image as the output on the screen
cv2.imshow("Resulting_image", imageread)
cv2.waitKey(0)
The output of the given program is shown in the snapshot below:
In the above program, the required module cv2 is imported. Then we are reading the image whose shape is to be detected using the imread() function. Then we are converting the input image to a grayscale image using the cvtColor() function. Then we are using the threshold() function to convert the grayscale image to a binary image. Then we are finding the contours in the given image using findContours() function. Then for each of the contours detected, the shape of the contours is approximated using the approxPolyDP() function and the contours are drawn in the image using drawContours() function. Then we are writing the name of the detected shapes on the image. Then the resulting image is displayed as the output on the screen.
Example #2
OpenCV program in python to determine the shape of the polygons in a given image by using approxPolyDP() function name the detected shapes and then display the resulting image as the output on the screen:
Code:
#importing the module cv2
import cv2
#reading the image whose shape is to be detected using imread() function
imageread = cv2.imread('C:/Users/admin/Desktop/Images/pentagon.png')
#converting the input image to grayscale image using cvtColor() function
imagegray = cv2.cvtColor(imageread, cv2.COLOR_BGR2GRAY)
#using threshold() function to convert the grayscale image to binary image
_, imagethreshold = cv2.threshold(imagegray, 245, 255, cv2.THRESH_BINARY_INV)
#finding the contours in the given image using findContours() function
imagecontours, _ = cv2.findContours(imagethreshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#for each of the contours detected, the shape of the contours is approximated using approxPolyDP() function and the contours are drawn in the image using drawContours() function
for count in imagecontours:
epsilon = 0.01 * cv2.arcLength(count, True)
approximations = cv2.approxPolyDP(count, epsilon, True)
cv2.drawContours(imageread, [approximations], 0, (0), 3)
#the name of the detected shapes are written on the image
i, j = approximations[0][0]
if len(approximations) == 3:
cv2.putText(imageread, "Triangle", (i, j), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
elif len(approximations) == 4:
cv2.putText(imageread, "Rectangle", (i, j), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
elif len(approximations) == 5:
cv2.putText(imageread, "Pentagon", (i, j), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
elif 6 < len(approximations) < 15:
cv2.putText(imageread, "Ellipse", (i, j), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
else:
cv2.putText(imageread, "Circle", (i, j), cv2.FONT_HERSHEY_COMPLEX, 1, 0, 2)
#displaying the resulting image as the output on the screen
cv2.imshow("Resulting_image", imageread)
cv2.waitKey(0)
The output of the given program is shown in the snapshot below:
In the above program, the required module cv2 is imported. Then we are reading the image whose shape is to be detected using the imread() function. Then we are converting the input image to a grayscale image using the cvtColor() function. Then we are using the threshold() function to convert the grayscale image to a binary image. Then we are finding the contours in the given image using findContours() function. Then for each of the contours detected, the shape of the contours is approximated using the approxPolyDP() function and the contours are drawn in the image using drawContours() function. Then we are writing the name of the detected shapes on the image. Then the resulting image is displayed as the output on the screen.
Recommended Articles
We hope that this EDUCBA information on “OpenCV ApproxPolyDP” was beneficial to you. You can view EDUCBA’s recommended articles for more information.