findContours Assertion Failed

asked 2015-03-22 15:17:04 -0500

Antares gravatar image

Hello, I have really annoying problem with this error:

image description

This error occurs when code return from function where is used function findContours. I tried everything. Only thing that helps is use function imshow right after findContours but it works only when I call my function only once.

This works

void OpenCVTEST::ContoursTest(cv::Mat src)
    {
        cv::vector<cv::vector<cv::Point>> img_contours;
        cv::vector<cv::Vec4i> hierarchy;
        cv::Mat  new_contours_img;

        findContours( src, img_contours, hierarchy, CV_RETR_LIST ,CV_CHAIN_APPROX_NONE, cv::Point(0, 0) );
        cv::imshow("image",src);
    }

This doesn´t work

void OpenCVTEST::ContoursTest(cv::Mat src)
    {
        cv::vector<cv::vector<cv::Point>> img_contours;
        cv::vector<cv::Vec4i> hierarchy;
        cv::Mat  new_contours_img;

        findContours( src, img_contours, hierarchy, CV_RETR_LIST ,CV_CHAIN_APPROX_NONE, cv::Point(0, 0) );
    }

Please help what is wrong.


OpenCV 2.4.10

Visual Studio 2012

Linked libs (vc 11): opencv_core2410d.lib opencv_imgproc2410d.lib opencv_highgui2410d.lib opencv_ml2410d.lib opencv_video2410d.lib opencv_features2d2410d.lib opencv_calib3d2410d.lib opencv_objdetect2410d.lib opencv_contrib2410d.lib opencv_legacy2410d.lib opencv_flann2410d.lib

edit retag flag offensive close merge delete

Comments

1

I'm not sure, but try void OpenCVTEST::ContoursTest(cv::Mat &src) instead of cv::Mat src. I had problems with that in the past. Evantualy copy src before transfering to the function or in the function, because findContours will change the data of the cv::Mat.

I recommend you to use InputArray, OutputArray and InputOutputArray instead.

void OpenCVTEST::ContoursTest(cv::InputOutputArray src) {
cv::Mat _src = src.getMat();

cv::vector<cv::vector<cv::Point>> img_contours;
cv::vector<cv::Vec4i> hierarchy;

findContours( _src, img_contours, hierarchy, CV_RETR_LIST ,CV_CHAIN_APPROX_NONE, cv::Point(0, 0) );

}

For more information it is useful to look at some functions in the OpenCV sources.

matman gravatar imagematman ( 2015-03-22 16:39:58 -0500 )edit