cv::Mat() data pointer vs Mat::clone()

asked 2013-09-25 20:38:29 -0500

jfdesgagne gravatar image

updated 2013-09-25 20:38:53 -0500

I have a pointer to a buffer image. I'm trying to create a cv::Mat and assign it my buffer image. The only way i found is by doing

uchar* buffer; //have some data in it
cv::Mat(height, width, CV_8UC1, buffer); //which won't copy the data, will use the pointer as reference

The problem is I don't have ownership of that buffer. So I can either :

create a mat and assign it that buffer, than do a copyTo itself. (which is the same thing than I clone())

do a malloc and memcpy of that buffer (so i have a pointer that i own) and create a mat from it. (which is much faster).

Is the method #2 wrong? Why is it so much faster?

Bonus questions: What is hapenning to my buffer memory when I do operation on my mat? Is it getting resized? Is it getting updated (if i do a cv::flip, will it update the original buffer) ? And why is the clone() so expensive?

Sorry for the multiple questions, but I think they are all related and will help me to understand a little bit better the cv::Mat structure.

edit retag flag offensive close merge delete