Selecting lines from Hough lines

I am using Hough Lines to define the angle for this image. I am planning to find the intersection of the lines as an angle. This is an image. enter image description here

Unfortunately Hough returns many rows for each row I expect enter image description here

How do I adjust the Hough lines so that only one line matches the actual line in the image?

+3


source to share


4 answers


Collect the intersection of the whole line

for (int i = 0; i < lines.size(); i++)
{
    for (int j = i + 1; j < lines.size(); j++)
    {       
        cv::Point2f pt = computeIntersectionOfTwoLine(lines[i], lines[j]);
        if (pt.x >= 0 && pt.y >= 0 && pt.x < image.cols && pt.y < image.rows)
        {
            corners.push_back(pt);
        }
    }
}

      



You can google an algorithm to find the intersection of two strings. After you have collected all the intersection points, you can easily determine the minimum maximum that the upper left and lower right points will give you. From these two points, you can easily get a rectangle.

Here's Sorting 2nd point array to find four corners and http://opencv-code.com/tutorials/automatic-perspective-correction-for-quadrilateral-objects/ Please refer to these two links.

+1


source


OpenCVs hough transform can really use some improvement without limitation. Without it, you get this duplicate row phenomenon. Unfortunately, I don't know what an easy way to tweak this other than overriding hough's own transform. (This is a valid option. The Hough transformation is pretty simple)

Fortunately, this is easy to fix in post-production:



For the non-probabilistic hough transformation, OpenCv will return the strings in order of confidence, with the strongest string. So just take the first four lines, which are very different from each other in both ro and aunt.

  • add the first line found by HoughLines to the new list: strong_lines
  • for each line found by HoughLines:
    • check if its rho and theta are close to any strong line (for example, rho is within 50px and theta is within 10 ° of another line).
    • if not, put it in the strong_lines list
    • if you find 4 strong_lines, break
+7


source


I have implemented the approach described by HugoRune, and although I would share my code as an example of how I did it. I used a tolerance of 5 degrees and 10 pixels.

strong_lines = np.zeros([4,1,2])

minLineLength = 2
maxLineGap = 10
lines = cv2.HoughLines(edged,1,np.pi/180,10, minLineLength, maxLineGap)

n2 = 0
for n1 in range(0,len(lines)):
    for rho,theta in lines[n1]:
        if n1 == 0:
            strong_lines[n2] = lines[n1]
            n2 = n2 + 1
        else:
            closeness_rho = np.isclose(rho,strong_lines[0:n2,0,0],atol = 10)
            closeness_theta = np.isclose(theta,strong_lines[0:n2,0,1],atol = np.pi/36)
            closeness = np.all([closeness_rho,closeness_theta],axis=0)
            if not any(closeness) and n2 < 4:
                strong_lines[n2] = lines[n1]
                n2 = n2 + 1

      

+4


source


The above approach (suggested by @HugoRune and implemented by @ Onamission21) is correct, but has a small bug. cv2.HoughLines

may return negative values ​​rho and theta upto pi. Note, for example, that line (r0,0) is very close to line (-r0, pi-epsilon), but they will not be found in the above proximity test. I was just handling negative rhos by applying rho*=-1, theta-=pi

prior to calculating proximity.

0


source







All Articles