C++ opencv从相机数据创建mat
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6924790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
opencv create mat from camera data
提问by M.K.
in my programm I have function that takes the image from camera and should pack all data in OpenCV Mat
structure. From the camera I get width, height and unsigned char*
to the image buffer. My Question is how I can create Mat from this data, if I have global Mat variable. The type for Mat structure I took CV_8UC1.
在我的程序中,我具有从相机获取图像的功能,并且应该将所有数据打包成OpenCV Mat
结构。从相机我得到宽度、高度和unsigned char*
图像缓冲区。我的问题是,如果我有全局 Mat 变量,我如何从这些数据创建 Mat。我采用了 CV_8UC1 的 Mat 结构类型。
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 right width
and height
of image
, but width
and height
of my global Mat image_
is 0
. How I can put the data in my global Mat
varible correctly?
当我测试的程序,我马上width
和height
的image
,但width
和height
我的全球垫image_
是0
。如何Mat
正确地将数据放入我的全局变量中?
Thank you.
谢谢你。
回答by karlphillip
Unfortunately, neither clone()
nor copyTo()
successfully copy the values of width/height to another Mat. At least on OpenCV 2.3! It's a shame, really.
不幸的是,既没有clone()
也没有copyTo()
成功地将宽度/高度的值复制到另一个垫子上。至少在 OpenCV 2.3 上!真的很可惜。
However, you could solve this issue and simplify your code by making the function return the Mat
. This way you won't need to have a global variable:
但是,您可以通过使函数返回Mat
. 这样你就不需要全局变量了:
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);
}