How to draw contours on squares drawn with pencil (Python)

asked 2019-12-19 15:58:59 -0600

updated 2019-12-19 18:25:25 -0600

supra56 gravatar image

Code:

import cv2

img = cv2.imread('media/multi-rec.jpg')
img = cv2.resize(img, (414, 732))

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

edges_high_thresh = cv2.Canny(image=gray, threshold1=60, threshold2=120)
contours, _ = cv2.findContours(image=edges_high_thresh,
                               mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    area = cv2.contourArea(cnt)
    peri = cv2.arcLength(curve=cnt, closed=True)
    approx = cv2.approxPolyDP(curve=cnt,
                              epsilon=0.01*peri,
                              closed=True)
    if area > 100 and len(approx) == 4:
        cv2.drawContours(image=img, contours=[approx],
                         contourIdx=-1, color=(0, 255, 0), thickness=1)

cv2.imshow('Image edges', edges_high_thresh)
cv2.waitKey(0)
cv2.imshow('Image', img)
cv2.waitKey(0)

Edges Draw found contours

edit retag flag offensive close merge delete

Comments

(Is the problem really _drawing_ or _detecting_ the squares...)

mvuori gravatar imagemvuori ( 2019-12-20 02:05:00 -0600 )edit

@gytbal5. I merely manged got 3 so far.

supra56 gravatar imagesupra56 ( 2019-12-20 14:47:50 -0600 )edit