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

标签 c++ opencv

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

关于c++ - 我正在尝试将像素数据转换为 OpenCV Mat 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28712063/

相关文章:

c++ - 从空整数到逗号列表中的指针的转换

c++ - 使用 MatGeoLib 进行 3D 碰撞检测?

c++ - 如何在另一台计算机(linux)上使用 OpenCV 运行 C++ 库?

c++ - 在 Vector C++ 中访问指针

c++ - boost 库构建 - 运行时链接和链接选项之间的区别

c++ - STL 复制在 'typedef' 类型的数组上失败

arrays - Opencv:将RGB矩阵转换为一维数组

c++ - 帮助使用扩张函数 OpenCV

python - 使用 matplotlib 显示灰度 OpenCV 图像

c++ - 如何从现有的基类对象创建派生类对象?