运动目标前景检测之ViBe源代码分析
一方面为了学习,一方面按照老师和项目的要求接触到了前景提取的相关知识,具体的方法有很多,帧差、背景减除(GMM、CodeBook、 SOBS、 SACON、 VIBE、 W4、多帧平均……)、光流(稀疏光流、稠密光流)、运动竞争(Motion Competition)、运动模版(运动历史图像)、时间熵……等等。
更为具体的资料可以参考一下链接,作者做了很好的总结。点击打开链接http://blog.csdn.net/zouxy09/article/details/9622285
我只要针对作者提供的源代码,加上我的理解最代码捉了做了相关的注释,便于自己对代码的阅读和与大家的交流,如果不妥之处,稀罕大家多多提出,共同进步
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;
- int c_xoff[9] = {-1, 0, 1, -1, 1, -1, 0, 1, 0}; //x的邻居点,9宫格
- 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++) //可以这样理解,针对一帧图像,建立了20帧的样本集
- {
- m_samples[i] = Mat::zeros(_image.size(), CV_8UC1); //针对每一帧样本集的每一个像素初始化为8位无符号0,单通道
- }
- 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 model
- int random = rng.uniform(0, 9); //随机产生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) //逐个像素判断,当匹配个数大于阀值MIN_MATCHES,或整个样本集遍历完成跳出
- {
- dist = abs(m_samples[count].at<uchar>(i, j) - _image.at<uchar>(i, j)); //当前帧像素值与样本集中的值做差,取绝对值
- if (dist < RADIUS) //当绝对值小于阀值是,表示当前帧像素与样本值中的相似
- matches++;
- count++; //取样本值的下一个元素作比较
- }
- if (matches >= MIN_MATCHES) //匹配个数大于阀值MIN_MATCHES个数时,表示作为背景
- {
- // It is a background pixel
- m_foregroundMatchCount.at<uchar>(i, j) = 0; //被检测为前景的个数赋值为0
- // Set background pixel to 0
- m_mask.at<uchar>(i, j) = 0; //该像素点值也为0
- // 如果一个像素是背景点,那么它有 1 / defaultSubsamplingFactor 的概率去更新自己的模型样本值
- int random = rng.uniform(0, SUBSAMPLE_FACTOR); //以1 / defaultSubsamplingFactor概率跟新背景
- 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 //匹配个数小于阀值MIN_MATCHES个数时,表示作为前景
- {
- // It is a foreground pixel
- m_foregroundMatchCount.at<uchar>(i, j)++;
- // Set background pixel to 255
- m_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);
- }
- }
- }
- }
- }
- }
main.cpp(你懂的……
)
- #include <opencv2/opencv.hpp>
- #include "ViBe.h"
- #include <iostream>
- #include <cstdio>
- #include<stdlib.h>
- using namespace cv;
- using namespace std;
- int main(int argc, char* argv[])
- {
- Mat frame, gray, mask;
- VideoCapture capture;
- capture.open("E:\\overpass\\11.avi");
- 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;
- }
- system("pause");
- return 0;
- }
运动目标前景检测之ViBe源代码分析的更多相关文章
- [转]前景检测算法--ViBe算法
原文:http://blog.csdn.net/zouxy09/article/details/9622285 转自:http://blog.csdn.net/app_12062011/article ...
- OpenCV:OpenCV目标检测Hog+SWindow源代码分析
参考文章:OpenCV中的HOG+SVM物体分类 此文主要描述出HOG分类的调用堆栈. 使用OpenCV作图像检测, 使用HOG检测过程,其中一部分源代码如下: 1.HOG 检测底层栈的检测计算代码: ...
- OpenCV:OpenCV目标检测Adaboost+haar源代码分析
使用OpenCV作图像检测, Adaboost+haar决策过程,其中一部分源代码如下: 函数调用堆栈的底层为: 1.使用有序决策桩进行预测 template<class FEval> i ...
- 运动检测(前景检测)之(一)ViBe
运动检测(前景检测)之(一)ViBe zouxy09@qq.com http://blog.csdn.net/zouxy09 因为监控发展的需求,目前前景检测的研究还是很多的,也出现了很多新的方法和思 ...
- ViBe(Visual Background extractor)背景建模或前景检测
ViBe算法:ViBe - a powerful technique for background detection and subtraction in video sequences 算法官网: ...
- paper 83:前景检测算法_1(codebook和平均背景法)
前景分割中一个非常重要的研究方向就是背景减图法,因为背景减图的方法简单,原理容易被想到,且在智能视频监控领域中,摄像机很多情况下是固定的,且背景也是基本不变或者是缓慢变换的,在这种场合背景减图法的应用 ...
- 运动检测(前景检测)之(二)混合高斯模型GMM
运动检测(前景检测)之(二)混合高斯模型GMM zouxy09@qq.com http://blog.csdn.net/zouxy09 因为监控发展的需求,目前前景检测的研究还是很多的,也出现了很多新 ...
- [转]运动检测(前景检测)之(二)混合高斯模型GMM
转自:http://blog.csdn.net/zouxy09/article/details/9622401 因为监控发展的需求,目前前景检测的研究还是很多的,也出现了很多新的方法和思路.个人了解的 ...
- 目标检测之vibe---ViBe(Visual Background extractor)背景建模或前景检测
ViBe算法:ViBe - a powerful technique for background detection and subtraction in video sequences 算法官网: ...
随机推荐
- uva 12096 - The SetStack Computer(集合栈)
例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...
- Python3 模块、包调用&路径
''' 以下代码均为讲解,不能实际操作 ''' ''' 博客园 Infi_chu ''' ''' 模块的优点: 1.高可维护性 2.可以大大减少编写的代码量 模块一共有三种: 1.Python标准库 ...
- webDriver + Firefox 浏览器 完美兼容
搞java最烦的就是不同版本的适配问题.现分享下实测成功的案例. Firefox:4.0.1 selenium:selenium-server-standalone-2.43.1.jar 下面这个链接 ...
- Java技术——I/O知识学习
个字节,主要用在处理二进制数据,字节用来与文件打交道,所有文件的储存都是通过字节(byte)的方式,在磁盘上保留的并不是文件的字符而是先把字符编码成字节,再储存这些字节到磁盘.在读取文件(特别是文本文 ...
- Android开发——Android系统启动以及APK安装、启动过程
0. 前言 从Android手机打开开关,到我们可以使用其中的app时,这个启动过程到底是怎么样的? 1. 系统上电 当给Android系统上电,在电源接通的瞬间,CPU内的寄存器和各引脚均会被 ...
- C++11中default的使用
In C++11, defaulted and deleted functions give you explicit control over whether the special member ...
- 4-oracle11g安装
1.导入安装包,解压 [root@ocp Desktop]# unzip p10404530_112030_Linux-x86-64_1of7.zip [root@ocp Desktop]# unzi ...
- How to find your web part
When we deploy a web part, we can find it on any pages through the follow steps: Firstly, ...
- 「日常训练」Greedy Arkady (CFR476D2C)
不用问为啥完全一致,那个CSDN的也是我的,我搬过来了而已. 题意(Codeforces 965C) $k$人分$n$个糖果,每个糖果至多属于1个人.A某人是第一个拿糖果的.(这点很重要!!) 他$x ...
- 用Python 的一些用法与 JS 进行类比,看有什么相似?
Python 是一门运用很广泛的语言,自动化脚本.爬虫,甚至在深度学习领域也都有 Python 的身影.作为一名前端开发者,也了解 ES6 中的很多特性借鉴自 Python (比如默认参数.解构赋值. ...