小编Aad*_*dit的帖子

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

我有以下图像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) …
Run Code Online (Sandbox Code Playgroud)

python opencv image-processing contour

5
推荐指数
0
解决办法
1万
查看次数

标签 统计

contour ×1

image-processing ×1

opencv ×1

python ×1