Qt+OpenCV打开视频文件并在窗口界面上显示
1、新建一个Qt Widgets Application,工程配置文件(.pro文件)内容如下:
#-------------------------------------------------
#
# Project created by QtCreator 2021-03-19T09:06:07
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled2
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
#工程文件 头文件和库包含进来
INCLUDEPATH += D:/QTproject/opencv_3.4.2_Qt/opencv_3.4.2_Qt/include
LIBS +=D:/QTproject/opencv_3.4.2_Qt/opencv_3.4.2_Qt/x86/bin/libopencv_*.dll
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 <QPushButton>
#include <qmessagebox.h>
#include <qfiledialog.h>
#include <opencv2/opencv.hpp>
#include <QDebug>
#include <QApplication>
#include <QTimer>
using namespace cv;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
//初始化消息框
initConnect();
VideoCapture cap;
//消息框函数
void on_btnOpen_file();
void ReadFrame();
QString path;
QTimer * timer;
private:
Ui::MainWindow *ui;
};
#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);
initConnect();
cap = VideoCapture();
// Mat img=imread("1.jpg");
// imshow("cv",img);
// cvtColor(img,img,CV_BGR2GRAY);
// imshow("cv1",img);
// Mat img_threshold;
// threshold(img,img_threshold,150.0,255.0,THRESH_BINARY);
// imshow("cv2",img_threshold);
// Mat img_erode;//存放腐蚀的图片
// //腐蚀
// Mat elem=getStructuringElement(MORPH_RECT,Size(4,4));
// erode(img_threshold,img_erode,elem);
// imshow("cv3",img_erode);
// //降噪
// medianBlur(img_erode,img_erode,);
// //膨胀
// Mat img_dilate;
// Mat elem2=getStructuringElement(MORPH_RECT,Size(9,9));
// dilate(img_erode,img_dilate,elem2);
// imshow("cv4",img_dilate);
// waitKey();
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow::initConnect()
{
connect(this->ui->pushButton,&QPushButton::clicked,this,&on_btnOpen_file);
connect(timer,&QTimer::timeout,this,&ReadFrame);
}
void MainWindow::on_btnOpen_file()
{
path=QFileDialog::getOpenFileName(this,"打开文件","./","*.*");
this->ui->lineEdit->setText(path);
//读取视频
bool ret=cap.open(path.toStdString().c_str());
timer->start(50);
//cap=VideoCapture(0);
// Mat img=imread(path.toStdString());
// //imshow("img",img);
// QImage im(img.data,img.cols,img.rows,QImage::Format_RGB888);
// this->ui->label_image_before->setPixmap(QPixmap::fromImage(im));
// //模糊后
// blur(img,frame,Size(10,10));
// QImage im1(frame.data,frame.cols,frame.rows,QImage::Format_RGB888);
// this->ui->label_image_after->setPixmap(QPixmap::fromImage(im1));
}
void MainWindow::ReadFrame()
{
Mat frame;
Mat frame1;
Mat frame2;
//opencv读取图片
//qDebug()<<path;
if(cap.isOpened())
{
//qDebug()<<ret;
cap.read(frame);
if(!frame.empty())
{
cvtColor(frame,frame1,CV_BGR2RGB);
cvtColor(frame,frame2,CV_BGR2GRAY);
QImage im(frame1.data,frame1.cols,frame1.rows,QImage::Format_RGB888);
QImage im1(frame2.data,frame2.cols,frame2.rows,QImage::Format_Grayscale8);
//imshow("cv",frame1);
qDebug()<<frame.data;
this->ui->label_image_before->setPixmap(QPixmap::fromImage(im));
this->ui->label_image_after->setPixmap(QPixmap::fromImage(im1));
//waitKey(1000);
qApp->processEvents();
}
}
}