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