一方面為了學習,一方面按照老師和項目的要求接觸到了前景提取的相關知識,具體的方法有很多,幀差、背景減除(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}; //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);
- }
- }
- }
- }
- }
- }
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;
- }