Ask Your Question

Use allocated buffer for Mat Data Pointer

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

roosemberth gravatar image


I'm Passing frames from 2 cameras to 2 Mat types via ( 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

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

SR gravatar image

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

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

edit flag offensive delete link more



Thank you.

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

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

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 = 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


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

Seen: 17,653 times

Last updated: Oct 27 '20