Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when using Hough OpenCV

If I don't do anything (that is, don't change the color detection HSV via a Controls Window), the app runs fine. However if I change the HSV values when the app is running, I get the following errors. I have tested the code without Hough, it runs fine.

The CPU Error -

Unhandled exception at 0x00007FF9ECA64388 (ucrtbase.dll) in HoughFinder.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.

This is my code -

VideoCapture capture(0); // 0 is my webcam

...

capture.read(displayOriginal))
...(Code to detect colors for extra accuracy)
cudaCanny->detect(imgThresholded, imgCanny);

vector<Vec2f> lines;
//Ptr<HoughLinesDetector> hough = createHoughLinesDetector(1, CV_PI / 180, 100); CUDA code...
//hough->detect(imgCanny, lines); CUDA code...
HoughLines(displayCanny, lines, 1, CV_PI / 180, 100, 0, 0); // CPU code...
    for (size_t i = 0; i < lines.size(); i++)
    {
        float rho = lines[i][0], theta = lines[i][1];
        Point pt1, pt2;
        double a = cos(theta), b = sin(theta);
        double x0 = a*rho, y0 = b*rho;
        pt1.x = cvRound(x0 + 1000 * (-b));
        pt1.y = cvRound(y0 + 1000 * (a));
        pt2.x = cvRound(x0 - 1000 * (-b));
        pt2.y = cvRound(y0 - 1000 * (a));
        line(displayHough, pt1, pt2, Scalar(0, 0, 255), 3, CV_AA);
    }

imshow("Hough", displayHough);
imshow("Live Video", displayOriginal);

Extra Info -

If I use CUDA code to use Hough, I get this error -

Unhandled exception at 0x00007FF9F561A1C8 in HoughFinder.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000A75E81EB70.

App Error (Don't get this error while using CPU code) -

OpenCV Error: Assertion failed (d == 2 && (sizes[0] == 1 || sizes[1] == 1 || sizes[0]*sizes[1] == 0)) in cv::_OutputArray::create, file OPENCV_DIR\opencv-sources\modules\core\src\matrix.cpp, line 2363

Can anyone help? If either the CPU or CUDA code is fixed, its fine, but I would more prefer the CUDA error to be fixed (As CUDA has extra speed).

like image 868
FadedCoder Avatar asked Mar 13 '23 03:03

FadedCoder


2 Answers

After lots of trials and errors, I finally found the solution. Actually the output in detect should be a GpuMat not a vect2d. I would have figured this out earlier, but the documentation of OpenCV is very confusing. Here's the edited code -

Ptr<HoughLinesDetector> houghLines = createHoughLinesDetector(1, CV_PI / 180, 120);
GpuMat tmpLines; // This should be GpuMat...
vector<Vec2d> lines;
GpuMat imgCanny;
... 
while(true) {
    ...
    houghLines->detect(imgCanny, tmpLines);
    houghLines->downloadResults(tmpLines, lines);
    ...
}

OpenCV GPU Error (Function Not Implemented) in Hough Transform

like image 192
Albeirobu Avatar answered Mar 31 '23 06:03

Albeirobu


I had a similar problem. My code is something like this:

cv::Mat src, src_gray, src_blured, detected_edges, hough_lines;
std::vector<cv::Vec4i>  lines;
src = cv::cvarrToMat(img, false);

opencv_run_ = true;
while (opencv_run_) {
    cv::cvtColor(src, src_gray, CV_BGR2GRAY);
    cv::medianBlur(src_gray, src_blured, blur_size_);
    cv::Canny(src_blured, detected_edges, canny_low_threshold_, canny_max_threshold_, canny_aperture_size_);
    cv::HoughLinesP(detected_edges, lines, 2, CV_PI / 2, hough_votes_, hough_min_line_length_, hough_max_line_gap_);
    hough_lines = cv::Mat::zeros(size_horizontal_, size_vertical_, CV_8UC4);
    for (size_t i = 0; i < lines.size(); i++) {
        cv::line(hough_lines, cv::Point(lines[i][0], lines[i][1]), cv::Point(lines[i][2], lines[i][3]), cv::Scalar(0, 0, 255), 3, 8);
    }
    if (lines.size() > 0) lines.clear();

    cv::imshow("Gray scale", src_gray);
    cv::imshow("Edges", detected_edges);
    cv::imshow("Hough", hough_lines);

    //Some logic in waitKey and sleeptime
    cv::waitKey(sleep_time);
}

cvDestroyAllWindows();
cvReleaseImageHeader(&img);

where img is a pointer to an IplImage in which i manually configure the values. (My image data are coming from a camera whose API gives me void * i.e. raw data)

This particular piece of code is running inside a boost::thread. While everything was running fine while inside the loop, when i did

opencv_run = false;
this_boost_thread.join();

in order to stop the thread, i was getting the invalid parameter exception. What baffled me is that the exception was thrown after the thread return which alarmed that this a classic case of stack corruption.

After hours of search i came across some post in some forum that said this is probably a problem with linked libraries. So i checked my opencv installation and saw that my libs are in a vc12 folder which means Visual Studio 2014 (I like to install pre built because i am an idiot), different from VS 2015 that i use.

So i searched for Visual Studio 2015 opencv libs, found some in the opencv 3.1 version https://sourceforge.net/projects/opencvlibrary/files/opencv-win/ , while i was using opencv 2.4.13. I decided not use them and build opencv from scratch. So i cloned from here https://github.com/opencv/opencv, followed the instructions given here http://docs.opencv.org/2.4/doc/tutorials/introduction/windows_install/windows_install.html and builded vc14 x86 opencv3.1 libraries which seem to work.

like image 37
k_kaz Avatar answered Mar 31 '23 06:03

k_kaz