使用Hough变换,OpenCV和python进行平行线检测 [英] Parallel Line detection using Hough Transform, OpenCV and python

查看:993
本文介绍了使用Hough变换,OpenCV和python进行平行线检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一直在努力的算法方面的帮助.我正在尝试检测阈值图像中的所有线条,检测所有线条,然后仅输出平行的线条.阈值图像输出我感兴趣的对象,然后我通过Canny边缘检测器对该图像进行过滤.然后将该边缘图像通过概率霍夫变换.现在,我希望该算法能够检测任何图像中的平行线.我想通过尝试检测所有线条的坐标并计算它们的斜率(然后是角度)来做到这一点.平行线必须具有相同或几乎相同的角度,这样,我只能输出具有相同角度的线.我也许可以在图像中画一条假想的线,然后将其用作图像中所有检测到的线的参考?我只是不明白如何使用通过函数cv2.HoughLinesP()检测到的所有线的坐标.该函数的文档说输出是一个4D数组,这对我来说很混乱.这是我的代码的一部分:

I need help on an algorithm I've been working. I'm trying to detect all the lines in a thresholded image, detect all the lines and then output only those that are parallel. The thresholded image outputs the object of my interest, and then I filter this image through a canny edge detector. This edge image is then passed through the Probabilistic Hough Transform. Now, I want the algorithm to be capable of detecting parallel lines in any image. I had in mind to do this by trying to detect the coordinates of all the lines and calculate their slope (with this then the angle). Parallel lines must have the same or almost the same angle and in that way I could output only the lines with the same angle. I could maybe draw an imaginary line in the image and then use it as reference for all the detected lines in the image? I just don't understand how to use the coordinates of all the lines detected through the function cv2.HoughLinesP(). The documentation of this functions says that the output is a 4D array and this is confusing for me. This is a part of my code:

rho_res = .1 # [pixels]

theta_res = np.pi / 180. # [radians]

threshold = 50 # [# votes]

min_line_length = 100 # [pixels]

max_line_gap = 40 # [pixels]

lines = cv2.HoughLinesP(edge_image, rho_res, theta_res, threshold, np.array([]),

                        minLineLength=min_line_length, maxLineGap=max_line_gap)

画线

    if lines is not None:
                for i in range(0, len(linesP)):
                    coords = lines[i][0]
                    slope = (float(coords[3]) - coords[1]) / (float(coords[2]) - coords[0])
                    cv2.line(img, (coords[0], coords[1]), (coords[2], coords[3]), (0,0,255), 2, cv2.LINE_AA)

关于如何推断所有检测到的行,然后仅输出平行行的任何想法?我已经在线尝试了几种算法,但似乎都没有用.同样,我的问题是理解和使用函数cv2.HoughLinesP()的输出变量.我还找到了应该计算斜率的代码.我试过了,但是只是给我一个值(一个斜率).我想要图像中所有线条的斜率.

Any idea on how I could extrapolate all the detected lines and then output only those that are parallel? I have tried a few algorithms online but none seems to work. Again, my problem is understanding and working with the output variables of the function cv2.HoughLinesP(). I have also find a code that is supposed to calculate the slope. I tried this but is just giving me one value (one slope). I want the slope of all the lines in the image.

推荐答案

将Hough变换投影到角度轴上.这会给您提供一维信号,它是theta的函数,与该方向上的线量"成正比.该信号中的峰值表示具有许多平行线的方向.找到最大的峰,使您有θ.

Project the Hough transform onto the angle axis. This gives you a 1D signal as a function of theta, that is proportional to the "amount of line" in that orientation. Peaks in this signal indicate orientations that have many parallel lines. Find the largest peak, that gives you a theta.

现在返回到Hough变换图像,并检测具有此theta值的峰(可能允许一些摆动).现在,您将在该方向上具有所有平行线.

Now go back to the Hough transform image, and detect peaks with this value of theta (maybe allow a little bit of wiggle). Now you’ll have all parallel lines at this orientation.

对不起,我无法为您提供与cv2.HoughLinesP兼容的代码,我不知道此功能.我希望这个描述能为您提供一个起点.

Sorry I can’t give you code that works with cv2.HoughLinesP, I don’t know this function. I hope this description gives you a starting point.

这篇关于使用Hough变换,OpenCV和python进行平行线检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆