The article directories

      • Introduction to the
      • The core code
      • The results

Introduction to the

Opencv is an open source computer vision library, very many functions, here briefly introduce OpencV decoding playback Mp4 files, and image display to Qt QLabel above.

The core code

The header file

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include "opencv2/opencv.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>

using namespace std;
using namespace cv;


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow(a);void InitVideo(a);

private slots:
    void on_play_clicked(a);
    void playTimer(a);

    void on_stop_clicked(a);

private:
    Ui::MainWindow *ui;
    QTimer *m_pTimer;
    VideoCapture *m_pVideo;
};

#endif // MAINWINDOW_H
Copy the code

The implementation code

CvtColor (frame, frame, COLOR_BGR2RGB); cvtColor(frame, frame, COLOR_BGR2RGB); QImage disImage = QImage((const unsigned) char*)(frame.data),frame.cols,frame.rows,frame.cols*frame.channels(),QImage::Format_RGB888); ui->label->setPixmap(QPixmap::fromImage(disImage)); // Display the image

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
using namespace cv;
using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_pTimer = new QTimer;
    m_pTimer->setInterval(30);  // Time 30 ms to read a frame of data
    connect(m_pTimer, &QTimer::timeout, this, &MainWindow::playTimer);

    ui->play->setEnabled(true);
    ui->stop->setEnabled(false);

    InitVideo(a); } MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::playTimer(a)
{
    Mat frame;

    // Read a frame from cap and store it in FRAM
    *m_pVideo >> frame;
    if ( frame.empty()) {return;
    }
    cv::cvtColor(frame, frame, COLOR_BGR2RGB);// Image format conversion
    QImage disImage = QImage((const unsigned char*)(frame.data),frame.cols,frame.rows,frame.cols*frame.channels(),QImage::Format_RGB888);
    ui->label->setPixmap(QPixmap::fromImage(disImage));// Display the image
}

void MainWindow::InitVideo(a)
{
    m_pVideo = new VideoCapture("test.mp4");

}

void MainWindow::on_play_clicked(a)
{
    m_pTimer->start(a); ui->play->setEnabled(false);
    ui->stop->setEnabled(true);

}

void MainWindow::on_stop_clicked(a)
{
    ui->play->setEnabled(true);
    ui->stop->setEnabled(false);

    m_pTimer->stop(a); }Copy the code

controls

For testing, the interface is relatively simple, the middle is a QLabel, the following two keys for control playback.

The results

The screen recording tool doesn’t look very good, it’s actually quite sharp.



Wechat Official Account: