Opencv create a rug from camera data

in my program, I have a function that takes an image from the camera and should pack all the data into an OpenCV Mat structure. From the camera, I get the width, height and unsigned char* in the image buffer. My question is how can I create a Mat from this data if I have a global variable called Mat. Mat structure type I took CV_8UC1.

 Mat image_; function takePhoto() { unsigned int width = getWidthOfPhoto(); unsinged int height = getHeightOfPhoto(); unsinged char* dataBuffer = getBufferOfPhoto(); Mat image(Size(width, height), CV_8UC1, dataBuffer, Mat::AUTO_STEP); printf("width: %u\n", image.size().width); printf("height: %u\n", image.size().height); image_.create(Size(image.size().width, image.size().height), CV_8UC1); image_ = image.clone(); printf("width: %u\n", image_.size().width); printf("height: %u\n", image_.size().height); } 

When I test my program, I get the width and height rights from image , but the width and height my global Mat image_ is 0 . How can I correctly put data into my global Mat varible?

Thanks.

+8
c ++ opencv
source share
1 answer

Unfortunately, neither clone() nor copyTo() succeeded in successfully copying the width / height values ​​to another Mat. At least on OpenCV 2.3! This is a shame, really.

However, you can solve this problem and simplify your code by forcing the function to return Mat . This way you do not need to have a global variable:

 Mat takePhoto() { unsigned int width = getWidthOfPhoto(); unsigned int height = getHeightOfPhoto(); unsigned char* dataBuffer = getBufferOfPhoto(); Mat image(Size(width, height), CV_8UC1, dataBuffer, Mat::AUTO_STEP); return Mat; } int main() { Mat any_img = takePhoto(); printf("width: %u\n", any_img.size().width); printf("height: %u\n", any_img.size().height); } 
+14
source share

All Articles