私は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
を保存しようとすると、元のフレームのサイズと同じですが、ランダムなモノクロピクセルです。
colorTableが不足しているようです。これをwhileループの前に追加してください: 'QVector sColorTable(256);バイナリから 'QImage'を作成した後、' img.setColorTable()を追加してください(int i = 0; i <256; ++ i){sColorTable [i] = qRgb(i、i、i);} sColorTable); ' –
Miki
カラー画像入力の場合またはグレースケール画像の入力の場合? afair私はカラーテーブルを使用しなければならなかった。 – Micka