Detect Polygon Shape in Image Using OpenCV in Python

In this tutorial, we will introduce the way to detect polygen shpaes in an image using python opencv.

1. Import library

import cv2
import numpy as np

2. Read an image

img = cv2.imread('image.jpg')  #read image from system
cv2.imshow('original', img)    #Displaying original image
cv2.waitKey(0)

3. Convert image to grayscale image

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

4. Find contours in an image

edged = cv2.Canny(gray, 170, 255)  
(contours,_) = cv2.findContours(edged,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

5. Create a function to determine type of shapes

def detectShape(cnt):      
       shape = 'unknown' 
       peri=cv2.arcLength(cnt,True) 
       vertices = cv2.approxPolyDP(cnt, 0.02 * peri, True)
       sides = len(vertices) 
       if (sides == 3): 
            shape='triangle' 
       elif(sides==4): 
             x,y,w,h=cv2.boundingRect(cnt)
             aspectratio=float(w)/h 
             if (aspectratio==1):
                   shape='square'
             else:
                   shape="rectangle" 
       elif(sides==5):
            shape='pentagon' 
       elif(sides==6):
            shape='hexagon' 
       elif(sides==8): 
            shape='octagon' 
       elif(sides==10): 
            shape='star'
       else:
           shape='circle' 
       return shape

6. Detect shapes in an image

for cnt in contours:
    moment=cv2.moments(cnt) 
    cx = int(moment['m10'] / moment['m00']) 
    cy = int(moment['m01'] / moment['m00']) 
    shape=detectShape(cnt) 
    cv2.drawContours(img,[cnt],-1,(0,255,0),2)
    cv2.putText(img,shape,(cx,cy),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0),2)  #Putting name of polygon along with the shape 
    cv2.imshow('polygons_detected',img) 
cv2.waitKey(0) 
cv2.destroyAllWindows()