Ask Your Question
3

How to Create an image from pixels in memory?

asked Jul 20 '12

Lacrymology gravatar image

updated Jul 20 '12

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)

Preview: (hide)

2 answers

Sort by » oldest newest most voted
3

answered Jul 20 '12

AlexanderShishkov gravatar image
uint16_t *depthMap = (uint16_t*)buffer;
int cols = 640;
int rows = 480;
Mat depthFrame( rows, cols, CV_16UC1 );
memcpy( depthFrame.data, depthMap, rows*cols*sizeof(uint16_t) );
Preview: (hide)

Comments

3

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 (Jul 20 '12)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 (Jul 20 '12)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 (Jul 20 '12)edit

how did you know this was a depthFrame?

Lacrymology gravatar imageLacrymology (Jul 20 '12)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 (Jul 21 '12)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 (Jul 23 '12)edit
4

answered Jul 20 '12

Michael Burdinov gravatar image

updated Jul 21 '12

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.

Preview: (hide)

Question Tools

Stats

Asked: Jul 20 '12

Seen: 6,213 times

Last updated: Jul 21 '12