1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| void MainWindow::on_openFileBtn_clicked() { isOpenFile=true; if(capture.isOpened()) { capture.release(); } QString FileName=QFileDialog::getOpenFileName(this,tr("打开文件"),".", tr("Video Files(*.avi *.mp4 *.flv)")); capture.open(FileName.toStdString()); if(capture.isOpened()) { rate=capture.get(CV_CAP_PROP_FPS);
if(isOpenFile) { vd_x=capture.get(CV_CAP_PROP_FRAME_COUNT); ui->videoLong->setRange(0,vd_x); } capture>>frame; if(!frame.empty()) { width_x=ui->ImageLabel->width()*1.0/frame.cols; height_x=ui->ImageLabel->height()*1.0/frame.rows;
cv::resize(frame,frame,Size(),width_x,height_x); image=Mat2QImage(frame); ui->ImageLabel->setPixmap(QPixmap::fromImage(image));
timer=new QTimer(this); timer->setInterval(1000/rate); connect(timer,SIGNAL(timeout()),this,SLOT(nextFrame()));
timer->start(); } }
return; }
QImage Mat2QImage(Mat cvImg) { QImage qImg; if(cvImg.channels()==3) { cvtColor(cvImg,cvImg,CV_BGR2RGB);
qImg=QImage((const unsigned char *)(cvImg.data), cvImg.cols,cvImg.rows, cvImg.cols*cvImg.channels(), QImage::Format_RGB888 ); } else if(cvImg.channels()==1) { qImg=QImage((const unsigned char *)(cvImg.data), cvImg.cols,cvImg.rows, cvImg.cols*cvImg.channels(), QImage::Format_Indexed8 ); } else { qImg=QImage((const unsigned char *)(cvImg.data), cvImg.cols,cvImg.rows, cvImg.cols*cvImg.channels(), QImage::Format_RGB888 ); } return qImg; } void MainWindow::nextFrame() { capture>>frame; if(!frame.empty()) { cv::resize(frame,frame,Size(),width_x,height_x); image=Mat2QImage(frame); ui->ImageLabel->setPixmap(QPixmap::fromImage(image)); if(isOpenFile) { ui->videoLong->setValue((int)(capture.get(CV_CAP_PROP_POS_FRAMES))); } } }
|