Ask Your Question

How to Create an image from pixels in memory?

asked 2012-07-20 09:45:31 -0600

Lacrymology gravatar image

updated 2012-07-20 10:12:25 -0600

How can I create an image from an array of bytes (or even better u16)? I'm having a hard time going through the docs, sorry for the n00b question

By the by, this is to be used for video (blob tracking on a depth field)

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-07-20 09:49:43 -0600

AlexanderShishkov gravatar image
uint16_t *depthMap = (uint16_t*)buffer;
int cols = 640;
int rows = 480;
Mat depthFrame( rows, cols, CV_16UC1 );
memcpy(, depthMap, rows*cols*sizeof(uint16_t) );
edit flag offensive delete link more



if the Mat will be used as input only, or as output but you're absolutely sure that no memory reallocation will occur - it's possible to use the data without copying:

Mat depthFrame( rows, cols, CV_16UC1, depthMap );

Andrey Pavlenko gravatar imageAndrey Pavlenko ( 2012-07-20 09:57:07 -0600 )edit

Great, no I'll copy it because I want to use just one or two lines of the original image (well, maybe I could do Mat depthFrame(2, cols, CV16UC1, depthMap + linewidth * SELECTEDLINE)).

Lacrymology gravatar imageLacrymology ( 2012-07-20 10:08:41 -0600 )edit

Oh! wait, just to get clear.. this is video (image stream) data. Is there a better way to do it in this case?

Lacrymology gravatar imageLacrymology ( 2012-07-20 10:09:43 -0600 )edit

how did you know this was a depthFrame?

Lacrymology gravatar imageLacrymology ( 2012-07-20 12:54:39 -0600 )edit

16U type is typical for Kinect-like depth maps) I case of video, you should creat Mat only one time and use memcpy for each frame. Or if you want to do it without copying of data, you can change Mat header for each frame as Andrey said .

AlexanderShishkov gravatar imageAlexanderShishkov ( 2012-07-21 17:00:10 -0600 )edit

If you want to copy the data, you can do this:

Mat two_rows=Mat(2, cols, CV16UC1, depthMap + linewidth * SELECTEDLINE).clone();

Niu ZhiHeng gravatar imageNiu ZhiHeng ( 2012-07-23 04:08:51 -0600 )edit

answered 2012-07-20 10:02:09 -0600

Michael Burdinov gravatar image

updated 2012-07-21 03:04:35 -0600

If you want to use existing buffer without copping it use:

Mat(rows, cols, CV_16U, your_buffer, stride_of_buffer);

It will create Mat that will contain your buffer. Note that Mat won't take the responsibility of releasing it, you will have to do that yourself. This just a wrapper for your data, no data is copied.

It doesn't really matter if the buffer is part of video or something else.

And the last parameter is optional (strideofbuffer). You don't need it if your buffer is continuous in memory.

edit flag offensive delete link more

Question Tools


Asked: 2012-07-20 09:45:31 -0600

Seen: 5,141 times

Last updated: Jul 21 '12