Opencv - 从数组创建Mat

时间:2016-09-09 18:08:32

标签: c++ qt opencv

我尝试从uchar *创建mat对象。我找不到有用的转换。我的代码如下;

uchar* urgbImg; // this value is created another function

Mat img_argb(HEIGHT, WIDTH, CV_8UC4, urgbImg);
Mat img_rgb(HEIGHT, WIDTH, CV_8UC3);
img_argb.convertTo(img_rgb, CV_8UC3);


cv::imwrite("RGB.png", img_rgb);

QImage img1(urgbImg, WIDTH, HEIGHT, QImage::Format_ARGB32);
QImage img2 = img1.convertToFormat(QImage::Format_RGB32);

QFile file2(QString::fromStdString("QRGB.png"));
file2.open(QIODevice::WriteOnly);
img2.save(&file2,"PNG",100);
file2.close();

QRGB文件是一个很好的结果,但不是RGB文件。我有uchar数组所以它是8位。

我尝试了CV_8UC3(没有从CV_8UC4转换),CV_32SC3和CV_32SC4。所有结果都很糟糕。如何从uchar *创建rgb图像?

1 个答案:

答案 0 :(得分:0)

convertTo()不要将CV_8UC4图片转换为CV_8UC3

您应该使用cvtColor()功能

Mat img_argb(HEIGHT, WIDTH, CV_8UC4, urgbImg);
Mat img_rgb;
cvtColor(img_argb,img_rgb,COLOR_BGRA2BGR);