Running into a problem with opencv 2.4.0 findContours function constantly crashing. Unfortunately, it’s been very difficult for me to pinpoint the issue. Hoping someone has run into a similar problem.
I’m capturing the depth stream from a Kinect sensor using the Microsoft K4W SDK 1.5, copying that to an OpenCV Mat, then converting it to an 8UC1 image via cvtColor and threshold. I run a countNonZero just to be sure the image isn’t blank before passing it to findContours. But even the simplest findcountours implementation crashes.
Here’s my basic code:
rawdepth = Mat(Size(640,480),CV_8UC4);
thresh = Mat::zeros(640,480,CV_8UC1);
// storage for contours
vector<vector<Point>> contours;
cvtColor(rawdepth,thresh,CV_RGB2GRAY);
threshold(thresh,thresh,0,255,THRESH_BINARY);
if(countNonZero(thresh) > 100 ) {
// This crashes
findContours(thresh,contours,RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
}
I did verify that the actual Mat being passed to findcontours is a single-channel image and that it is not blank (ie, there are some 500+ points). But I’m wondering if this is a heap or thread related problem since I’ve heard findcontours can actually modify the input Mat?
At this point I’m going to try and use OpenCV 2.4.1 which was just released, though I don’t see any fixed bugs that would point to fixing this problem.
Any ideas are much appreciated…
The problem turned out to be a compiler flag which was limiting the program’s stack usage to 10MB (10,000,000 bytes). Unfortunately this wasn’t enough for the Kinect video I was capturing. After removing the compiler flag, the findContours will now compile and work properly!
I’m using VC++ 2010 and the compiler flags were under Project Properties->Linker->System. The two fields were: “Stack Reserve Size” and “Stack Commit Size”
After clearing them and recompiling, the project works beautifully!