2017-04-27 61 views
0

我正在使用相机来获取作为cv :: Mat对象的imgTomo1图像。这是一张CV_32F图片。 我试图用QPixmap在QLabel上展示它。 这里是我的代码:在QPixmap中显示cv :: Mat(类型CV_32F)

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的新手,所以我在格式上有些迷失。 我认为转换应该起作用,所以我不知道我在做什么。

编辑:我已删除了fllowing行:

imgTomo1.convertTo(imgTomo,CV_8UC1);

它导致信息丢失。 现在我不再有黑屏了,但有些“雪”(我猜猜看像是非常quicly地从1到0的奇怪像素),我真的不能看到我的相机是什么样子显示的。

谢谢您的解答, 格雷

回答

0

我并不确切知道什么是你的代码错误,但我用下面的代码到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 link关于如何Mat图像转换为QImage更多的信息。

+0

谢谢你为你的答案。我设法找出了其中一个问题。实际上使用“imgTomo1.convertTo(imgTomo,CV_8UC1);”我正在将我的float32转换为8位上的intigned int,导致信息丢失。现在,我有一个图像,但它有点搞砸了,在场景中有很多“雪”(我不知道如何放置)。 –

0

1.Convert的CV_8U类型

 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.You可以使用该代码的垫类型转换为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(); 
     } 
     } 
+0

谢谢你的回答。 正如我所说,使用: > CurrentMat.convertTo(Temp,CV_8U); 是我显示的图像全黑的原因。 我已经删除了这一行,我的代码ius和你一样做了相同的事情。 我的问题是,现在,显示的图像似乎充满了某种“噪音”。 –

+0

我明白你的观点。 '雪'点像素很可能是被卡住的像素。由于某些原因,相机上可能会出现像素修改。我的意思是,它来自相机。欲了解更多信息,你可以看看这个[链接](https://photographylife.com/dead-vs-stuck-vs-hot-pixels/) –