I'm doing an image processing inside the QR code finder patter. The problem is I can't perfectly crop the inner black square inside finder pattern. It's always have some white space around the black or the crop is inside the black square. The QR image is below
I find the inner black square using cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE), calculate the arcLength, if it is a 4 arcLength then I count the contourArea and check if it is a square using ratio. The complete function is below.
contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
fps = []
if hierarchy is not None:
hierarchy = hierarchy[0]
for i, c in enumerate(contours):
approx = cv2.approxPolyDP(c, 0.02 * cv2.arcLength(c, True), True)
if len(approx) == 4:
area = cv2.contourArea(c)
x, y, w, h = cv2.boundingRect(approx)
cv2.drawContours(img, [approx], -1, (0,0,255), 2)
if area < 500 or area > 5000:
continue
ratio = w / float(h)
if ratio < 0.8 or ratio > 1.2:
continue
child_count = 0
k = i
while hierarchy[k][2] != -1: # cek anak
child_count += 1
k = hierarchy[k][2]
if child_count == 1:
rect = cv2.boundingRect(approx)
fps.append(c) # (x,y,w,h)
If I run the script, only one square is collected. And it is not perfectly crop to only the square. I still have some white area around the square. See below
Is there someone can help me with this situation? Any possible solution will really help me.

