I have cv::Mat and my custom struct that holds the data. Is it safe to pass the data from cv::Mat to my structure like this?
struct MyStruct {
uint8_t* data;
}
MyStruct foo(){
MyStruct s;
cv::Mat m = cv::imread("test.png", 0);
s.data = m.data;
m.data = nullptr;
return s;
}
Basically, I need OpenCV not to destroy allocated cv::Mat data. I also dont want to create copy, since the data may be quite large.
Or is there any other way?
Will not do a new copy of data. It will just copy the header and will increase smart pointer counter.
To apply deep copy you need to do:
else data will be shared between a and b.
If you want use custom storage, it's better to allocate and copy explicitely
then allow opencv free allocated Mat, else you risk to break memory management in your application.
Also you may try to create a matrix using preallocated array:
But not sure about memory management in this case, can't try it now.