使用 OpenCV 进行灰度图像中的条纹边框或轮廓检测

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

我有一个灰度图像,我尝试了一些方法,例如 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)

python opencv image-processing
1个回答
0
投票

如果您知道深色条纹是水平的,检查每行的平均值可能会有所帮助。

下图只是绘制了每一行的平均值。 (正弦数据是一维的,这个图的宽度没有意义)。

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