天天看點

python提取圖像輪廓_從OpenCV中的圖像中提取輪廓 - Python

我有以下Image result.png ,每個矩形都繪制了Countours:

python提取圖像輪廓_從OpenCV中的圖像中提取輪廓 - Python

在下一步中,我試圖僅提取這些矩形的内部部分以獲得具有集中數字(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)

它正在工作,但它根據如下所示建立的Countours提供每個可能的裁剪圖像(每行不同的圖像):

python提取圖像輪廓_從OpenCV中的圖像中提取輪廓 - Python

它正在提取 18 Images 而我隻想要在其中心有數字而沒有任何噪音的圖像 . 任何人都可以幫助我縮小這個提取過程嗎?