Qt5和opencv常用函数

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 }
取一帧,并把其展示到label
 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 }
摄像头预览函数

 

posted @ 2018-12-01 20:25  墨飞飞  阅读(268)  评论(0编辑  收藏  举报