Search code examples
opencvlinehough-transform

Line detection in a binary image using opencv


I am trying to detect the crossing lines in in the following binary image:

enter image description here

Using the HoughLine detection isn't producing the correct results

Any pointers or direction would be appreciated. Thanks


Solution

  • using the image directly as input, you will have to invert it (white = lines to detect).

    I'll get those results with HoughLinesP

    enter image description here

    and if you dilate the inverted image once you get this result:

    enter image description here

    with this code:

    int main()
    {
        cv::Mat color = cv::imread("../inputData/LineDetectionCrosses.png");
        cv::Mat gray;
    
        // convert to grayscale
        cv::cvtColor(color, gray, CV_BGR2GRAY);
    
        cv::Mat mask = 255-gray;
    
        //cv::dilate(mask, mask, cv::Mat());
    
        // detect HoughLinesP
        std::vector<cv::Vec4i> lines;
        cv::HoughLinesP(mask, lines, 1, CV_PI/2880, 50, 300, 10 );
        for( size_t i = 0; i < lines.size(); i++ )
        {
            cv::Vec4i l = lines[i];
            cv::line( color, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar(0,0,255), 3, CV_AA);
        }
    
        // display results
        cv::imshow("mask",mask);
        cv::imshow("output", color);
        cv::waitKey(0);
    
    }
    

    but if it was my project, I'd probably try to detect the circles first and find/extract lines with known center point!