Ask Your Question

calling findcontours throws an exception...

asked 2015-07-26 10:55:04 -0600

st5000 gravatar image


I'm new to opencv, trying my hand at image tracking using OpenCV 3.0 and Visual Studio version 12.0.31101.00 Update 4 (2013). I am using code from this tutorial: (I'd link directly to the code in dropbox but youtube is playing games w/the URLs.)

...which is (likely) intended for an older version of OpenCV & Visual Studio. But, after altering the project's variables and the program's include and using lines, the code compiles and successfully calls many OpenCV methods. Including "cvtColor", "absdiff", "threshold" and "blur".

Unfortunately, the program throws an exception when "findContours" is called. When checking the "findContours" arguments, I see where the 3rd variable (usually referred to as "hierarchy") is abnormally large (19 decimal digits long) in size.

After reading some posts, I tried switching from calling the "threshold" method to calling the "Canny" method when preparing the image for the "findContours" method. But the program still threw the exception. I have even tried completely different videos. The calls to the other methods worked (pressing "d" while the program runs displays the intermediate images verifying this). But the "findContours" method (pressing "t" while the program runs allows calls to the "findContours" method) still threw the exception.

It is not a very long program. I'll try and insert my version of code here:

(Wow, including code using this web/interface/editor is really difficult! The editor keeps breaking it up with (kind-of) normal text.)

(Sorry, it doesn't appear I can drop in all the code w/o this editor chopping it up. I'll try to drop in only the important bits then.)

This is the bit of code that prepare the image for the call to the "findContours" method. The calls to "imshow" verify these methods are working as expected. Notice I tried to replace a call to the "threshold" method with a call to the "Canny" method as an attempt to avoid the exception when calling the "findContours" method. The call to the "findContours" method is inside the "searchForMovement" method called at the end of this section of code:

        while (capture.get(CV_CAP_PROP_POS_FRAMES)<capture.get(CV_CAP_PROP_FRAME_COUNT) - 1){

        //read first frame;
        //convert frame1 to gray scale for frame differencing
        cv::cvtColor(frame1, grayImage1, COLOR_BGR2GRAY);
        //copy second frame;
        //convert frame2 to gray scale for frame differencing
        cv::cvtColor(frame2, grayImage2, COLOR_BGR2GRAY);
        //perform frame differencing with the sequential images. This will output an "intensity image"
        //do not confuse this with a threshold image, we will need to perform thresholding afterwards.
        cv::absdiff(grayImage1, grayImage2, differenceImage);
        //threshold intensity image at a given sensitivity value
        cv::threshold(differenceImage, thresholdImage, SENSITIVITY_VALUE, 255, THRESH_BINARY);
        if (debugMode == true){
            //show the difference image and threshold image
            cv::imshow("Difference Image", differenceImage);
            cv::imshow("Threshold Image", thresholdImage);
            //if not in debug mode, destroy the windows so we don't see them anymore
            cv::destroyWindow("Difference Image");
            cv::destroyWindow("Threshold Image");
        //blur the image to get rid ...
edit retag flag offensive close merge delete


This thread concludes that the debug version of OpenCV contains a bug and to try the release version of OpenCV: ...this worked for me. After switching to release and compiling the project, the call to the "findContours" method did not throw an exception and worked as expected. Is this a bug then?
How should be proceed?

st5000 gravatar imagest5000 ( 2015-07-26 11:06:04 -0600 )edit

Which exception is thrown?

I'm using cv::findContours() and Visual Studio 2013 (not update 4 at the moment) many times and I had never any problem. Neither in release nor in debug mode.

Do you use precompiled libraries or do you build it by your own. I haven't tested it with precompiled libraries. Perhabs there is a problem with. With the self compiled libraries you can also go into findContours() function itself and search the location where the error happens.

The size of hierarchy.size() should be the same as the size of contours.size() and it says, that findContours() has found 19 seperated contours, which can also be a single pixel coordinate. Or did i misunderstood something?

matman gravatar imagematman ( 2015-07-27 12:12:10 -0600 )edit

In the link from st5000 the thread starter says, that he renamed the opencv_world300d.dll into opencv_world300.dll. I think this could be a linker problem. Please check if you linked the debug library (opencv_world300d.lib) in debug mode not the release lib.

matman gravatar imagematman ( 2015-07-27 12:17:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-08-19 17:50:30 -0600

st5000 gravatar image

Sorry @matman, my forum notification settings must be wrong as I didn't get email about your post. I believe I used precompiled OpenCV libraries. I don't recall renaming a debug OpenCV library file as a released OpenCV library file. I'll have to go back and check the wording of the exception and post what I find later.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-07-26 10:55:04 -0600

Seen: 2,281 times

Last updated: Jul 26 '15