I'm试图将像素数据转换为OpenCV Mat对象

I'm trying to convert pixel data to an OpenCV Mat object

本文关键字:转换 数据 像素数 OpenCV 对象 Mat 像素      更新时间:2023-10-16

我有原始像素数据,我想通过opencv-cvShowImage()函数输出。

我有以下代码:

#include <opencv2/highgui/highgui.hpp>
// pdata is the raw pixel data as 3 uchars per pixel
static char bitmap[640*480*3];
memcpy(bitmap,pdata,640*480*3);
cv::Mat mat(480,640,CV_8UC3,bitmap);
std::cout << mat.flags << ", "
          << mat.dims  << ", "
          << mat.rows  << ", "
          << mat.cols  << std::endl;
cvShowImage("result",&mat);

哪个输出:

1124024336, 2, 480, 640

到控制台,但无法使用cvShowImage()输出图像。相反,抛出一个异常,并显示消息:

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat

我怀疑问题出在我创建垫子对象的方式上,但我很难找到关于我应该如何做到这一点的更具体的信息。

我认为CV_8UC3的描述不足以呈现数据数组。难道它不必知道数据是RGB还是YUY2等等吗。?我该如何设置?

尝试cv::imshow("result", mat),而不是混合使用旧的C和新的C++API。我认为将Mat转换为CvArr*是问题的根源。

所以,像这样的东西:

#include <opencv2/highgui/highgui.hpp>
// pdata is the raw pixel data as 3 uchars per pixel
static char bitmap[640*480*3];
memcpy(bitmap,pdata,640*480*3);
cv::Mat mat(480,640,CV_8UC3,bitmap);
std::cout << mat.flags << ", "
          << mat.dims  << ", "
          << mat.rows  << ", "
          << mat.cols  << std::endl;
cv::imshow("result", mat);