Ask Your Question

Place data of Mat at a specific memoryarea pointed to by a pointer

asked 2017-06-29 01:56:20 -0500

TADRIAN gravatar image

I have created a memory object on the shared memory with following OpenCL-Function call:

cl_mem buffer_img_GAUSS_TEST = clCreateBuffer(context, CL_MEM_ALLOC_HOST_PTR, sizeof(uchar) * size_cols * size_rows,NULL,&status);

A call of this function gives me the pointer:

uchar *src_ptr;
src_ptr  = (uchar *)clEnqueueMapBuffer(cmdQueue, buffer_img_GAUSS_TEST, CL_TRUE, CL_MAP_READ, 0, sizeof(uchar) * size_cols* size_rows, 0, NULL, NULL, &status);

Now I want to read an image with following OpenCV function call:

Mat img= imread( "scene.jpg", IMREAD_GRAYSCALE );

Is it possible to "say" that the data of the picture should be placed at the data-area pointed to by src_ptr? The area located by the buffer_img_GAUSS_TEST has exactly the size needed for the data of Template

In other words: I want to replace this part of the code, which copys the data from the Image to the address pointed to by src_pointer.

memcpy ( src_ptr,, sizeof(uchar) * img.cols * img.rows);
edit retag flag offensive close merge delete


if your question was: "can i load an image directly into gpu memory ?" - then the answer is NO.

berak gravatar imageberak ( 2017-06-29 02:07:46 -0500 )edit

I have a shared memory between the Host(ARM) and the Device (FPGA). on this shared memory i created the buffer. After that i want to read an image and safe the data of the image on the allocated memory which was allocated by clCreateBuffer

TADRIAN gravatar imageTADRIAN ( 2017-06-29 03:16:13 -0500 )edit

Isn't that a problem similar to this one?

KjMag gravatar imageKjMag ( 2017-06-29 06:25:18 -0500 )edit

not really.

you can build a (shallow) Mat structure around a pointer, but you cannot force imread() to use your memory.

it's another story, if you use your own tga or pgm loader.

berak gravatar imageberak ( 2017-06-29 06:34:08 -0500 )edit

It´s the opposite direction. On the thread he initialized a CV:Mat with values located in the shared memory (where the pointer pointed to) but i want to read in a picture in a CV:Mat and save the values on a memory area a pointer is pointing to.

TADRIAN gravatar imageTADRIAN ( 2017-06-29 06:34:26 -0500 )edit

Ok, thanks for pointing that out.

KjMag gravatar imageKjMag ( 2017-06-29 08:49:57 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2017-06-29 20:03:36 -0500

Tetragramm gravatar image

If you map the pointer into a Mat, and the Mat is the exact size and type of what you are copying, you can simply use the copyTo function.

Mat src_mat(img.rows, img.cols, img.type(), src_ptr, *pitch if necessary*);

copyTo is the only reliable way to do this. Some functions will store their result into it, but not all, and some depend on the other input parameters for where they store it.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-06-29 01:56:20 -0500

Seen: 228 times

Last updated: Jun 29 '17