2

Qt Creator でフォームをデザインしました。私は3つのコントロールを持っています。

  • グループ ボックス (下部のコントロール)
  • ウィジェット (中央のコントロール)
  • ラベル (最上部のコントロール)

ラベルでビデオのフレームを表示/再生します(適切な方法ではないかもしれませんが、Qtを初めて使用するので、当面はうまくいきます)。

私のコントロールは固定サイズです。

私の質問は、コントロール (つまり、ウィジェットまたはラベル) に従ってビデオのサイズを変更するにはどうすればよいですか? すべてのビデオをウィジェットまたはラベルと同じサイズで再生したい (どちらも同じサイズ)。

わかりやすくするために、プログラムの 2 つのスナップショットがあります。

2番目のビデオ(小さい方)を最初のビデオと同じもので再生したい。または、ビデオがウィジェットよりも大きい場合は、ラベルのサイズに縮小する必要があります。ストレッチと縮小のようなものです。

両方のビデオで私のウィジェットの境界線を見ることができます。

どんな解決策でも大歓迎です。前もって感謝します

最初のビデオ(通常のもの)

これは2番目のビデオです(サイズが小さい)

4

2 に答える 2

3

myLabel.setScaledContents(true)するべきです。

于 2012-06-01T06:40:12.393 に答える
1

コードに問題が見つかりました。無意識のうちにラベルのサイズを変更していましたが、そうする必要はありませんでした。そのコード行を削除した後、私は問題を解決しました。

私のコードは次のとおりです。

 std::string fileName = QFileDialog::getOpenFileName(this,tr("Open Video"), ".",tr("Video Files (*.mp4 *.avi)")).toStdString();

 cv::VideoCapture capture(fileName);

 if (!capture.isOpened())
 {
     QMessageBox msgBox;
     msgBox.setWindowTitle("Error");
     msgBox.setIcon(QMessageBox::Critical);
     msgBox.setButtonText(2,"OK");
     msgBox.setText("The video was not loaded!");
     msgBox.exec();
 }

 Mat cur_frame, pre_frame; // current video frame

 capture.read(imgsize);

 cv::Mat out(imgsize.rows,imgsize.cols,CV_8SC1);
 cv::Mat out2(imgsize.rows,imgsize.cols,CV_8SC1);

     Mat cur_frame_gray(imgsize.rows,imgsize.cols,CV_8SC1);

if (!paused) {
         // read next frame if any
         if (!capture.read(cur_frame)) break;

         cvtColor(cur_frame,cur_frame_gray,CV_RGB2GRAY);

         QCoreApplication::processEvents();

         //read second frame if any
         if(!capture.read(pre_frame)) break;
         cvtColor(pre_frame,pre_frame_gray,CV_RGB2GRAY);

         QCoreApplication::processEvents();

         Mat resizedframe1,resizedframe2,resizedframe3,resizedframe4; // to contain resized image

         cvtColor(cur_frame,cur_frame,CV_BGR2RGB);

        // here I am dividing the rows and columns of the frames by 2 to make them smaller. 
         cv::resize(cur_frame,resizedframe1,cv::Size(cur_frame.cols/2,cur_frame.rows/2));

         IplImage  myFrame=resizedframe4;

        Mat matFrame=&myFrame;

         QImage img1= QImage((const unsigned char*)(resizedframe1.data),resizedframe1.cols,resizedframe1.rows,QImage::Format_RGB888);

         //display on label
         //the label stretches and shrinks according to the frame of the video.

         ui->label->setScaledContents(true);
         ui->label->setPixmap(QPixmap::fromImage(img1));

}
// resize the label to fit the image
         // The problem was here, I was resizing the label but I shouldn't have to do it.
         // by removing the following line of code, my problem was solved :)
           ui->label->resize(ui->label->pixmap()->size());

         QCoreApplication::processEvents();

         waitKey(delay);

 }

         // Close the video file
         capture.release();

}
于 2012-06-01T14:49:55.333 に答える