【问题标题】:Houghlines no detect all lines opencv+pythonHoughlines 没有检测到所有行 opencv+python
【发布时间】:2015-03-12 18:30:20
【问题描述】:

我真的需要帮助,我尝试了很多天知道如何在 python 中使用 OpenCV 的 houghlines 函数。

该函数不会检测所有行,即使是阈值的结果(如果它正在显示)。我有一些图像和我正在使用的部分代码。非常感谢您的帮助。对不起我的英语。

    blur = cv2.GaussianBlur(mask,(5,5),0)
    ret,edges = cv2.threshold(mask,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    kernel = np.ones((3,3),np.uint8)
    image = cv2.erode(edges,kernel,iterations = 1)

    lines = cv2.HoughLines(image,1,np.pi/180,105)
    for rho,theta in lines[0]:
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a*rho
        y0 = b*rho
        x1 = int(x0 + 1000*(-b))
        y1 = int(y0 + 1000*(a))
        x2 = int(x0 - 1000*(-b))
        y2 = int(y0 - 1000*(a))
        cv2.line(img,(x1,y1),(x2,y2),(0,0,255),1)

生成的图像here

阈值图像here

【问题讨论】:

  • 试试HoughLinesP,而不是std::vector<cv::Vec4i> lines; cv::HoughLinesP(inThres, lines, 1, CV_PI/720.0, 10, 20, 10);,这给了我大部分的台词
  • 是的!非常感谢您的回复!但是,你能解释一下参数的修改吗?我还有一个问题,如何避免在线条上画线,即在线条上只画一条线,因为 houghlines 总是把两条线画在一起

标签: python opencv lines hough-transform


【解决方案1】:

是因为 for bucle 在lines[0] -> [0] 你必须试试这个:

for i in  range(0,lines.shape[0]):
    rho,thetha = lines[i,0]
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho`
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))
    cv2.line(img,(x1,y1),(x2,y2),(0,0,255),1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多