Ask Your Question
4

Using external image data in a cv::Mat

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

L3Bee gravatar image

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

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
4

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

SR gravatar image

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

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
3

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

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

Comments

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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 12,932 times

Last updated: Feb 28 '13