使用 QImage::loadFromData 将 cv::mat 转换为 QImage
Convert cv::mat to QImage using QImage::loadFromData
我发现了类似的主题,但没有这个特定的主题。任何想法以下代码有什么问题?它返回'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");
谢谢!
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 data
相关文章:
- 将 cv::mat 转换为 QImage
- Qt-工作线程崩溃时将cv::Mat转换为QImage
- Qt - 将空指针(原始数据)转换为 QImage 并将其显示在标签上
- 警告在将 OpenCV 垫映像投射到 QImage 时使用旧式转换
- 将 QImage 转换为字符*
- 将qimage*转换为void*,反之亦然
- 如何将 QImage 中的像素数据转换为其他格式
- 将命令行参数转换为qimage
- 与iMread相比,qimage to cv ::垫子转换了奇怪的行为
- 如何将灰度/二进制垫子转换为qimage
- 在CV :: MAT和Qimage之间进行转换会使程序崩溃
- 将Qimage转换为cvMat 64FC3格式
- 如何将 QImage 转换为 opencv Mat
- QImage转换返回null
- 将IplImage IPL_DEPTH_32S转换为QImage Format_RGB32的最快方法
- 将 uint16 dicom 映像转换为 qimage
- 如何将 QImage 转换为 QByteArray
- 通过使用原始数据将QImage转换为cv::Mat
- 在QT中将16位QImage转换为8位无符号字符
- 将 QImage 转换为YUV420P像素格式