Search code examples
c++opencvmemory-managementmat

How can I determine whether a cv::Mat is using internal or external data?


I'm integrating OpenCV with a legacy codebase that has its own ref-counted image class. I'm adding a constructor for creating these images from cv::Mat. As an optimization, I'd like to exploit cv::Mat's refcounting mechanism & make a shallow copy, when it owns the data. However, when it's using external data, then I need to force a deep copy.

The problem is that, from reading the docs, I don't see a way to determine whether a cv::Mat owns its data or not. Can this be done (without modifying OpenCV)?

BTW, in case it matters, I'm using OpenCV 3.1.


Solution

  • Use the member UMatData * u of cv::Mat. It should be 0 if cv::Mat uses external data, otherwise you can get the ref counter as follows

    img.u->refcount