I have the following Image result.png with Countours drawn at every rectangle:
In the next step I am trying to extract only the inner portion of these rectangles to get the images which has centralised digits(2, 0, 1, 8). The code I am using is as follows:
import cv2
BLACK_THRESHOLD = 200
THIN_THRESHOLD = 10
im = cv2.imread('result.png', 0)
ret, thresh = cv2.threshold(im, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, 1, 3)
idx = 0
for cnt in contours:
idx += 1
x, y, w, h = cv2.boundingRect(cnt)
roi = im[y:y + h, x:x + w]
if h < THIN_THRESHOLD or w < THIN_THRESHOLD:
continue
cv2.imwrite(str(idx) + '.png', roi)
cv2.rectangle(im, (x, y), (x + w, y + h), (200, 0, 0), 2)
cv2.imshow('img', im)
cv2.waitKey(0)
It is working but it is giving every possible cropped image on the basis of the Countours created as shown below(different image at each row):
It is extracting 18 Images whereas I only want Images which has digits in its center without any kind of noise. Can anyone help me how to narrow this extraction process?
CV_RETR_EXTERNAL
in the findContours function