Steal data ptr from Mat or GpuMat

asked 2019-02-13 11:30:42 -0500


Is there a way for me to steal the data pointer of a cv::Mat or cv::cuda::GpuMat such that I can hold on to that data without having to copy it before the Mat goes out of scope and gets released?

I’m in a scenario where my GpuMat goes out of scope and I will only manipulate the data array further in CUDA. Is there a way for me to set the refcount to NULL?

edit retag flag offensive close merge delete


Your question is quite unclear. Are you asking for CV::Mat or CV::GpuMat? One quick look in docu gives you this: uchar* cv::Mat::data pointer to the data GpuMat meanw the image is on the GPU. In this you should get familiar with this concept.

Ikarus79 gravatar imageIkarus79 ( 2019-02-13 12:13:43 -0500 )edit

Thanks captain obvious

Reinier gravatar imageReinier ( 2019-02-13 22:33:35 -0500 )edit

That data pointer will be invalidated when the GpuMat is released because its refcount will go to 0 and the data will be freed. If the GpuMat is constructed with user data this does not happen. My question therefore is: what manipulation to the refcount do I need to make to replicate such behavior? I want to hold on to my data, so to speak, and I do not want GpuMat to deallocate it ever.

Reinier gravatar imageReinier ( 2019-02-13 22:36:59 -0500 )edit

The cv::Mat documentation states clearly: Once the array is created, it is automatically managed via a reference-counting mechanism. If the array header is built on top of user-allocated data, you should handle the data by yourself. -> in other words, if you allocate the buffer by yourself the memory will be not released if ref-count reaches 0. For cv:GpuMat the documentations states clearly as well: no functions that return references to their data (because references on GPU are not valid for CPU)

Ikarus79 gravatar imageIkarus79 ( 2019-02-14 03:00:21 -0500 )edit

Got it. But there is no post-hoc way to pass ownership of the data? (As a convenience)

Reinier gravatar imageReinier ( 2019-02-14 22:28:29 -0500 )edit