我的代码是这样的:
import numpy as np
import cv2
import math
# prepare the image
image = cv2.imread("image")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3,3), 0)
ret3,thesh = cv2.threshold(blurred,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# line detection
rho=1
theta=math.pi/180
thresh = 10
minLength= 30
maxGap= 5
lines = cv2.HoughLinesP(th3.copy(), 1, rho, thresh, minLength, maxGap)
img = image.copy()
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(image,(x1,y1),(x2,y2),(255,0,0),1)
似乎 HoughLinesP 无法检测到水平线,无论我扭曲了上面的参数值是什么。有没有办法同时检测水平线和垂直线?
非常感谢!
最佳答案
这里有错误的 HoughLinesP 参数。正确的应该是:
lines = cv2.HoughLinesP(th3, rho, theta, thresh, None, minLength, maxGap)
关于opencv - 在 opencv 中使用 HoughLines 进行线检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56079716/