在 OpenCV 中从图像中提取轮廓 - Python

Aad*_*dit 5 python opencv image-processing contour

我有以下图像result.png,在每个矩形处绘制了 Countours:

在此输入图像描述

在下一步中,我尝试仅提取这些矩形的内部部分以获得具有集中数字(2,0,1,8)的图像。我使用的代码如下:

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)
Run Code Online (Sandbox Code Playgroud)

它正在工作,但它根据创建的 Countours 给出每个可能的裁剪图像,如下所示(每行都有不同的图像):

在此输入图像描述

它正在提取18 张图像,而我只想要中心有数字且没有任何噪音的图像。谁能帮助我如何缩小这个提取过程?