Ask Your Question

Using external image data in a cv::Mat

asked 2013-02-27 16:40:22 -0500

L3Bee gravatar image

updated 2013-02-28 02:26:03 -0500

SR gravatar image

I am working on integrating some OpenCV functionality into an existing application that passes data around with pointers. What I want to do is wrap a Mat around the existing data, use some OpenCV functions, then let the data go when I'm done. However, the program is time-sensitive, and I would like to avoid making any copies of the data.

I see a few options. First, is there anything equivalent to Eigen's Map<arrayxxd> that does exactly what I want? Secondly, can I simply re-assign Mat's ptr, width, and height variables, then put them back when I'm done? Or is there a different option?

Thank you for your help.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-02-28 02:24:33 -0500

SR gravatar image

updated 2013-02-28 02:30:23 -0500

Use the following:

T* external_mem = .... // external memory of type T
cv::Mat wrapped(rows, cols, cv::DataType<T>::type, external_mem, CV_AUTOSTEP); // does not copy

Or shorter if you know the type T, e.g. T = float:

float* external_mem = .... // external memory of type float
cv::Mat wrapped(rows, cols, CV_32FC1, external_mem, CV_AUTOSTEP); // does not copy

Remember to free/delete the pointer external_mem after usage. The Mat wrapped does not release the memory for you when it is destructed.

edit flag offensive delete link more

answered 2013-02-27 18:51:56 -0500

xaffeine gravatar image

See the reference manual page basic structures (cpp). In particular there is a constructor for cv::Mat, that takes a raw pointer along with information about the dimensions and type. This should do what you want! I use it for pointers that come from DirectShow and other libraries.

edit flag offensive delete link more


I now wonder how I didn't see that when I was looking at it before. Thank you both.

L3Bee gravatar imageL3Bee ( 2013-02-28 08:38:54 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2013-02-27 16:40:22 -0500

Seen: 8,541 times

Last updated: Feb 28 '13