Opencv3.1基于Vibe去除前景
2016-11-23 17:02
190 查看
Vibe.h 和Vibe.cpp还有main函数
Vibe.h:
#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; };
void deleteSamples() { delete samples; };
private:
unsigned char ***samples;
// float samples[1024][1024][NUM_SAMPLES+1];//保存每个像素点的样本值
/*
Mat m_samples[NUM_SAMPLES];
Mat m_foregroundMatchCount;*/
Mat m_mask;
};
vibe.cpp:
#include "ViBe.h"
using namespace std;
using namespace cv;
int c_xoff[9] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 }; //x的邻居点
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)
{
//动态分配三维数组,samples[][][NUM_SAMPLES]存储前景被连续检测的次数
samples = new unsigned char **[_image.rows];
for (int i = 0; i<_image.rows; i++)
{
samples[i] = new unsigned char *[1024];
for (int j = 0; j<_image.cols; j++)
{
samples[i][j] = new unsigned char[NUM_SAMPLES + 1];
for (int k = 0; k<NUM_SAMPLES + 1; k++)
{
samples[i][j][k] = 0;
}
}
}
m_mask = 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);
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;
samples[i][j][k] = _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);
int dist;
while (matches < MIN_MATCHES && count < NUM_SAMPLES)
{
dist = abs(samples[i][j][count] - _image.at<uchar>(i, j));
if (dist < RADIUS)
matches++;
count++;
}
if (matches >= MIN_MATCHES)
{
// It is a background pixel
samples[i][j][NUM_SAMPLES] = 0;
// Set background pixel to 0
m_mask.at<uchar>(i, j) = 0;
// 如果一个像素是背景点,那么它有 1 / defaultSubsamplingFactor 的概率去更新自己的模型样本值
int random = rng.uniform(0, SUBSAMPLE_FACTOR);
if (random == 0)
{
random = rng.uniform(0, NUM_SAMPLES);
samples[i][j][random] = _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);
samples[i][j][random] = _image.at<uchar>(i, j);
}
}
else
{
// It is a foreground pixel
samples[i][j][NUM_SAMPLES]++;
// Set background pixel to 255
m_mask.at<uchar>(i, j) = 255;
//如果某个像素点连续N次被检测为前景,则认为一块静止区域被误判为运动,将其更新为背景点
if (samples[i][j][NUM_SAMPLES]>50)
{
int random = rng.uniform(0, NUM_SAMPLES);
if (random == 0)
{
random = rng.uniform(0, NUM_SAMPLES);
samples[i][j][random] = _image.at<uchar>(i, j);
}
}
}
}
}
}
下面是main函数:
#include "ViBe.h"
#include <cstdio>
using namespace cv;
using namespace std;
int time = 0;
int main(int argc, char* argv[])
{
Mat frame, gray, mask;
VideoCapture capture;
capture.open(0);
capture.set(CV_CAP_PROP_FRAME_WIDTH, 680);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
if (!capture.isOpened())
{
cout << "No camera or video input!\n" << endl;
return -1;
}
ViBe_BGS Vibe_Bgs;
bool count = true;
while (1)
{
capture >> frame;
time++;
if (frame.empty())
continue;
cvtColor(frame, gray, CV_RGB2GRAY);
if (count)
{
Vibe_Bgs.init(gray);
Vibe_Bgs.processFirstFrame(gray);
cout << " Training ViBe complete!" << endl;
count = false;
}
else
{
if(time=15){
Vibe_Bgs.testAndUpdate(gray);
mask = Vibe_Bgs.getMask();
morphologyEx(mask, mask, MORPH_OPEN, Mat());
imshow("mask", mask);
cout << time << endl;
time = 0;
}
}
imshow("input", frame);
if (cvWaitKey(10) == 27)
break;
}
return 0;
}
Vibe.h:
#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; };
void deleteSamples() { delete samples; };
private:
unsigned char ***samples;
// float samples[1024][1024][NUM_SAMPLES+1];//保存每个像素点的样本值
/*
Mat m_samples[NUM_SAMPLES];
Mat m_foregroundMatchCount;*/
Mat m_mask;
};
vibe.cpp:
#include "ViBe.h"
using namespace std;
using namespace cv;
int c_xoff[9] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 }; //x的邻居点
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)
{
//动态分配三维数组,samples[][][NUM_SAMPLES]存储前景被连续检测的次数
samples = new unsigned char **[_image.rows];
for (int i = 0; i<_image.rows; i++)
{
samples[i] = new unsigned char *[1024];
for (int j = 0; j<_image.cols; j++)
{
samples[i][j] = new unsigned char[NUM_SAMPLES + 1];
for (int k = 0; k<NUM_SAMPLES + 1; k++)
{
samples[i][j][k] = 0;
}
}
}
m_mask = 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);
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;
samples[i][j][k] = _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);
int dist;
while (matches < MIN_MATCHES && count < NUM_SAMPLES)
{
dist = abs(samples[i][j][count] - _image.at<uchar>(i, j));
if (dist < RADIUS)
matches++;
count++;
}
if (matches >= MIN_MATCHES)
{
// It is a background pixel
samples[i][j][NUM_SAMPLES] = 0;
// Set background pixel to 0
m_mask.at<uchar>(i, j) = 0;
// 如果一个像素是背景点,那么它有 1 / defaultSubsamplingFactor 的概率去更新自己的模型样本值
int random = rng.uniform(0, SUBSAMPLE_FACTOR);
if (random == 0)
{
random = rng.uniform(0, NUM_SAMPLES);
samples[i][j][random] = _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);
samples[i][j][random] = _image.at<uchar>(i, j);
}
}
else
{
// It is a foreground pixel
samples[i][j][NUM_SAMPLES]++;
// Set background pixel to 255
m_mask.at<uchar>(i, j) = 255;
//如果某个像素点连续N次被检测为前景,则认为一块静止区域被误判为运动,将其更新为背景点
if (samples[i][j][NUM_SAMPLES]>50)
{
int random = rng.uniform(0, NUM_SAMPLES);
if (random == 0)
{
random = rng.uniform(0, NUM_SAMPLES);
samples[i][j][random] = _image.at<uchar>(i, j);
}
}
}
}
}
}
下面是main函数:
#include "ViBe.h"
#include <cstdio>
using namespace cv;
using namespace std;
int time = 0;
int main(int argc, char* argv[])
{
Mat frame, gray, mask;
VideoCapture capture;
capture.open(0);
capture.set(CV_CAP_PROP_FRAME_WIDTH, 680);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
if (!capture.isOpened())
{
cout << "No camera or video input!\n" << endl;
return -1;
}
ViBe_BGS Vibe_Bgs;
bool count = true;
while (1)
{
capture >> frame;
time++;
if (frame.empty())
continue;
cvtColor(frame, gray, CV_RGB2GRAY);
if (count)
{
Vibe_Bgs.init(gray);
Vibe_Bgs.processFirstFrame(gray);
cout << " Training ViBe complete!" << endl;
count = false;
}
else
{
if(time=15){
Vibe_Bgs.testAndUpdate(gray);
mask = Vibe_Bgs.getMask();
morphologyEx(mask, mask, MORPH_OPEN, Mat());
imshow("mask", mask);
cout << time << endl;
time = 0;
}
}
imshow("input", frame);
if (cvWaitKey(10) == 27)
break;
}
return 0;
}
相关文章推荐
- Opencv3.1基于混合高斯去除前景(适用于动态视频去除前景)
- 基于opencv3.1的cnn分类系统集成
- 一个去除视频前景,保留背景的 opencv 小程序 (附注释)
- 基于VS2013 MFC的OPENCV3.1环境构建及测试
- 学习OpenCV范例(二十四)—ViBe前景检测(二)
- Elam的caffe笔记之配置篇:基于CentOS6.5 python3.6 CUDA8.0 cudnnv5.1 opencv3.1 下的caffe配置
- 基于ubuntu16.04配置opencv3.1+python3.5
- (基于OPENCV3.1)Mat 对象的初始化
- 基于OpenCV 2.4.9/3.1的汉字显示(FreeType 2.6.2)
- 基于Hi3559A ARM64位嵌入式平台的OpenCV 2.4/3.1 移植
- 两种Python基于OpenCV的固定位置半透明水印去除方案
- 基于Opencv的目标检测与跟踪阴影去除算法实现
- 人脸识别opencv3.1 for python(基于LBH)
- opencv教程(基于python)----grabcut前景提取
- 微信跳一跳辅助程序开发,基于C++与opencv图像识别
- opencv2两张图像拼接融合_基于SURF特征提取
- 在Ubuntu16.04安装OpenCV3.1并实现USB摄像头图像采集
- opencv3.1+cuda8.0+caffe报错
- 【Python_OpenCv】笔记5:基于python和OPenCv的ROI区域的设置
- Rob Hess的SIFT算法的C语言实现(基于OpenCV)