我有一个灰度图像,我尝试了一些方法,例如 findContour 和 HoughLinesP 来检测所附图像中的条纹。输出未反映预期的检测。我的示例代码之一如下,使用 findContour。但是,该脚本会检测条纹区域之外的其他区域。
如何正确检测条纹(轮廓/边框)?
import cv2
import imutils
img = cv2.imread('image.png', cv2.IMREAD_GRAYSCALE)
img = cv2.GaussianBlur(img, (5, 5), 0)
ret,thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
cnts = cv2.findContours(image=thresh, mode=cv2.RETR_EXTERNAL,
method=cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
img_col = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
image_copy = img_col.copy()
cv2.drawContours(image=image_copy, contours=[c], contourIdx=-1,
color=(0, 255, 0), thickness=1, lineType=cv2.LINE_AA)
# see the results
cv2.imshow('Output', image_copy)
cv2.waitKey(0)