vibe前景提取算法示例代码
//ViBe.h#pragma once
#include <iostream>
#include "opencv2/opencv.hpp"using namespace cv;
using namespace std;#define NUM_SAMPLES 20 //每个像素点的样本个数
#define MIN_MATCHES 2 //#min指数
#define RADIUS 20 //Sqthere半径
#define SUBSAMPLE_FACTOR 16 //子采样概率class ViBe_BGS
{
public:ViBe_BGS(void);~ViBe_BGS(void);void init(const Mat _image); //初始化void processFirstFrame(const Mat _image);void testAndUpdate(const Mat _image); //更新Mat getMask(void){ return m_mask; };private:Mat m_samples[NUM_SAMPLES];Mat m_foregroundMatchCount;Mat m_mask;
};
//ViBe.cpp#include <opencv2/opencv.hpp>
#include <iostream>
#include "ViBe.h"using namespace std;
using namespace cv;static int c_xoff[9] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 }; //x的邻居点
static int c_yoff[9] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 }; //y的邻居点ViBe_BGS::ViBe_BGS(void)
{}
ViBe_BGS::~ViBe_BGS(void)
{}/**************** Assign space and init ***************************/
void ViBe_BGS::init(const Mat _image)
{for (int i = 0; i < NUM_SAMPLES; i++){m_samples[i] = Mat::zeros(_image.size(), CV_8UC1);}m_mask = Mat::zeros(_image.size(), CV_8UC1);m_foregroundMatchCount = Mat::zeros(_image.size(), CV_8UC1);
}/**************** Init model from first frame ********************/
void ViBe_BGS::processFirstFrame(const Mat _image)
{RNG rng;int row, col;for (int i = 0; i < _image.rows; i++){for (int j = 0; j < _image.cols; j++){for (int k = 0; k < NUM_SAMPLES; k++){// Random pick up NUM_SAMPLES pixel in neighbourhood to construct the modelint random = rng.uniform(0, 9);row = i + c_yoff[random];if (row < 0)row = 0;if (row >= _image.rows)row = _image.rows - 1;col = j + c_xoff[random];if (col < 0)col = 0;if (col >= _image.cols)col = _image.cols - 1;m_samples[k].at<uchar>(i, j) = _image.at<uchar>(row, col);}}}
}/**************** Test a new frame and update model ********************/
void ViBe_BGS::testAndUpdate(const Mat _image)
{RNG rng;for (int i = 0; i < _image.rows; i++){for (int j = 0; j < _image.cols; j++){int matches(0), count(0);float dist;while (matches < MIN_MATCHES && count < NUM_SAMPLES){dist = abs(m_samples[count].at<uchar>(i, j) - _image.at<uchar>(i, j));if (dist < RADIUS)matches++;count++;}if (matches >= MIN_MATCHES){// It is a background pixelm_foregroundMatchCount.at<uchar>(i, j) = 0;// Set background pixel to 0m_mask.at<uchar>(i, j) = 0;// 如果一个像素是背景点,那么它有 1 / defaultSubsamplingFactor 的概率去更新自己的模型样本值int random = rng.uniform(0, SUBSAMPLE_FACTOR);if (random == 0){random = rng.uniform(0, NUM_SAMPLES);m_samples[random].at<uchar>(i, j) = _image.at<uchar>(i, j);}// 同时也有 1 / defaultSubsamplingFactor 的概率去更新它的邻居点的模型样本值random = rng.uniform(0, SUBSAMPLE_FACTOR);if (random == 0){int row, col;random = rng.uniform(0, 9);row = i + c_yoff[random];if (row < 0)row = 0;if (row >= _image.rows)row = _image.rows - 1;random = rng.uniform(0, 9);col = j + c_xoff[random];if (col < 0)col = 0;if (col >= _image.cols)col = _image.cols - 1;random = rng.uniform(0, NUM_SAMPLES);m_samples[random].at<uchar>(row, col) = _image.at<uchar>(i, j);}}else{// It is a foreground pixelm_foregroundMatchCount.at<uchar>(i, j)++;// Set background pixel to 255m_mask.at<uchar>(i, j) = 255;//如果某个像素点连续N次被检测为前景,则认为一块静止区域被误判为运动,将其更新为背景点if (m_foregroundMatchCount.at<uchar>(i, j) > 50){int random = rng.uniform(0, SUBSAMPLE_FACTOR);if (random == 0){random = rng.uniform(0, NUM_SAMPLES);m_samples[random].at<uchar>(i, j) = _image.at<uchar>(i, j);}}}}}
}
//ViBeMain.cpp#include "opencv2/opencv.hpp"
#include "ViBe.h"
#include <iostream>
#include <cstdio>using namespace cv;
using namespace std;int main(int argc, char* argv[])
{Mat frame, gray, mask;VideoCapture capture;capture.open("MAH00054.MP4");if (!capture.isOpened()){cout << "No camera or video input!\n" << endl;return -1;}ViBe_BGS Vibe_Bgs;int count = 0;while (1){count++;capture >> frame;if (frame.empty())break;cvtColor(frame, gray, CV_RGB2GRAY);if (count == 1){Vibe_Bgs.init(gray);Vibe_Bgs.processFirstFrame(gray);cout << " Training GMM complete!" << endl;}else{Vibe_Bgs.testAndUpdate(gray);mask = Vibe_Bgs.getMask();morphologyEx(mask, mask, MORPH_OPEN, Mat());imshow("mask", mask);}imshow("input", frame);if (cvWaitKey(10) == 'q')break;}return 0;
}
vibe前景提取算法示例代码相关推荐
- OpenCV4中有哪些视频背景/前景分割(背景建模/前景提取)算法的类,它们各自的算法原理、特点是什么,并附示例代码
关于OpenCV4中有哪些视频背景/前景分割(背景建模/前景提取)算法的类,汇总如下: 上面的汇总不仅显示了OpenCV4中有哪些视频背景/前景分割(背景建模/前景提取)算法的类,还显示了它们的继承. ...
- OpenCV3.0中有哪些视频背景/前景分割(背景建模/前景提取)算法的类,它们各自的算法原理、特点是什么,并附示例代码
关于OpenCV3中有哪些视频背景/前景分割(背景建模/前景提取)算法的类,汇总如下: cv::Algorithm cv::BackgroundSubtractor cv::Backgro ...
- cart算法示例代码
以下是基于sklearn库的CART算法示例代码.通过构建决策树(采用Gini作为指标)对随机生成(通过np.random.randint方法)的数字进行分类,自变量X为100×4的矩阵,随机生成的数 ...
- ViBe 背景提取算法原理
本文由原始文档的部分章节翻译得到: ViBe: A universal background subtraction algorithm for video sequences Olivier Bar ...
- vibe前景提取改进算法
// improveVibeAlgorithm.h #ifndef IMPROVED_VIBE_ALGORITHM_H #define IMPROVED_VIBE_ALGORITHM_H#includ ...
- 寻路机器人单片机程序示例_人工智能: 自动寻路算法实现(D、D*算法)(示例代码)...
博客转载自:https://blog.csdn.net/kongbu0622/article/details/1871520 据 Drew 所知最短路经算法现在重要的应用有计算机网络路由算法,机器人探 ...
- 常用加密与解密算法示例代码
一.System.Security.Cryptography 命名空间 System.Security.Cryptography 命名空间提供加密服务,包括安全编码和解码的数据,以及许多其他操作,如哈 ...
- topsis法matlab程序,TOPSIS算法(示例代码)
title: TOPSIS算法 date: 2020-02-24 11:18:06 categories: 数学建模 tags: [评价模型, MATLAB] mathjax: true 定义 ? C ...
- 图像的论轮廓提取算法与代码
第7章 边沿检测与提取,轮廓跟踪 我们在第三章介绍平滑与锐化时引入了模板操作,今天还要用到它. 7.1 边沿检测 我们给出一个模板 和一幅图象 .不难发现原图中左边暗,右边亮,中间存在着一条明显的 ...
最新文章
- c++map的使用_mybatis源码 | mybatis插件及动态代理的使用
- java id pid_根据id,pid生成树结构,再生成id,pid形式
- 886 微型计算机原理,886微型计算机原理及应用.pdf
- m文件中函数的执行顺序
- rhel7.0安装篇
- 用iframe框架,登录过期后登录框在框架里加载的问题
- 如何确保分布式场景下的并发幂等性?
- 如何部署WAR到tomcat?
- MinGW gcc下载链接及sjlj、dwarf、seh异同以及gcc安装
- Android车牌识别sdk开发包,基于Android和iOS平台的车牌识别SDK开发包
- 记一次成功修复U盘的痛苦经历
- WannaCry勒索病毒分析
- Godaddy Backorder 失败后申请退款。
- 图数据库——Nebula Graph安装部署(含示例)
- PIC 1508 TIM1的定时器中断使用
- HDMI设计1--HDMI 1.4b SPEC的阅读个人总结
- 《信息物理融合系统(CPS)设计、建模与仿真——基于 Ptolemy II 平台》——2.6 类和继承...
- Love Your Life》—— 热爱生活
- 我工作时戴耳机是为了治疗电脑
- XSS-Libs通关详解