How to loose pixel ownership of a Mat

I have a Mat and I want to pass its data to another container without copying. I want to make the Mat no longer possess ownership of that data because if it has the ownership, then it will try to release it at the end of my function (the container will not be usable anymore). I tried addref() function to add reference to Mat’s smart pointer and prevent its deletion. But it couldn’t help.

how do you construct the original Mat ? would it be possible to use the memory of “another container” (your “destination”) there already ? something like:

vector<uchar> container = .... (assume, you already have w*h bytes in there)

Mat m(h, w, CV_8U,; // no refcounting !
// process Mat from opencv

// processed data is now in "container"

Thanks for the reply
I have that container already. I will make a Mat just like what you did. Then I process the Mat and get to a new Mat. Now, I want to pass the data of this new Mat to a new container (not the previous one). Something like:

vector<uchar> container = ...

Mat m(h, w, CV_8U,;

Mat gray;
cvtColor(m, gray, COLOR_RGB2GRAY);

vector<uchar> container2; =;
Mat img = ....

vector<uchar> container(; // pre-allocate
Mat gray(img.size(), CV_8U,; // Mat as a "wrapper"
cvtColor(m, gray, COLOR_RGB2GRAY); // output is in "container" now

Thanks. This is a cool solution