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()
Do you use Visual Studio as IDE? If so try to use MFC as shared library.
as a general advice - do not use Pointers to cv::mMat.
it's a 'smart pointer' already, chances are high, you're thrashing the internal refcount this way.