Python:为findContours预处理图像

问题描述 投票:0回答:1

我正在尝试使用背景检测标志的轮廓

canny = cv2.Canny(np.asarray(out_gray), 50, 200)
_, contours, _ = cv2.findContours(canny.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image=out, contours=contours, contourIdx=-1, color=(255, 0, 0), thickness=20)
plt.imshow(out)
plt.show()

但是我得到的结果并不理想。

enter image description here

预处理图像以寻找轮廓的最佳方法是什么?

python opencv opencv-contour
1个回答
0
投票

如果在处理之前调整图像的大小,则会得到更多的轮廓。

import cv2
import numpy as np

img = cv2.imread('flag.png')
img = cv2.resize(img, (1200, 700)) # <---- resize here!
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(gray, 127, 255, 0)
canny = cv2.Canny(np.asarray(gray), 0, 200)

_, contours, _ = cv2.findContours(canny.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)


cv2.drawContours(image=img, contours=contours, contourIdx=-1, color=(255, 255, 0), thickness=5)

cv2.imshow('asd', img)
cv2.waitKey(0)

输出:

enter image description here

具有调整大小的Canny图像:

enter image description here

没有调整大小的Canny图片:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.