我正在尝试使用背景检测标志的轮廓
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()
但是我得到的结果并不理想。
预处理图像以寻找轮廓的最佳方法是什么?
如果在处理之前调整图像的大小,则会得到更多的轮廓。
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)
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)
输出:
具有调整大小的Canny图像:
没有调整大小的Canny图片: