赞
踩
【vibe算法介绍】
ViBe算法是一种高效的像素级视频背景建模和前景检测算法。以下是对该算法的详细介绍:
一、算法原理
ViBe算法的核心思想是通过为每个像素点存储一个样本集,利用该样本集与当前像素值进行比较,从而判断该像素是否属于背景。算法的主要步骤包括背景模型的初始化、前景检测过程和背景模型的更新方法。
二、算法优点
三、算法挑战与限制
综上所述,ViBe算法是一种高效、鲁棒的像素级视频背景建模和前景检测算法,具有广泛的应用前景。然而,在实际应用中仍需注意其挑战与限制,并进行相应的优化和改进。
【sort track算法介绍】
SORT(Simple Online and Realtime Tracking)算法是一种简单、高效且实用的多目标跟踪算法。以下是对SORT算法的介绍,内容将按照清晰的格式分点表示,并尽可能参考文章中的相关数字和信息:
SORT算法以其简单、高效和实时性强的特点,在多目标跟踪领域具有广泛的应用前景。然而,在实际应用中仍需注意其处理遮挡和目标外观特征方面的限制,并根据具体需求进行相应的优化和改进。
【测试环境】
vs2019
opencv==4.8.0
x64 release(Debug暂时没测试通过)
【部分实现源码】
- #define _CRT_SECURE_NO_WARNINGS
- #include "tracker.hpp"
- #include "trajectory.hpp"
- #include "utils.hpp"
- #include "vibe_sequential.hpp"
- #include <array>
- #include <chrono>
- #include <cmath>
- #include <cstddef>
- #include <cstdio>
- #include <cstdlib>
- #include <memory>
- #include <opencv2/core.hpp>
- #include <opencv2/imgcodecs.hpp>
- #include <opencv2/imgproc.hpp>
- #include <opencv2/highgui.hpp>
- #include <stdexcept>
- #include <string>
- #include <string_view>
- #include <thread>
- using namespace std;
- using namespace cv;
- int main(int argc, char* argv[]) {
-
-
-
- int maxNumBlobs = 64;
- VideoCapture capture("test.mp4");
- if (!capture.isOpened())
- return -1;
- double fps = capture.get(cv::CAP_PROP_FPS); // 帧率
- int width = capture.get(cv::CAP_PROP_FRAME_WIDTH); // 视频帧宽度
- int height = capture.get(cv::CAP_PROP_FRAME_HEIGHT); // 视频帧高度
- // Create vibe algorithm instance
- auto vibe = std::make_unique<ViBeSequential>(height, width, 14, 20, 2, 5);
- // Create tracker instance
- auto tracker = std::make_unique<SortTracker>(3, 3);
-
- auto detections = std::vector<cv::Rect2f>(8);
- cv::Mat fgMask(height, width, CV_8U);
- cv::Mat updateMask(height, width, CV_8U);
- cv::Mat fgBlobLabels(height, width, CV_32S);
- cv::Mat fgBlobCentroids(64, 2, CV_64F);
- cv::Mat fgBlobStats(64, 5, CV_32S);
-
- // Prepare structure elements for morphological filtering
- cv::Mat se3x3 = cv::getStructuringElement(cv::MORPH_ELLIPSE, { 3, 3 });
- cv::Mat se5x5 = cv::getStructuringElement(cv::MORPH_ELLIPSE, { 5, 5 });
- cv::Mat se7x7 = cv::getStructuringElement(cv::MORPH_ELLIPSE, { 7, 7 });
-
- // Prepare runtime measurement
- auto tm = cv::TickMeter();
-
-
-
- while (true)
- {
- Mat frame;
- capture >> frame; // 从相机读取新一帧
- if (frame.empty())
- {
- break;
- }
-
- /* Segmentation and update. */
- tm.reset();
- tm.start();
- // Run background segmentation with ViBe
- vibe->segment(frame, fgMask);
-
- // Process update mask
- cv::morphologyEx(fgMask, updateMask, cv::MORPH_OPEN, se3x3);
-
- // Update ViBe
- vibe->update(frame, updateMask);
-
- // Post-processing on foreground mask
- cv::morphologyEx(fgMask, fgMask, cv::MORPH_OPEN, se3x3);
- cv::morphologyEx(fgMask, fgMask, cv::MORPH_CLOSE, se5x5);
-
- tm.stop();
-
- double vibeProcessTimeMs = tm.getTimeMilli();
-
- // Find all connected components
- int numFgBlobs = cv::connectedComponentsWithStats(
- fgMask, fgBlobLabels, fgBlobStats, fgBlobCentroids);
-
- if (numFgBlobs > maxNumBlobs) {
- // Too many blobs, consider this frame invalid
-
- tracker->clear();
- continue;
- }
-
- detections.clear();
- for (int i = 1; i < numFgBlobs; i++) {
- auto* blobStat = fgBlobStats.ptr<int>(i);
-
- int x = blobStat[cv::CC_STAT_LEFT] - 6;
- int y = blobStat[cv::CC_STAT_TOP] - 6;
- int w = blobStat[cv::CC_STAT_WIDTH] + 12;
- int h = blobStat[cv::CC_STAT_HEIGHT] + 12;
- // int a = blobStat[cv::CC_STAT_AREA];
-
- // Add new bbox
- detections.emplace_back(x, y, w, h);
-
- // auto color = colors.row(i % colors.rows);
- cv::rectangle(frame, { x, y, w, h }, { 255, 50, 0 }, 1);
- }
-
- tm.reset();
- tm.start();
-
- // Update tracker with newly detected bboxes
- tracker->update(detections, frame);
-
- tm.stop();
- double trackingTimeMs = tm.getTimeMilli();
-
- std::array<char, 64> str;
- std::sprintf(str.data(),
- "[PROCESS TIME] ViBe: %.2f ms, Tracking: %.2f",
- vibeProcessTimeMs,
- trackingTimeMs);
-
- // Draw process time measurement result on current frame
- cv::putText(frame,
- str.data(),
- { 12, 30 },
- cv::FONT_HERSHEY_SIMPLEX,
- 0.5,
- { 0, 0, 255 },
- 1,
- cv::LINE_AA);
-
-
- cv::imshow("frame", frame);
- cv::imshow("fgmask", fgMask);
- cv::imshow("update mask", updateMask);
-
- if (char(waitKey(2)) == 'q')
- {
-
- break;
- }
- }
-
- capture.release();
- cv::destroyAllWindows();
-
-
- }
【效果展示】
【视频演示】
【源码下载地址】https://download.csdn.net/download/FL1623863129/89403409
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。