Reputation: 699
I am creating real time software, so often cvFindContours is called on a completely black mask. If this case, cvFindContours throws an exception, and the program crashes.
How would I make it so that if cvFindContours is unable to find contours, instead of the program crashing, the program just moves onto the next line of code (just simple continues)?
Thanks
PS: I thought about keeping one pixel automatically always white to prevent cvFindContours from not being able to find an contour, but this would be inconvenient to me.
Upvotes: 0
Views: 1116
Reputation: 14011
Why don't you try something as follows:
Mat black = Mat::zeros(Size(100, 100), CV_8UC1);
vector< vector<Point> > contours;
if(sum(black).val[0] > 0.0)
{
findContours(black, contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
}
else
{
cout << "It's a black image, so I'm not going to do anything..." << endl;
}
This is using the C++ interface, but you should be able to use cvSum
to accomplish the same thing. So, if the image is all black, that means the image contains only zeros. Therefore, the sum will be zero when it is a black mask.
Upvotes: 1