我正在尝试将像素数据转换为 OpenCV Mat 对象

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

我有想要通过 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

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

我认为 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);