ViBe,论文解读,代码实现(c++)
论文解读
O. Barnich, and M. V. Droogenbroeck, “ViBe: A universal background subtraction algorithm for video sequences,” IEEE Trans. Image Process., vol. 20, no. 6, pp. 1709–1724, Jun. 2011.
introducion
这是一个背景减除前景检测算法
创新点:
- 对于每一个像素,它的模型集合由过去时刻,该像素及其领域像素的像素值组成。
- 当前时刻像素的像素值与该像素的集合模型比较以判断除它是否属于背景像素。
- 更新模型的机制为随机更新机制。
Description of ViBe
困了。。。 明天继续
- Pixel Model and Classification Process
- Background Model Initialization From a Single Frame
- Updating the Background Model Over Time
some results
c++实现
main函数:
#include <opencv2/opencv.hpp>
#include "ViBe.h"
#include <iostream>
#include <cstdio>
#include<stdlib.h>
#include "GetFileName.h"
using namespace cv;
using namespace std;
int main()
{
// 路径下的所有文件名
string filePath = "F:\\dataset";
vector<string> files;
string format = ".bmp";
GetAllFormatFiles(filePath, files, format);
int size = files.size();
cout << size << endl;
// 结果保存的路径
string savepath1 = "F:\\pets2001";
// 要保存第几帧到第几帧的mask
int saveframeFirst = 1;
int saveframeLast = 499;
Mat frame, gray, mask,savemask;
ViBe_BGS Vibe_Bgs; //定义一个背景差分对象
int count = 0; //帧计数器,统计为第几帧
while (count < size)
{
count++;
string readimgname = filePath + "\\" + files[count - 1];
frame = imread(readimgname);
cout << readimgname << endl;
cvtColor(frame, gray, CV_RGB2GRAY); //转化为灰度图像
if (count == 1) //若为第一帧
{
Vibe_Bgs.init(gray);
Vibe_Bgs.processFirstFrame(gray); //背景模型初始化
cout << " init VIBE complete!" << endl;
}
else
{
Vibe_Bgs.testAndUpdate(gray);
mask = Vibe_Bgs.getMask();
morphologyEx(mask, mask, MORPH_OPEN, Mat());
imshow("mask", mask);
}
// 存一下要保存的mask
if (count <= saveframeLast && count >= saveframeFirst)
{
cvtColor(mask, savemask, CV_GRAY2RGB);
string savename = savepath1 + "\\" + files[count - 1];
imwrite(savename, savemask);
}
imshow("input", frame);
if (cvWaitKey(10) == 'q')
break;
}
system("pause");
return 0;
}
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 //子采样概率,决定背景更新的概率 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);
}
}
}
}
}
}