HoughLines Debug Assertion Failed

asked 2014-09-08 14:55:06 -0500

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:


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);


    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

edit retag flag offensive close merge delete



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?

FooBar gravatar imageFooBar ( 2014-09-09 01:38:18 -0500 )edit

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.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-09-09 08:14:20 -0500 )edit

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.

Cross22 gravatar imageCross22 ( 2014-09-21 15:45:07 -0500 )edit

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?

stantoncoffey gravatar imagestantoncoffey ( 2014-09-30 14:56:00 -0500 )edit