HoughLinesP not able to detect lines

asked 2017-11-13 11:15:28 -0600

MaxWell gravatar image

updated 2020-12-09 08:21:27 -0600


I have been struggling with detecting straight lines with HoughLines() after I used the canny edge detector. The canny edge detector works fine I believe (rectangle with triangle). But from the houghtransform I'm not been able to detect those lines. Underneath the pictures you will find the code I have been using and tried to play with the thershold values etc. I'm not really sure on how to improve the houghlines. Anyone who can help me?


image descriptionimage description

    Mat img = this->frame.clone();

    cv::Size size(3,3);

    adaptiveThreshold(img, img,255,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,249,10);
    cv::imwrite("threshold.jpg", img);

    cv::bitwise_not(img, img);
    cv::imwrite("bitwise.jpg", img);
    int lowThreshold = 100;
    cv::Canny( img, img, lowThreshold, lowThreshold*3, 3 );
    cv::imwrite("canny.jpg", img);

    vector<Vec4i> lines;
    HoughLinesP(img, lines, 1, CV_PI/180, 40, 5, 50);
    cout << lines.size() << endl;
    for( size_t i = 0; i < lines.size(); i++ )
        line( this->frame, Point(lines[i][0], lines[i][1]),
            Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 2, 8 );
    cv::imwrite("hough.jpg", frame);
    cv::imshow("frame", this->frame);

EDIT I don't have the original image anymore, but I still have the image after the gaussian filter image description

edit retag flag offensive close merge delete


Can you also include your original input image?

sjhalayka gravatar imagesjhalayka ( 2017-11-13 16:58:15 -0600 )edit

I edited the post

MaxWell gravatar imageMaxWell ( 2017-11-14 02:54:48 -0600 )edit

houghlines is working on gradients, so please don't binarize it before.

also, is is using it's own Canny, so drop that, too.

berak gravatar imageberak ( 2017-11-14 03:02:10 -0600 )edit

If your purpose is detecting (rectangle with triangle) then doing contour analysis and detecting shapes is better for this case.

Ziri gravatar imageZiri ( 2017-11-14 04:21:26 -0600 )edit

@Ziri that's what I'm trying now. Do you know a way to detect if a boundingrect has boundingrects inside?

MaxWell gravatar imageMaxWell ( 2017-11-14 05:00:20 -0600 )edit

CV_PI/180, 40, 5, 50 The 50 is too high. Needed to be lower. And 5 could be increased too. You can play around with 3 parameters.

supra56 gravatar imagesupra56 ( 2017-11-14 05:04:05 -0600 )edit

If you detect contours as a tree with RETR_TREE your triangle becomes a hole. (check contour Hierarchy) you'll also need to approximate contours to detect their shape (approxPolyDP).

Ziri gravatar imageZiri ( 2017-11-14 05:28:50 -0600 )edit

you'll be able to do the same with Hough but tuning parameters for different cases is difficult.

Ziri gravatar imageZiri ( 2017-11-14 05:30:17 -0600 )edit