HoughLines Debug Assertion Failed
Good Evening,
I have an application where I need to detect straight line segments within an image. It is my understanding that the "HoughLines" function would be the right function to be used to achieve this. I found an example at the following link:
http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html
Here is a copy of the exact code I'm using:
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv ) {
Mat src = cvLoadImage("Untitled.png", 0);
if(src.empty()) {
cout << "can't open file" << endl;
return -1;
}
Mat dst; Canny(src, dst, 50, 200, 3);
Mat cdst; cvtColor(dst, cdst, CV_GRAY2BGR);
vector<Vec2f> lines;
HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 );
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( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
}
imshow("source", src);
imshow("detected lines", cdst);
waitKey();
return 0;
}
When I run this code I'm thrown the following:
"Debug Assertion Failed!
Program: c:\Projects\OtherFiles\TestOpenCV\Debug\TestOpenCV.exe
File: C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vector Line: 157
Expression: vector iterator + offset out of range
I've been searching for the solution to this for a number of days now and can't seem to find anyone else with my exact same problem, otherwise I would not have posted to a forum.
If anyone could provide me with a solution to this problem or could direct me to another forum post with some more help then I would very much appreciate it.
By the way, this is being written in Microsoft Visual Studio 2008 Professional Edition on a Windows 7 machine.
Thanks, Stanton
The code works for me, so that it should be fine (On Ubuntu with g++ 4.6.4 and OpenCV 2.4.6) . Could you maybe post your picture? Where does the error happen? What is the smallest program you can write to create this error? Could you remove everything after the canny-command and run it again?
First guess looking at the error, you are somewhere using an index that is impossible. Like FooBar said, debugging is the way to go. Print out each index combo before further processing and see where the error happens.
Does it assert when lines goes out of scope?
Try moving the vector<vec2f> lines; declaration and HoughLines call into its own scope/function and see where the assertion happens.
Sorry for my late reply... I was using vs2008, but also have vs2013 installed and wanted to see if maybe I setup something wrong under vs2008. I used this setup tutorial: http://www.youtube.com/watch?v=vwhTKsvHwfQ to setup my project.
Now when I run the program, I am able to get through the program and display the images to the screen (including the lines produced from the houghline function), however, when I press a key (to exit the program) I get a "Windows has triggered a breakpoint in test.exe". If I comment out the houghlines function call then I don't get this problem.
I found a person with the same exact problem as me at this link: http://stackoverflow.com/questions/19169560/hough-line-transform-on-live-video-causes-breakpoint. Does anyone have any suggestions to fix this?