Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How do I pass ownership of pixel data to cv::Mat

I am creating a cv::Mat passing in pixel data that I have allocated externally.

cv::Mat transformedResult(vImageResult.height,
                          vImageResult.width,
                          CV_8UC1,
                          vImageResult.data);

I would like the cv::Mat to take ownership of the bytes (i.e. create a refCount and free the bytes when it reaches zero) . However the documentation says

Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically deallocated, so you should take care of it.

  • If I free the underlying vImageResult.data immediately, the I will get a bad access crash somewhere down the line.
  • If I don't free the underlying vImageResult.data then the data will leek.

Is there a way to pass ownership?

click to hide/show revision 2
retagged

updated 2014-03-19 11:12:21 -0600

berak gravatar image

How do I pass ownership of pixel data to cv::Mat

I am creating a cv::Mat passing in pixel data that I have allocated externally.

cv::Mat transformedResult(vImageResult.height,
                          vImageResult.width,
                          CV_8UC1,
                          vImageResult.data);

I would like the cv::Mat to take ownership of the bytes (i.e. create a refCount and free the bytes when it reaches zero) . However the documentation says

Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically deallocated, so you should take care of it.

  • If I free the underlying vImageResult.data immediately, the I will get a bad access crash somewhere down the line.
  • If I don't free the underlying vImageResult.data then the data will leek.

Is there a way to pass ownership?