2016-11-22 18 views
3

私はQtを学び始めてビデオをロードして再生するシンプルなビデオプレーヤーを作ろうとしています。それは完璧にうまくいった。これに閾値処理機能が追加されました。しきい値はspinBoxから取得されます。 コードは、値0(通常のビデオが表示されている場合を除く)を除いて、スピンボックスの値でしきい値処理が行われるように書き込まれます。 だから、これは同じのための私の機能である:スピンボックスの値のグレースケール/バイナリマットを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); 
} 
} 

は0に等しいことは、正常に動作が、スピンボックスの値がインクリメントされたとき、私は唯一の黒い画面を取得します。私はimshow(cv:: Mat binary)を試してみましたが、正しいバイナリイメージを表示していますが、QImage imgを保存しようとすると、元のフレームのサイズと同じですが、ランダムなモノクロピクセルです。

+1

colorTableが不足しているようです。これをwhileループの前に追加してください: 'QVector sColorTable(256);バイナリから 'QImage'を作成した後、' img.setColorTable()を追加してください(int i = 0; i <256; ++ i){sColorTable [i] = qRgb(i、i、i);} sColorTable); ' – Miki

+0

カラー画像入力の場合またはグレースケール画像の入力の場合? afair私はカラーテーブルを使用しなければならなかった。 – Micka

答えて

2

インデックスされた画像のカラーテーブルが不足しているようです。あなたは(whileループの前に)カラーテーブルを追加する必要があります。

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

、あなたが@KubaOberで指摘したように、

img.setColorTable(sColorTable); 

を追加したりする必要がバイナリMatからQImageを作成した後、あなたはすべてのMatにラップすることができ、一般的には

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

:Qtの5.5から、あなたはまた、形式QImage::Format_Grayscale8を使用することができます関数内での変換。下にあるのバグ修正のバージョン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も必要ですか? –

関連する問題