HoughLine Not working

2019-09-15 20:22发布

问题:

I'm trying to detect lines in an image using houghLines and Canny edge detector but every time I get exe has stopped working, and this is really annoying. I'm using the latest pre-compiled exe and visual studio as ide. The canny works perfectly but from the moment I try to hough.. problem.

Using OpenCV 3.1.0 and vs 2015.

Code:

void detectLines(Mat image) {
    Mat dest = image.clone();
    Mat graydest = image.clone();

    if (image.channels() == 3) {
        cvtColor(image, image, CV_BGR2GRAY);
    }

    double threshold = 5;

    Canny(image, dest, 0.4*threshold, threshold);
    cvtColor(dest, graydest, COLOR_GRAY2BGR);

    imshow("Display Window", dest);
    waitKey(0);

    vector<Vec2f> lines;

    HoughLines(dest, lines,1,CV_PI / 180, 0,0);

    cout << "Number of lines " << lines.size() << endl;

    if (!lines.empty()) {

        for (size_t i = 0; i < lines.size(); i++)
        {
            float rho = lines[i][0];
            float theta = lines[i][1];
            double a = cos(theta), b = sin(theta);
            double x0 = a*rho, y0 = b*rho;

            cout << rho << " " << theta << " " << a << " " << x0 << " " << endl;

            Point pt1(cvRound(x0 + 1000 * (-b)),
                cvRound(y0 + 1000 * (a)));
            Point pt2(cvRound(x0 - 1000 * (-b)),
                cvRound(y0 - 1000 * (a)));

            line(graydest, pt1, pt2, Scalar(0, 0, 255), 3, 8);
        }
    }

    imshow("source", image);
    imshow("Display Window", graydest);
    waitKey(0);

}

The output is crap 1/2 of the time it actually return a vector, the other 1/2 it just goes stopped working.

Debugging further gives a read access violation and I think the vector size of the lines is just too big.

[SOLUTION]

See below, thx miki

回答1:

This kind of errors is usually caused by mixing debug/release libraries.

Be sure to use in Debug mode opencv_<module><version>d (with trailing d) libraries, and in Release mode without the trailing d.

As it turns out from comments, you're linking in Debug mode to both opencv_world310.lib and opencv_world310d.lib. You should remove the first one, since in Debug mode you should have only debug libraries (with trailing d).