Qt+OpenCV打开视频文件并在窗口界面上显示

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();
}

5、mainwindow.ui内容如下

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>660</width>
    <height>565</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>10</y>
      <width>640</width>
      <height>480</height>
     </rect>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
   <widget class="QWidget" name="">
    <property name="geometry">
     <rect>
      <x>9</x>
      <y>500</y>
      <width>621</width>
      <height>25</height>
     </rect>
    </property>
    <layout class="QHBoxLayout" name="horizontalLayout">
     <item>
      <widget class="QPushButton" name="btnOpenVideo">
       <property name="text">
        <string>打开视频</string>
       </property>
      </widget>
     </item>
     <item>
      <spacer name="horizontalSpacer">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QPushButton" name="btnCloseVideo">
       <property name="text">
        <string>关闭视频</string>
       </property>
      </widget>
     </item>
     <item>
      <spacer name="horizontalSpacer_2">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
    </layout>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>660</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

  • 5
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
可以使用QtOpenCV库来实现自动拍照并保存图片的功能。下面是一个简单的实现步骤: 1.在Qt创建一个图形界面,用于显示摄像头的实时画面。 2.使用OpenCV库来打开摄像头并获取实时画面。 3.添加一个按钮,用于触发拍照功能。 4.当用户点击按钮时,使用OpenCV库来捕获当前的图像,并保存为图片文件。 下面是一个示例代码: ```c++ #include <opencv2/opencv.hpp> #include <QtGui/QImage> #include <QtGui/QPixmap> #include <QtGui/QPushButton> #include <QtGui/QVBoxLayout> #include <QtGui/QWidget> #include <string> using namespace cv; using namespace std; class CameraWidget : public QWidget { Q_OBJECT public: CameraWidget(QWidget *parent = 0) : QWidget(parent) { // 创建布局 QVBoxLayout *layout = new QVBoxLayout(); // 创建显示摄像头画面的标签 m_imageLabel = new QLabel(this); layout->addWidget(m_imageLabel); // 创建拍照按钮 QPushButton *captureButton = new QPushButton("Capture", this); layout->addWidget(captureButton); connect(captureButton, SIGNAL(clicked()), this, SLOT(captureImage())); // 设置布局 setLayout(layout); // 打开摄像头 m_camera = new VideoCapture(0); if (!m_camera->isOpened()) { // 如果打开摄像头失败,给出提示信息 m_imageLabel->setText("Failed to open the camera!"); } else { // 如果摄像头打开成功,启动定时器,定时更新画面 m_timer = new QTimer(this); connect(m_timer, SIGNAL(timeout()), this, SLOT(updateImage())); m_timer->start(30); } } ~CameraWidget() { // 关闭定时器和摄像头 m_timer->stop(); m_camera->release(); } private slots: void updateImage() { // 获取当前帧 Mat frame; *m_camera >> frame; // 将帧转换成QImage格式,并在标签上显示出来 QImage image(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888); QPixmap pixmap = QPixmap::fromImage(image); m_imageLabel->setPixmap(pixmap); } void captureImage() { // 获取当前帧 Mat frame; *m_camera >> frame; // 生成文件名 string filename = "image.jpg"; // 保存为图片文件 imwrite(filename, frame); } private: QLabel *m_imageLabel; VideoCapture *m_camera; QTimer *m_timer; }; int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建窗口显示摄像头画面 CameraWidget *widget = new CameraWidget(); widget->show(); return app.exec(); } ``` 在这个示例代码,我们创建了一个CameraWidget类,用于显示摄像头的实时画面。我们通过调用OpenCV库的VideoCapture类来打开摄像头并获取实时画面,并将其显示在一个QLabel控件上。我们还添加了一个拍照按钮,当用户点击该按钮时,我们使用OpenCV库来捕获当前的图像,并将其保存为图片文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值