Opencv从相机数据创建mat
opencv create mat from camera data
在我的程序中,我有从相机中获取图像的函数,并且应该将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: %un", image.size().width);
printf("height: %un", image.size().height);
image_.create(Size(image.size().width, image.size().height), CV_8UC1);
image_ = image.clone();
printf("width: %un", image_.size().width);
printf("height: %un", image_.size().height);
}
当我测试我的程序时,我得到image
的width
和height
,但我的全局Mat image_
的width
和height
是0
。我如何才能把数据在我的全局Mat
变量正确?
谢谢。
不幸的是,clone()
和copyTo()
都没有成功地将宽度/高度的值复制到另一个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: %un", any_img.size().width);
printf("height: %un", any_img.size().height);
}
相关文章:
- 从数组创建 Mat 时,'cv::Mat' 和 'int' 类型不兼容
- 如何在 dlib 的 array2d 图像上从 dlib::rectangle 创建 cv::Mat<rgb_pixel> ?
- 在另一个分配的矩阵中为特定范围创建 cv::Mat 标头
- 'cv::Mat::type':非标准语法;使用 '&' 创建指向成员的指针
- 创建opencv Mat到2D网格
- 如何使用模板函数从缓冲区(T* 数据数组)创建 cv::Mat
- OpenCV: cv::Mat 无法从看似有效的数据中创建矩阵
- OpenCV 创建 MAT 并添加元素
- 在 opencv 中从 Mat 图像创建多个子图像?尝试为每个连接的组件创建子图像
- 如何使用avcodec从OpenCV::Mat类型的jpeg图像创建视频
- 在 Java 客户端中创建 Mat 对象,使用 opencv 从C++服务器发送
- C++/cv::Mat相当于使用memcpy创建IplImage
- 将cv::Mat A的每个成员与cv::Mat B合并,以创建一个cv::Mat C,结果为[A(x,y),B(x,y)
- 从指针创建Mat
- OpenCV从堆中的数组创建Mat
- OpenCV -如何从uint8_t指针创建Mat
- Opencv -从数组创建Mat
- Opencv从相机数据创建mat
- 从原始缓冲区数据创建Mat图像
- opencv C++从android NV21图像数据缓冲区创建Mat对象