Ask Your Question
0

Use allocated buffer for Mat Data Pointer

asked 2014-10-21 14:42:23 -0600

roosemberth gravatar image

Hello,

I'm Passing frames from 2 cameras to 2 Mat types via (Videocapture.read()). The problem is that I have to copy in order to enqueue them in an OpenCL Buffer. As I can directly map (OpenCL)Device Memory into Host Memory (thus making it as a transparent buffer), is there any way I can Use my allocated buffer to fill the mat container with my frame?

Best Regards!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2014-11-09 06:55:28 -0600

SR gravatar image

updated 2014-11-09 06:58:23 -0600

If you want to "fill the container with your frame" then there is no way around allocating matrix and copying your data into that matrix. However, if you just want to wrap your data pointer by a cv::Mat object (provided that the data organization of this kind makes sense) you can simply call the wrapper constructor:

//! constructor for matrix headers pointing to user-allocated data
Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);

You can wrap your frame as simple as

Mat wrapped(MatrixRows, MatrixCols, MatrixType, MyDataBuffer);

Note that the destruction of wrapped does not free your buffer! You need to manage the underlying memory yourself.

If you then want to obtain a real copy it is probably the easiest to do

Mat copy = wrapped.clone();

See also http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-mat

edit flag offensive delete link more

Comments

1

Thank you.

hodu gravatar imagehodu ( 2020-10-27 23:03:25 -0600 )edit
0

answered 2014-11-09 05:29:14 -0600

roosemberth gravatar image

So, I figured out that I could Initialize my Mat as

cv::Mat MatrixName(MatrixRows, MatrixCols, MatrixType/*(ie. CV_8UC4)*/, \
    NULL/*(This is the Data Pointer)*/, MatrixRowStep);

And then I could Assign the Pointer to the Data Member as

MatrixName.data = MyDataBuffer;

Which is, in my application, Mapped to the OpenCL GPU Memory-Mapped Buffer

Then I was Able to Normally Use Functions as cv::imshow, etc...

Best Regards!

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-10-21 14:42:23 -0600

Seen: 19,469 times

Last updated: Oct 27 '20