2017-04-27 59 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/) –