在qpixMap中显示一个CV :: MAT(键入CV_32F)

Display a cv::Mat (type CV_32F) in a QPixmap

本文关键字:CV MAT 键入 32F qpixMap 一个 显示      更新时间:2023-10-16

我正在使用摄像头获取cv :: mat对象的imgtomo1映像。它是CV_32F图像。我试图使用QPIXMAP在Qlabel上展示它。这是我的代码:

   cv::Mat imgTomo;
   imgTomo1.convertTo(imgTomo,CV_8UC1);
  static QVector<QRgb>  sColorTable;
  // only create our color table the first time
 if ( sColorTable.isEmpty() )
   sColorTable.resize( 256 );
       for ( int i = 0; i < 256; ++i )
       {
           sColorTable[i] = qRgb( i, i, >i );
       }
   }
   QImage image( imgTomo.data,
                 imgTomo.cols, imgTomo.rows,
                 static_cast<int>(imgTomo.step),
                 QImage::Format_Indexed8);
   image.setColorTable( sColorTable );
   _afficheImg->setPixmap(QPixmap::fromImage(image));

不幸的是,显示的图像仍然是黑色的。当我是OpenCV的新手时,我的格式有些迷失。我认为转换应该起作用,所以我真的不知道我在误解什么。

编辑:我已删除了fllowning线:

imgtomo1.convertto(imgtomo,cv_8uc1(;

这导致信息丢失。现在,我没有黑屏,但是有些"雪"(我猜是非常奇特的巫婆表格1至0的像素(,我看不到我的相机可以显示什么。

谢谢您的回答,grégoire

我不确定您的代码有什么问题,但是我使用以下代码将cv::Mat映像转换为QImage

if (frame.channels()== 3){
        cv::cvtColor(frame, RGBframe, CV_BGR2RGB);
        img = QImage((const unsigned char*)(RGBframe.data),
                         RGBframe.cols,RGBframe.rows,QImage::Format_RGB888);
}
else
{
        img = QImage((const unsigned char*)(frame.data),
                         frame.cols,frame.rows,QImage::Format_Indexed8);
}

您甚至可以查看Follwing链接,以获取有关如何将Mat图像转换为QImage的更多信息。

1.convert cv_8u type

       Mat Temp;
       CurrentMat.convertTo(Temp, CV_8U);

2.检查通道号:

 int theType = Temp.type();
    int channel_number = (theType / 8) + 1;
    if(channel_number == 4){
        cvtColor(Temp, Temp, CV_BGRA2BGR);
    }

3.您可以使用此代码将MAT类型转换为Qimage:

        QImage putImage(const Mat& mat)
        {
        // 8-bits unsigned, NO. OF CHANNELS=1
        if (mat.type() == CV_8UC1)
        {
            // Set the color table (used to translate colour indexes to qRgb values)
            QVector<QRgb> colorTable;
            for (int i = 0; i < 256; i++)
                colorTable.push_back(qRgb(i, i, i));
            // Copy input Mat
            const uchar *qImageBuffer = (const uchar*)mat.data;
            // Create QImage with same dimensions as input Mat
            QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
            img.setColorTable(colorTable);
            return img;
        }
        // 8-bits unsigned, NO. OF CHANNELS=3
        if (mat.type() == CV_8UC3)
        {
            // Copy input Mat
            const uchar *qImageBuffer = (const uchar*)mat.data;
            // Create QImage with same dimensions as input Mat
            QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
            return img.rgbSwapped();
        }
        else
        {
            qDebug() << "ERROR: Mat could not be converted to QImage.";
            return QImage();
        }
        }