经历了一年多的徘徊,最近终于下定决心好好学习图像处理。
第一篇就是参考学习了 这位博主的文章,非常精彩。 地址 http://blog.csdn.net/zouxy09/article/details/9622285
Vibe这个算法真是不错,快而简洁。
参考论文
M. VAN DROOGENBROECK, and O. PAQUOT. Background Subtraction : Experiments and Improvements for ViBe. In Change Detection Workshop(CDW), Providence, Rhode Island; 6 pages, June 2012.
O. Barnich and M. Van Droogenbroeck, ViBe: A powerful random technique to estimate the background in video sequences. In Proc. Int. Conf. Acoust., Speech Signal Process; Apr. 2009, pp. 945–948.
O. Barnich and M. Van Droogenbroeck, ViBe: A universal background subtraction algorithm for video sequences. In IEEE Transactions on Image Processing; 20(6):1709-1724, June 2011.
下面是对博客中代码的理解注释
vibe.h
#ifndef VIBE_H
#define VIBE_H
#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 //子采样概率 有 1/UBSAMPLE_FACTOR 概率更新自己的样本值
#define background 0 //背景像素
#define foreground 255 //前景像素
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]; //每个像素有NUM_SAMPLES个采样点
Mat m_foregroundMatchCount; //某个像素点连续N次被检测为前景,则认为一块静止区域被,将其更新为背景点。
Mat m_mask; //输出的二值图像
};
#endif
vide.cpp
#include "vibe.h"
int c_xoff[9] = {-1, 0, 1, 1, 1, 0, -1, -1, 0}; //x的邻居点
int c_yoff[9] = {-1, -1, -1, 0, 1, 1, 1, 0, 0}; //y的邻居点
/* 9个邻域点
*
* A B C
* H I D
* G F E
*
*/
ViBe_BGS::ViBe_BGS(void)
{
}
ViBe_BGS::~ViBe_BGS(void)
{
}
/**************** Assign space and init ***************************/
/*
初始化Vibe算法各变量
*/
void ViBe_BGS::init(const Mat _image)
{
for(int i = 0; i < NUM_SAMPLES; i++)
{
m_samples[i] = Mat::zeros(_image.size(), CV_8UC1);//刚开始都给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;//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++)//取NUM_SAMPLES个采样点
{
// 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]; //这里表示产生的随机数会在8领域范围内选择点作为采样点
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指数,最小交集
{
dist = abs(m_samples[count].at<uchar>(i, j) - _image.at<uchar>(i, j));//这先计算里欧氏距离
if (dist < RADIUS) //如果在我们设定的采样半径之内,匹配计数+1
matches++;
count++;
}
if (matches >= MIN_MATCHES)//#min 最小交集符合要求
{
// It is a background pixel
m_foregroundMatchCount.at<uchar>(i, j) = 0;
//说明该点与周围点融合得比较好,可以作为背景处理
//某个像素点连续N次被检测为前景,则认为一块静止区域被误判为运动,将其更新为背景点。这里需及时清零.
// Set background pixel to 0
m_mask.at<uchar>(i, j) = 0; //作为图像背景点
// 如果一个像素是背景点,那么它有 1 / defaultSubsamplingFactor 的概率去更新自己的模型样本值
int random = rng.uniform(0, SUBSAMPLE_FACTOR);//
if (random == 0) // 1/SUBSAMPLE_FACTOR的概率去更新自己的样本。
{
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) // 1/SUBSAMPLE_FACTOR的概率去更新8领域范围内的样本。
{
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 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, NUM_SAMPLES);
if (random == 0)
{
random = rng.uniform(0, NUM_SAMPLES);
m_samples[random].at<uchar>(i, j) = _image.at<uchar>(i, j);
}
}
}
}
}
}
#include "widget.h"
#include <QApplication>
#include "opencv2/opencv.hpp"
#include "vibe.h"
int main(int argc, char *argv[])
{
Mat frame, gray, mask;
VideoCapture capture;
capture.open("campus.avi");//campus sequence
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(20) == 'q' )
break;
}
return 0;
}
好了。这个就到这里。下面我就要尝试对其进行程序上的小修改,让它跑得更快些。
每天进步一点。