Python opencv find pentagon contour on image

Question:

I am trying to read numbers from simple attached image.

enter image description here

For this I am trying to find pentagon which holds number. However when I am trying to find pentagon using opencv findcontour function it is not giving correct values. I tried various permutation with that function. None of that worked.

I have tried following so far:

import cv2 as cv
import numpy as np

im = cv.imread(r'out.jpg')
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 200, 255, 0)

contours, hierarchy = cv.findContours(thresh, cv.RETR_LIST  , cv.CHAIN_APPROX_SIMPLE)

for c in contours:
    print(len(c))

output:
1
1
1
1
1
1
1
1
38
36
1
1
85
87
128
133
55
47
4
4
7
4
4
4

None of this is 5, so above points can’t be pentagon.

can you please help me if I am making any mistake?

Asked By: Bhavesh Ghodasara

||

Answers:

You’re on the right track. After finding contours, you need to perform contour approximation using cv2.approxPolyDP + cv2.arcLength. You can check the return value from cv2.approxPolyDP which will give you the polygonal curve shape approximation. If this value is five then you can assume it is a pentagon. Here’s a simple approach:

  1. Obtain binary image. Load image, grayscale, bilateral filter, Otsu’s threshold

  2. Find contours and perform contour approximation. Find contours with cv2.findContours then perfrom contour approximation. If a contour passes this filter we extract the bounding rectangle coordinates with cv2.boundingRect and extract/save the ROI using Numpy slicing.


The detected ROI’s in teal

enter image description here

Extracted/saved ROIs

enter image description here

Note: There are two ROIs saved as individual images but they are the same.

Code

import cv2
import numpy as np

# Load image, grayscale, bilaterial filter, Otsu's threshold
image = cv2.imread('1.jpg')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.bilateralFilter(gray,9,75,75)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Find contours, perform contour approximation, and extract ROI
ROI_num = 0
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.04 * peri, True)
    # If has 5 then its a pentagon
    if len(approx) == 5:
        x,y,w,h = cv2.boundingRect(approx)
        cv2.rectangle(image, (x, y), (x + w, y + h), (200,255,12), 2)
        ROI = original[y:y+h, x:x+w]
        cv2.imwrite('ROI_{}.png'.format(ROI_num), ROI)
        ROI_num += 1

cv2.imshow('thresh', thresh)
cv2.imshow('ROI', ROI)
cv2.imshow('image', image)
cv2.waitKey()
Answered By: nathancy