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 | Detect corner of an image using OpenCV
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- Detect an object with OpenCV-Python
- Detect Cat Faces in Real-Time using Python-OpenCV
- Log transformation of an image using Python and OpenCV
- Image Resizing using OpenCV | Python
- Python | Image blurring using OpenCV
- Image Processing without OpenCV | Python
- Image Registration using OpenCV | Python
- Reading an image in OpenCV using Python
- Image Translation using OpenCV | Python
- Image Pyramid using OpenCV | Python
- Cartooning an Image using OpenCV - Python
- Image Steganography using OpenCV in Python
- OpenCV Python Program to blur an image
- Negative transformation of an image using Python and OpenCV
- Python | OpenCV program to read and save an Image
- OpenCV Python Program to analyze an image using Histogram
- Find Circles and Ellipses in an Image using OpenCV | Python
- Create Local Binary Pattern of 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 firstname.lastname@example.org. 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.