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();
    }
}
相关推荐
乖乖是干饭王24 分钟前
Linux系统编程中的_GNU_SOURCE宏
linux·运维·c语言·学习·gnu
待什么青丝1 小时前
【TMS570LC4357】之相关驱动开发学习记录2
c语言·arm开发·驱动开发·单片机·学习
行云流水剑2 小时前
【学习记录】如何使用 Python 提取 PDF 文件中的内容
python·学习·pdf
AAA废品回收站陈师傅2 小时前
68常用控件_QGroupBox的使用
qt
明月醉窗台2 小时前
qt使用笔记二:main.cpp详解
数据库·笔记·qt
虾球xz2 小时前
CppCon 2015 学习:CLANG/C2 for Windows
开发语言·c++·windows·学习
沉到海底去吧Go3 小时前
【图片自动识别改名】识别图片中的文字并批量改名的工具,根据文字对图片批量改名,基于QT和腾讯OCR识别的实现方案
数据库·qt·ocr·图片识别自动改名·图片区域识别改名·pdf识别改名
蓝婷儿3 小时前
6个月Python学习计划 Day 17 - 继承、多态与魔术方法
开发语言·python·学习
蜉蝣之翼❉3 小时前
opencv如何在仿射变换后保留完整图像内容并自动裁剪
opencv·计算机视觉
持续前进的奋斗鸭4 小时前
Postman测试学习(1)
学习·postman