vs2019使用opencv实现ViBe算法
参考代码网址:https://github.com/upcAutoLang/BackgroundSplit-OpenCV/tree/master/src/ViBe
链接
ViBe参考文章:https://www.jianshu.com/p/48baa72c6e5f
链接
(1)Vibe.h
#include<iostream>
#include<cstdio>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
//每个像素点的样本个数默认值
#define DEFAULT_NUM_SAMPLES 20
//#min指数默认值
#define DEFAULT_MIN_MATCHES 2
//Sqthere半径默认值
#define DEFAULT_RADIUS 20
//子采样概率默认值
#define DEFAULT_RANDOM_SAMPLE 16
class ViBe
{
public:
ViBe(int num_sam = DEFAULT_NUM_SAMPLES,
int min_match = DEFAULT_MIN_MATCHES,
int r = DEFAULT_RADIUS,
int rand_sam = DEFAULT_RANDOM_SAMPLE);
~ViBe(void);
//背景模型初始化
void init(Mat img);
//处理第一帧图像
void ProcessFirstFrame(Mat img);
//运行ViBe算法,提取前景区域并更新背景模型样本库
void Run(Mat img);
//获取前景模型二值图像
Mat getFGModel();
//删除样本库
void deleteSamples();
//x的邻居点
int c_xoff[9];
//y的邻居点
int c_yoff[9];
private:
//样本库
unsigned char*** samples;
//前景模型二值图像
Mat FGModel;
//每个像素点的样本个数
int num_samples;
//#min指数
int num_min_matches;
//Sqthere半径
int radius;
//子采样概率
int random_sample;
};
(2)Vibe.cpp
#include"Vibe.h"
/*
构造函数ViBe
参数:
int num_sam:每个像素点的样本个数
int min_match:#min 指数
int r: Sqthere 半径
int rand_sam:子采样概率
*/
ViBe::ViBe(int num_sam, int min_match, int r, int rand_sam)
{
num_samples = num_sam;
num_min_matches = min_match;
radius = r;
random_sample = rand_sam;
int c_off[9] = {
-1,0,1,-1,1,-1,0,1,0 };
for (int i = 0; i < 9; i++) {
c_xoff[i] = c_yoff[i] = c_off[i];
}
}
/*析构函数:~ViBe
说明:释放样本库内存
*/
ViBe::~ViBe(void)
{
deleteSamples();