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

1. mainwindow.h

cpp 复制代码
#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

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();
    }
}
相关推荐
m0_726365832 分钟前
哈希分分预测系统 打造自适应趋势分析「Python+DeepSeek+PyQt5」
python·qt·哈希算法
微露清风14 分钟前
系统性学习C++-第二十讲-哈希表实现
c++·学习·散列表
星火开发设计1 小时前
C++ queue 全面解析与实战指南
java·开发语言·数据结构·c++·学习·知识·队列
new_zhou1 小时前
vs2019+qt工程中生成dump文件及调试
开发语言·qt·visual studio·dump调试
如果你想拥有什么先让自己配得上拥有2 小时前
近似数的思考学习
学习
hqwest2 小时前
码上通QT实战16--监控页面08-连接后状态处理
qt·串口·信号与槽·serialport·通信过程·打开串口·com1
ha20428941943 小时前
Linux操作系统学习记录之----自定义协议(网络计算器)
linux·网络·学习
振华说技能3 小时前
SolidWorks学习大纲-从基础到全面精通,请看详情
学习
曦月逸霜3 小时前
离散数学-学习笔记(持续更新中~)
笔记·学习·离散数学
im_AMBER3 小时前
Leetcode 101 对链表进行插入排序
数据结构·笔记·学习·算法·leetcode·排序算法