【发布时间】:2021-09-13 13:39:35
【问题描述】:
我有以下图片: Original Image
我想检测下图中以红色显示的 3 个垂直边缘: Desired output
我尝试了以下方法:
#green is the original image
gray = cv2.cvtColor(green, cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray green" ,gray)
ret, thresh1 = cv2.threshold(gray, 140, 255, cv2.THRESH_BINARY_INV)
cv2.imshow("140 Thresh", thresh1)
edges = cv2.Canny(thresh1,100,250,apertureSize = 7)
cv2.imshow("Edges", edges)
canny 边缘检测器显示以下输出:Canny result
我尝试过如下使用sobel垂直检测:
sobel_vertical = cv2.Sobel(thresh1, cv2.CV_64F, 1, 0, ksize=7)
cv2.imshow("Vertical", sobel_vertical)
这是 sobel 垂直输出:Sobel Vertical output
按照this的回答,我尝试了同样的代码,如下:
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=1.4,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=10)
a,b,c = lines.shape
for i in range(a):
cv2.line(green, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 1, cv2.LINE_AA)
cv2.imshow("Lines", green)
这给了我这个输出:Houghlines output
任何人都可以建议我,我做错了什么,或者我可以做些什么来获得所需的输出,或者接近那个。我根本不希望检测到水平边缘。谢谢!
【问题讨论】:
-
对于从 Houghlines 输出中获得的线,您可以计算每条线的斜率或倾角,并且您可以应用一个条件,即仅显示那些斜率在给定范围内的线说0到90度
-
感谢@AtharvaGundawar,它成功了。必须说,这是你的非常基本但聪明的想法。请将此作为答案,以便我接受并关闭此问题。
标签: python opencv image-processing