2016-11-22 268 views
3

我已经开始学习Qt,并试图制作一个简单的视频播放器,它将加载视频并播放它。它工作得很好。现在添加了阈值功能。阈值将从spinBox中获得。 代码的写入方式是除了值为0(显示正常视频)之外,阈值操作将在spinBox中以值的形式完成。 所以这是我的同一个函数:如何将灰度/二进制Mat转换为QImage?

void Player::run() 
{ 

while(!stop) 
{ 

    if(!capture.read(frame)) 
     stop = true; 
    // convert RGB to gray 
    if(frame.channels() == 3) 
    { 
     if(thresh == 0) 
     { 
      cvtColor(frame, RGBframe, CV_BGR2RGB); 
      img = QImage((const unsigned char*)(RGBframe.data), 
          RGBframe.cols,RGBframe.rows,QImage::Format_RGB888); 

     } 
     else 
     { 
      Mat temp; 
      cvtColor(frame, temp, CV_BGR2GRAY); 
      threshold(temp, binary, thresh, 255, 0); 
      img = QImage((const unsigned char*)(binary.data), 
           binary.cols, binary.rows, QImage::Format_Indexed8); 




      bool save = img.save("/home/user/binary.png"); 

      cout<<"threshold value = "<<thresh<<endl; 

      //imshow("Binary", binary); 
     } 
    } 
    else 
    { 
     if(thresh == 0) // original Image 
     { 
      img = QImage((const unsigned char*)(frame.data), 
          frame.cols,frame.rows,QImage::Format_Indexed8); 

     } 
     else // convert to Binary Image 
     { 

      threshold(frame, binary, thresh, 255, 0); 

      img = QImage((const unsigned char*)(binary.data), 
           binary.cols, binary.rows, QImage::Format_Indexed8); 
     } 




    } 

    emit processedImage(img); 
    this->msleep(delay); 
} 
} 

为SPINBOX值等于0,则运行正常,但当SPINBOX值增加我只得到黑屏。我试过imshow(cv:: Mat binary),它显示正确的二进制图像,但当我尝试保存QImage img时,它是一些随机的黑白像素(尽管原始帧的大小相同)。

+1

看来你缺少一个colorTable。在while循环之前添加:'QVector sColorTable(256);对于(int i = 0; i <256; ++ i){sColorTable [i] = qRgb(i,i,i);}',并且在从二进制文件创建'QImage'后,添加'img.setColorTable sColorTable);' – Miki

+0

彩色图像输入案例还是灰度图像输入案例? afair灰度不是在Qt中索引的类型,afair我不得不使用颜色表。 – Micka

回答

2

看起来你错过了索引图像的颜色表。您需要添加一个颜色表(前while循环):

QVector<QRgb> sColorTable(256); 
for (int i = 0; i < 256; ++i){ sColorTable[i] = qRgb(i, i, i); } 

,你从二进制Mat创建QImage后,你需要添加

img.setColorTable(sColorTable); 

或者,正如@KubaOber指出,从Qt的5.5,你也可以使用格式QImage::Format_Grayscale8

// From Qt 5.5 
QImage image(inMat.data, inMat.cols, inMat.rows, 
      static_cast<int>(inMat.step), 
      QImage::Format_Grayscale8); 

一般情况下,你可以用所有MatQImage函数中的转换。下面是错误更正已更新版本cvMatToQImage最初发现here

然后,您可以从您的代码中删除所有转换为QImage,并改用此函数。

QImage cvMatToQImage(const cv::Mat &inMat) 
{ 
    switch (inMat.type()) 
    { 
     // 8-bit, 4 channel 
    case CV_8UC4: 
    { 
     QImage image(inMat.data, 
      inMat.cols, inMat.rows, 
      static_cast<int>(inMat.step), 
      QImage::Format_ARGB32); 

     return image; 
    } 

    // 8-bit, 3 channel 
    case CV_8UC3: 
    { 
     QImage image(inMat.data, 
      inMat.cols, inMat.rows, 
      static_cast<int>(inMat.step), 
      QImage::Format_RGB888); 

     return image.rgbSwapped(); 
    } 

    // 8-bit, 1 channel 
    case CV_8UC1: 
    { 
#if QT_VERSION >= 0x050500 

     // From Qt 5.5 
     QImage image(inMat.data, inMat.cols, inMat.rows, 
        static_cast<int>(inMat.step), 
        QImage::Format_Grayscale8); 
#else 
     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(inMat.data, 
      inMat.cols, inMat.rows, 
      static_cast<int>(inMat.step), 
      QImage::Format_Indexed8); 

     image.setColorTable(sColorTable); 
#endif 
    } 

    default: 
     qWarning() << "cvMatToQImage() - cv::Mat image type not handled in switch:" << inMat.type(); 
     break; 
    } 

    return QImage(); 
} 
+0

感谢这个附加功能@Miki –

+1

@ Optimus1072很高兴帮助。我更新了问题的标题以更好地反映问题并帮助其他人找到此解决方案 – Miki

+0

对于灰度图像,我们还需要colorMap吗? –

相关问题