在Qt中显示浮点图像

To display float point image in Qt

本文关键字:图像 显示 Qt      更新时间:2023-10-16

我在0到1的范围内得到一个浮点图像。我试图使用标签在Qt中显示这个浮动图像,但它不起作用。我检查了QImage格式,发现Qt不支持浮动图像。现在我正试着用这种方法……

            QVector<QRgb> colorTable;
            for (int i = 0; i < 256; i++) 
            colorTable.push_back(qRgb(i, i, i));
             Mat img2;
            DepthImg.convertTo(img2, CV_8UC1);
            assert(img2.isContinuous());
            QImage image = QImage((unsigned char*)(img2.data), DepthImg.cols, DepthImg.rows, QImage::Format_Indexed8);
            image.setColorTable(colorTable);
            ui.ThreeD->setPixmap(QPixmap::fromImage(image, Qt::AutoColor));
            ui.ThreeD->setScaledContents(true);
            qApp->processEvents();
            this->ui.ThreeD->show();

这里的"DepthImage"是一个浮点图像。

 Mat img2;
 DepthImg.convertTo(img2, CV_8UC1);
 assert(img2.isContinuous());
 QImage image = QImage((unsigned char*)img2.data, img2.cols, img2.rows, img2.cols*3, QImage::Format_RGB888);
 ui.ThreeD->setPixmap(QPixmap::fromImage(image, Qt::AutoColor));
 ui.ThreeD->setScaledContents(true);
 qApp->processEvents();
 this->ui.ThreeD->show();

以两种方式执行后,我只得到一个黑色的图像。谁能帮我解决这个问题?

您需要按255倍缩放图像。你可以通过修改

DepthImg.convertTo(img2, CV_8UC1);

DepthImg.convertTo(img2, CV_8UC1, 255.0);