如何在 python 中使用 opencv 检测图像中的垂直边缘

How to detect vertical edges in an image using opencv in python

我想检测甘蔗棒中的接头但不希望检测到边界线 这是我的原创 Original Image, and after applying Morphological Gradient I got this output image. The desired output that I need is to detect only vertical edges that is the joints, the desired output are only the red lines in this image.

所以,如果有人能帮助我,那就太好了!

您可以尝试增强以下代码以根据需要查找垂直边缘

img = cv2.imread("edges.png")
mask = img[:,:,0]

height, width = mask.shape

mask = cv2.threshold(mask, 100, 255, cv2.THRESH_BINARY)[1]
#cv2.imshow("mask", mask)

vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, height//30))
vertical_lines = cv2.erode(mask, vertical_kernel, iterations=1)
vertical_lines = cv2.dilate(vertical_lines, vertical_kernel, iterations=1)

#cv2.imshow("vertical_lines", vertical_lines)

img[vertical_lines > 0, 2] = 255
cv2.imshow("img", img)
cv2.waitKey(0)