一方面为了学习,一方面按照老师和项目的要求接触到了前景提取的相关知识,具体的方法有很多,帧差、背景减除(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};
- int c_yoff[9] = {-1, 0, 1, -1, 1, -1, 0, 1, 0};
-
- ViBe_BGS::ViBe_BGS(void)
- {
-
- }
- ViBe_BGS::~ViBe_BGS(void)
- {
-
- }
-
- void ViBe_BGS::init(const Mat _image)
- {
- for(int i = 0; i < NUM_SAMPLES; i++)
- {
- m_samples[i] = Mat::zeros(_image.size(), CV_8UC1);
- }
- m_mask = Mat::zeros(_image.size(),CV_8UC1);
- m_foregroundMatchCount = Mat::zeros(_image.size(),CV_8UC1);
- }
-
- 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++)
- {
-
- 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;
-
- m_samples[k].at<uchar>(i, j) = _image.at<uchar>(row, col);
- }
- }
- }
- }
-
- 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)
- {
- dist = abs(m_samples[count].at<uchar>(i, j) - _image.at<uchar>(i, j));
- if (dist < RADIUS)
- matches++;
-
- count++;
- }
-
- if (matches >= MIN_MATCHES)
- {
-
- m_foregroundMatchCount.at<uchar>(i, j) = 0;
-
-
- m_mask.at<uchar>(i, j) = 0;
-
-
- 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);
- }
-
-
- 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
- {
-
- m_foregroundMatchCount.at<uchar>(i, j)++;
-
-
- m_mask.at<uchar>(i, j) =255;
-
-
- 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源代码分析
原文:http://www.cnblogs.com/ywsoftware/p/4434065.html