How to detect polygons in image using OpenCV Python?
OpenCV (Open Source Computer Vision) is an open source library of computer vision and machine learning algorithms. It provides a comprehensive toolkit for image and video analysis. In this article, we will be looking at how to detect polygons in images using OpenCV Python.
The first step to do this is to install OpenCV Python. We can install it using pip:
pip install opencv-python
Once we have installed OpenCV Python, we can start by importing the necessary libraries:
import cv2
import numpy as np
Now that we have imported the libraries, we can load the image we want to detect polygons in. We can use the imread() function to load the image:
img = cv2.imread('image.jpg')
After loading the image, we can convert it to grayscale using the cvtColor() function:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
The next step is to detect the edges in the image. We can do this using the Canny edge detection algorithm:
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
Now that we have detected the edges in the image, we can find the contours using the findContours() function:
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
The findContours() function returns a list of contour points and a hierarchy of the contours. We can use the drawContours() function to draw the contours on the image:
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
The final step is to identify the polygons in the image. We can do this by approximating the contours using the approxPolyDP() function and checking the number of sides in the polygon. For example, to detect a square, we can check if the polygon has 4 sides:
for cnt in contours:
approx = cv2.approxPolyDP(cnt, 0.01 * cv2.arcLength(cnt, True), True)
if len(approx) == 4:
cv2.drawContours(img, [approx], 0, (0, 0, 255), 3)
In the above code, we first approximate the contour using the approxPolyDP() function with a tolerance of 1% of the contour perimeter. We then check if the number of sides in the polygon is 4 using the len() function. Finally, we draw the contour on the image with a red color.
Here is the complete code to detect polygons in an image using OpenCV Python:
import cv2
import numpy as np
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
for cnt in contours:
approx = cv2.approxPolyDP(cnt, 0.01 * cv2.arcLength(cnt, True), True)
if len(approx) == 4:
cv2.drawContours(img, [approx], 0, (0, 0, 255), 3)
cv2.imshow('image', img)
cv2.waitKey(0)
Conclusion
In this article, we have learned how to detect polygons in an image using OpenCV Python. We have seen how to load the image, convert it to grayscale, detect the edges, find the contours, and identify the polygons in the image. OpenCV Python provides a powerful platform for image and video analysis, and we have only scratched the surface of its capabilities here. With the tools we have learned in this article, we can build more advanced applications to analyze and process images and videos.