Issue
something weird is happening to me with the Mat of opencv when i resize the Mat.
this is the header in opencv: first i receive a pointer from my managed code in visual c#:
unsigned char * PointerImage( unsigned char* matData, int widht)
i create a Mat from the pointer in opencv:
cv::Mat OriginalImg(Size(widht,widht),CV_8UC3,matData);
If i don´t apply the resize and i send the pointer to my Mat created to my program in visual c#, it works fine, and i can show the image in my visual c#.
unsigned char *data = OriginalImg.data;
the problem begin when i apply the resize to put the image bigger :
cv::resize( OriginalImg, OriginalImg, size(270,270));
Now i can´t see the image .
here is the entire code:
unsigned char * PointerImage( unsigned char* matData, int widht){
cv::Mat OriginalImg(Size(widht,widht),CV_8UC3,matData);
cv::resize( OriginalImg, OriginalImg, size);
unsigned char *data = OriginalImg.data;
return data;
}
i doubt about the pointer when i apply the resize. thanks.
Solution
User @berak is quite right. You have a memory leak in your code. You cannot return a pointer to the local variable data created in the function. The destructor of OriginalImg object is fired when program gets out of PointerIMage function scope. The memory is released, so does OriginalImg.data, making this pointer unavialable.
Please consoder using global variable in your c++ code (in fact not good solution) or encapsulate your c++ code in the class which you can later use in c# (depends how you call the native code form .NET).
Answered By - marol Answer Checked By - Marie Seifert (sql.easybugfix Admin)
Post a Comment
Post a Comment