我已经找到了类似的主题,但不是这个主题。你知道下面的代码有什么问题吗?它返回‘loaded =false’,这显然意味着不能加载图像数据。
cv::Mat mymat;
QImage qimg;
mymat = cv::imread("C:\\testimages\\img1.png");
int len = mymat.total()*mymat.elemSize();
bool loaded = qimg.loadFromData((uchar*)mymat.data, len, "PNG");谢谢!
发布于 2013-10-03 07:54:29
cv::Mat类型保存已解码的图像。因此,您不能要求QImage使用loadFromData()方法再次解码它。
您需要设置一个格式与元素类型匹配的图像,该图像使用Mat::type()检索,然后将原始数据从矩阵复制到图像,或者为数据设置QImage头。在OpenCV术语中,“头”指的是不保存自己数据的对象。
下面的代码设置了一个头QImage。
QMap<int, QImage::Format> fmtMap;
fmtMap.insert(CV8_UC4, QImage::Format_ARGB32);
fmtMap.insert(CV_8UC3, QImage::Format_RGB888)
// We should convert to a color image since 8-bit indexed images can't be painted on
cv::Mat mat = cv::imread("C:\\testimages\\img1.png", CV_LOAD_IMAGE_COLOR);
Q_ASSERT(fmtMap.contains(cvImage.type());
Q_ASSERT(mat.isContinuous());
QImage img(cvImage.data, cvImage.cols, cvImage.rows, fmtMap[cvImage.type()]);
// The matrix *must* outlive the image, otherwise the image will access a dangling pointer to data使用QImage加载图像,然后为其创建标题矩阵可能会更容易。
QMap<QImage::Format, int> fmtMap;
fmtMap.insert(QImage::Format_ARGB32, CV8_UC4);
fmtMap.insert(QImage::Format_RGB32, CV8_UC4);
fmtMap.insert(QImage::Format_RGB888, CV_8UC3);
QImage img;
img.load("C:\\testimages\\img1.png");
Q_ASSERT(fmtMap.contains(img.format()));
cv::Mat mat(img.height(), img.width(), fmtMap[img.format()], img.bits());
// The image *must* outlive the matrix, otherwise the matrix will access a dangling pointer to datahttps://stackoverflow.com/questions/19145424
复制相似问题