1、新建一个Qt Widgets Application,工程配置文件(.pro文件)内容如下:
#-------------------------------------------------
#
# Project created by QtCreator 2018-12-11T12:57:12
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtOpenVideo
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += C:\opencv\build\include \
C:\opencv\build\include\opencv \
C:\opencv\build\include\opencv2
CONFIG(debug, debug|release):
{
LIBS += -LC:\opencv\build\x86\vc11\lib\
-lopencv_calib3d249d \
-lopencv_contrib249d \
-lopencv_core249d \
-lopencv_features2d249d \
-lopencv_flann249d \
-lopencv_gpu249d \
-lopencv_highgui249d \
-lopencv_imgproc249d \
-lopencv_legacy249d \
-lopencv_ml249d \
-lopencv_nonfree249d \
-lopencv_objdetect249d \
-lopencv_ocl249d \
-lopencv_photo249d \
-lopencv_stitching249d \
-lopencv_superres249d \
-lopencv_ts249d \
-lopencv_video249d \
-lopencv_videostab249d
}
2、main.cpp内容如下:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
3、mainwiodow.h内容如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
#include <QTimer>
#include <QImage>
#include <QFileDialog>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void ReadFrame();
void on_btnOpenVideo_clicked();
void on_btnCloseVideo_clicked();
private:
Ui::MainWindow *ui;
QTimer *timer;
VideoCapture capture;
Mat frame;
Mat result_frame;
};
#endif // MAINWINDOW_H
4、mainwindow.cpp内容如下
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(ReadFrame()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::ReadFrame()
{
if(capture.isOpened())
{
capture >> frame;
if(!frame.empty())
{
cvtColor(frame, result_frame, CV_BGR2RGB); // OpenCV中Mat读入的图像是BGR格式,要转换为RGB格式
cv::resize(result_frame, result_frame, Size(640, 480));
// 将抓取到的帧,转换为QImage格式。QImage::Format_RGB888不同的摄像头用不同的格式。
QImage image((const uchar*)result_frame.data, result_frame.cols, result_frame.rows, QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(image)); // 将图片显示到label上
ui->label->resize( ui->label->pixmap()->size()); // 将label控件resize到fame的尺寸
}
}
}
void MainWindow::on_btnOpenVideo_clicked()
{
QString file_name = QFileDialog::getOpenFileName(this, tr("Open Video"), ".", tr("Video File(*.avi *.mp4 *.h264)"));
capture.open(file_name.toStdString());
timer->start(25); // 开始计时,每隔25毫秒更新一次,超时则发出timeout()信号
}
void MainWindow::on_btnCloseVideo_clicked()
{
timer->stop(); // 停止读取数据。
// 释放内存;
capture.release();
frame.release();
}