convert Mat to QImage
1 void MainWindow::nextFrame() 2 { 3 camera>>frame;//已在头文件里声明camera和frame 4 if(!frame.empty()) 5 { 6 image=Mat2QImage(frame); 7 ui->label->setPixmap(QPixmap::fromImage(image)); 8 } 9 }
1 void MainWindow::on_pushButton_2_clicked() 2 { 3 if (capture.isOpened()) 4 capture.release(); //decide if capture is already opened; if so,close it 5 capture.open(0); //open the default camera 6 if (capture.isOpened()) 7 { 8 rate= capture.get(CV_CAP_PROP_FPS); 9 capture >> frame; 10 if (!frame.empty()) 11 { 12 13 image = Mat2QImage(frame); 14 ui->label->setPixmap(QPixmap::fromImage(image)); 15 QTimer *timer = new QTimer(this); 16 timer->setInterval(1000/rate); //set timer match with FPS 17 connect(timer, SIGNAL(timeout()), this, SLOT(nextFrame())); 18 timer->start(); 19 } 20 } 21 }