Ask Your Question

Convert void buffer pointer to Mat

asked 2016-10-15 11:50:40 -0600

Ralph058 gravatar image

updated 2016-10-16 00:37:42 -0600

berak gravatar image

I have a module which receives a void pointer to a buffer. The original data type is uint16.

When I try putting the variable into a Mat, I get weird errors from

Mat imBuff = cv::Mat(628, 314, frame, CV_16U);

like missing '}' which isn't missing.

When I try various conversions using reinterpret_cast, I get cast from 'void*' to 'xxx' loses precision. For example,

 Mat imBuff = cv::Mat(628, 314, reinterpret_cast<uint16_t >(frame), CV_16U);

 fives 'uint126_t' {aka short unsigned int}' loses precision

Any ideas on how to solve this?

Or, do I have to go back to the source file and change it there?

thanks Ralph

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-10-15 12:09:29 -0600

berak gravatar image

updated 2016-10-15 12:23:04 -0600

i think, you want to use this constructor here

apart from the 3rd arg reserved to the type (CV_16U) , note, that it's (rows,cols) in opencv, not (width,height)

in the end it will be:

void * data_pointer = ...
Mat m = Mat(314, 628, CV_16U, (uchar*)data_pointer);

you'll also have to very cautios here, since this constructor only makes a shallow copy of your data_pointer (no pixels copied). once, your original pointer gets out of scope, anything is invalid, so please take care, you

  • either stay within the scope of that pointer
  • or clone() the Mat made from it, and pass that on.
edit flag offensive delete link more



I think you are correct but I'm still getting a cast error. I'm working on it.

The indexes were correct for the target image because I am using it in the portrait mode. However, the camera system does not know this. In stead of 'clone', I will use 'transpose.'

Ralph058 gravatar imageRalph058 ( 2016-10-15 16:24:14 -0600 )edit

cv::Mat store it's data in a uchar* pointer, so you'll have to cast to that

berak gravatar imageberak ( 2016-10-16 00:41:07 -0600 )edit

Question Tools

1 follower


Asked: 2016-10-15 11:50:40 -0600

Seen: 6,537 times

Last updated: Oct 16 '16