Get a single line representation for multiple close by lines clustered together in opencv

前端 未结 6 2157
野趣味
野趣味 2020-12-04 15:42

I detected lines in an image and drew them in a separate image file in OpenCv C++ using HoughLinesP method. Following is a part of that resulting image. There are actually h

6条回答
  •  没有蜡笔的小新
    2020-12-04 16:18

    First off I want to note that your original image is at a slight angle, so your expected output seems just a bit off to me. I'm assuming you are okay with lines that are not 100% vertical in your output because they are slightly off on your input.

    Mat image;
    Mat binary = image > 125;  // Convert to binary image
    
    // Combine similar lines
    int size = 3;
    Mat element = getStructuringElement( MORPH_ELLIPSE, Size( 2*size + 1, 2*size+1 ), Point( size, size ) );
    morphologyEx( mask, mask, MORPH_CLOSE, element );
    

    So far this yields this image:

    These lines are not at 90 degree angles because the original image is not.

    You can also choose to close the gap between the lines with:

    Mat out = Mat::zeros(mask.size(), mask.type());
    
    vector lines;
    HoughLinesP(mask, lines, 1, CV_PI/2, 50, 50, 75);
    for( size_t i = 0; i < lines.size(); i++ )
    {
        Vec4i l = lines[i];
        line( out, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255), 5, CV_AA);
    }
    

    If these lines are too fat, I've had success thinning them with:

    size = 15;
    Mat eroded;
    cv::Mat erodeElement = getStructuringElement( MORPH_ELLIPSE, cv::Size( size, size ) );
    erode( mask, eroded, erodeElement );
    

提交回复
热议问题