Ask Your Question

Coder's profile - activity

2013-11-03 06:42:42 -0600 received badge  Editor (source)
2013-11-03 06:39:03 -0600 asked a question Problems with findContours() on 64bit VS 2012

Hello!
I have tried to run the following code

#include "stdafx.h"
#include "highgui.h"
#include "cv.h"

using namespace cv;

const string inputFileName = "image.jpg";

Mat toBinary( Mat src ) {
    Mat dst = src.clone();

    for (int i = 0; i < src.rows; i++)
        for (int j = 0; j < src.cols; j++)
            dst.at<uchar>(i, j) = src.at<uchar>(i, j) >= 100 ? 255 : 0;

    return dst;
}    

int _tmain(int argc, _TCHAR* argv[]) {
    Mat src = imread(inputFileName, CV_LOAD_IMAGE_GRAYSCALE);
    Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3);

    src = toBinary(src);
    src = src > 1;
    blur(src, src, Size(5, 5));

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(src, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_TC89_L1, Point(0, 0));

    int idx = 0;
    for( ; idx >= 0; idx = hierarchy[idx][0] )
    {
        Scalar color(rand() & 255, rand() & 255, rand() & 255);
        drawContours(dst, contours, idx, color, CV_FILLED, 8, hierarchy);
    }

    imshow("Components", dst);
    waitKey(0);
    return 0;
}

with this image
image description
and get this error
image description
But, when I changed

return 0;

to

exit(0)

this code worked without any error. Seems, what it was problem with free/allocate memory.
Why this happened and how to make code correct?