Polygon detection and classification using OpenCV contour analysis.
- Convert input image to grayscale and apply binary thresholding
- Find contours with
cv2.findContours - Filter out small contours (noise) by area
- Approximate each contour to a polygon using the Douglas-Peucker algorithm
- Classify by vertex count and geometry:
- 3 vertices → Triangle
- 4 vertices → Square or Rectangle (distinguished by aspect ratio)
- 5 vertices → Pentagon
- 6 vertices → Hexagon
- More → Circle
- Draw each shape with a distinct color and label
pip install opencv-python numpy
python simple_shape_detection.py # defaults to polygons.png
python simple_shape_detection.py myimage.png # custom image- OpenCV
- NumPy
MIT
