Search code examples
opencvcomputer-visionhough-transform

Choosing Lines From Hough Lines


I'm using Hough Lines to do corner detection for this image. i plan to find the intersection of the lines as the corner. This is the image. enter image description here

Unfortunately, Hough return lots of lines for each line I expect enter image description here

How do I tune the Hough Lines so there is only four lines each corresponds to actual line on the image?


Solution

  • Collect the intersection of all 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 the algorithm to find the intersection of two lines. Once you collect all the intersection points you can easily determine the min max which will give you top-left and bottom right points. From these two points you can easily get the rectangle.

    Refer to these two links: