Qt OpenCV 学习(三):跟踪视频中的运动物体

在这里插入图片描述

1. mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv2/opencv.hpp>

#include <QTimer>
#include <QPixmap>
#include <QDebug>
#include <QImage>

using namespace cv;
using namespace std;

QT_BEGIN_NAMESPACE
namespace Ui {
    class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow {
   
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_btnPlay_clicked();

    void on_btnStop_clicked();

    void importFrame();

private:
    Ui::MainWindow *ui;

    VideoCapture capture;
    QTimer *timer;
    Mat frame;
    Ptr<BackgroundSubtractorMOG2>ptrMOG;
};
#endif // MAINWINDOW_H

2. mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
   
    ui->setupUi(this);
    this->setWindowTitle("Video Play With Qt");

    // 高斯混合背景处理,创建一个 MOG2 背景提取器并应用
    ptrMOG = createBackgroundSubtractorMOG2(500, 16, false);

    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(importFrame()));
}

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

void MainWindow::on_btnPlay_clicked() {
   
    capture.open("D:\\BaiduNetdiskDownload\\bike.avi");

    double rate = capture.get(CAP_PROP_FPS);
    double timerDelay = 1000 / rate;

    timer->start(timerDelay);
}

void MainWindow::on_btnStop_clicked() {
   
    timer->stop();
    capture.release();
}

void MainWindow::importFrame() {
   
    Mat currentFrame;
    Mat grayFrame, foreGround;

    capture >> currentFrame;
    if (!currentFrame.empty()) {
   
        qDebug() << "Video is running...";
        Mat result = currentFrame.clone();

        ptrMOG->apply(currentFrame, foreGround, 0.05);
        threshold(foreGround, foreGround, 25, 255, THRESH_BINARY);

        // 腐蚀操作消除噪声
        Mat kernelErode = getStructuringElement(MORPH_RECT, Size(5, 5));
        erode(foreGround, foreGround, kernelErode);

        // 膨胀操作突出目标
        Mat kernelDilate = getStructuringElement(MORPH_RECT, Size(11, 11));
        dilate(foreGround, foreGround, kernelDilate);

        // 框选运动目标
        vector<vector<Point>>contours;
        findContours(foreGround, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
        for (uint64 i = 0; i < contours.size(); i++) {
   
            rectangle(result, boundingRect(contours[i]), Scalar(0, 255, 255), 1);
        }
        cvtColor(result, result, COLOR_BGR2RGB);

        QImage showVideo = QImage((const unsigned char*)(result.data), result.cols, result.rows, result.step, QImage::Format_RGB888);
        ui->labelVideo->setPixmap(QPixmap::fromImage(showVideo.scaled(ui->labelVideo->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    } else {
   
        qDebug() << "Play is over.";
        timer->stop();
    }
}

最近更新

  1. TCP协议是安全的吗?

    2023-12-10 12:04:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-10 12:04:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-10 12:04:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-10 12:04:03       18 阅读

热门阅读

  1. vs的生成事件error MSB3073

    2023-12-10 12:04:03       39 阅读
  2. 关于gitBash能使用linux命令删除windows的文件

    2023-12-10 12:04:03       38 阅读
  3. 算能PCIe开发环境搭建-一些记录

    2023-12-10 12:04:03       34 阅读
  4. TCP三次握手、四次挥手及状态转换详解

    2023-12-10 12:04:03       38 阅读
  5. windbg的vscode智能提示js

    2023-12-10 12:04:03       30 阅读
  6. ScheduledThreadPoolExecutor 类定时器弊端和有点

    2023-12-10 12:04:03       32 阅读
  7. facebook广告相比谷歌广告的优势

    2023-12-10 12:04:03       35 阅读
  8. facebook广告运营技巧

    2023-12-10 12:04:03       31 阅读
  9. Linux服务器超级实用的脚本

    2023-12-10 12:04:03       43 阅读