Ask Your Question

SinisterMJ's profile - activity

2016-12-14 19:07:12 -0600 received badge  Popular Question (source)
2016-09-09 06:38:02 -0600 received badge  Student (source)
2013-05-13 06:39:47 -0600 received badge  Supporter (source)
2013-05-13 06:32:45 -0600 received badge  Editor (source)
2013-05-13 06:28:09 -0600 asked a question findContours - OpenCV 2.4.5, Heap Corruption

I have read this happening on different questions here, but no answer worked so far, so this is my code:

cv::Mat* CVMatFromUC(unsigned char* input, int width, int height)
{
    cv::Mat* resultMat = new cv::Mat(height, width, CV_8UC1);
    resultMat->data = input;
    return resultMat;
}

unsigned char* UCFromMatUC(cv::Mat* input)
{
    int size = input->size.p[0] * input->size.p[1];
    unsigned char* result = new unsigned char[size];
    memcpy(result, input->data, size);
    return result;
}

unsigned char* CannyEdgeCV(unsigned char* input, int width, int height)
{
    std::vector<std::vector<cv::Point> > contours;
    std::vector<cv::Vec4i> hierarchy;

    cv::Mat* inp = CVMatFromUC(input, width, height);
    cv::Mat canny_output;
    cv::Mat* outp = new cv::Mat(height, width, CV_8UC1);
    cv::blur(*inp, *outp, cv::Size(3,3));
    cv::Canny(*outp, canny_output, 10.0, 15.0);
    if(!canny_output.type()==CV_8UC1){
        return NULL;
    }

    cv::findContours(canny_output, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE );
    //cv::findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );

    unsigned char* result = UCFromMatUC(inp);
    delete outp;
    delete inp;
    return result;
}

Originally I was only using the canny edge map, but later on I wanted to test the results of the contour functionality.

The Canny Edge works fine, and I get an image as expected (C:\fakepath\EdgeMap.bmp, but the findContours (both the one in code and the commented version) fail with a heap corruption error. What causes this?

The entry point for this is the CannyEdgeCV()