Approach : The approach we would be used to detect the shape of a given polygon will be based on classifying the detected shape on the basis of a number of sides it has. For example, if the detected polynomial has 3 sides, then it could be considered as a triangle, if the polynomial has 4 sides then it could be classified as a square or a rectangle.
- Make sure you have Python3, OpenCV, numpy already installed on your computer.
- Basic knowledge about OpenCV would be helpful – Basics of OpenCV
- Make sure to save the image in which shapes is to be detected in your local directory
Implementation : In the following code, we will be detecting an arrow-shaped object from the image ‘arrow.jpg’. The shape will be detected on the basis of the number of sides it has
Code: Python program to detect polygons in an image
Note : The parameter ‘110’ in threshold could be adjusted as per need if the object is of different colour and is based on trial and error.
Image with arrow
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- Python | Detect corner of an image using OpenCV
- Detect Cat Faces in Real-Time using Python-OpenCV
- Detect the RGB color from a webcam using Python - OpenCV
- Make filled polygons between two curves in Python using Matplotlib
- Make filled polygons between two horizontal curves in Python using Matplotlib
- Detect an object with OpenCV-Python
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Python | Ellipse (different polygons) in Kivy
- Python Bokeh - Plotting Multiple Polygons on a Graph
- Image resizing using Seam carving using OpenCV in Python
- Transition from OpenCV 2 to OpenCV 3.x
- Cartooning an Image using OpenCV - Python
- OpenCV Python Program to analyze an image using Histogram
- Reading an image in OpenCV using Python
- Image Pyramid using OpenCV | Python
- Negative transformation of an image using Python and OpenCV
- Python | Image blurring using OpenCV
- Image Resizing using OpenCV | Python
- Find Circles and Ellipses in an Image using OpenCV | Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.