opencv从相机数据创建垫 [英] opencv create mat from camera data

查看:214
本文介绍了opencv从相机数据创建垫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序我有一个功能,它从相机的图像,应该包装所有的数据 OpenCV Mat 结构。从相机我得到宽度,高度和 unsigned char * 到图像缓冲区。我的问题是如何可以从这些数据创建Mat,如果我有全局Mat变量。 Mat结构的类型为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\\\
,image.size()。width);
printf(height:%u\\\
,image.size()。height);

image_.create(Size(image.size()。width,image.size()。height),CV_8UC1);
image_ = image.clone();

printf(width:%u\\\
,image_.size()。width);
printf(height:%u\\\
,image_.size()。height);
}



当我测试我的程序时,我得到 width , height ,但宽度高度的全局Mat image _ 0



谢谢你。

$ b如果我在全局 Mat 不幸的是, clone() copyTo() code>将width / height的值复制到另一个Mat。至少在OpenCV 2.3!



但是,你可以通过使函数返回 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\\\
,any_img.size()。width);
printf(height:%u\\\
,any_img.size()。height);
}


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.

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?

Thank you.

解决方案

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.

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 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);
}

这篇关于opencv从相机数据创建垫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆