我在我的代码中发现了问题。我在不知不觉中调整了我不应该这样做的标签的大小。删除那行代码后,我解决了我的问题。
我的代码如下:
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();
}