運動目標前景檢測之ViBe源代碼分析


一方面為了學習,一方面按照老師和項目的要求接觸到了前景提取的相關知識,具體的方法有很多,幀差、背景減除(GMM、CodeBook、 SOBS、 SACON、 VIBE、 W4、多幀平均……)、光流(稀疏光流、稠密光流)、運動競爭(Motion Competition)、運動模版(運動歷史圖像)、時間熵……等等。
更為具體的資料可以參考一下鏈接,作者做了很好的總結。點擊打開鏈接http://blog.csdn.net/zouxy09/article/details/9622285

我只要針對作者提供的源代碼,加上我的理解最代碼捉了做了相關的注釋,便於自己對代碼的閱讀和與大家的交流,如果不妥之處,稀罕大家多多提出,共同進步微笑

ViBe.h(頭文件,一般做申明函數、類使用,不做具體定義)

 

[cpp]  view plaincopy
  1. #pragma once    
  2. #include <iostream>    
  3. #include "opencv2/opencv.hpp"   
  4.     
  5. using namespace cv;    
  6. using namespace std;    
  7.     
  8. #define NUM_SAMPLES 20      //每個像素點的樣本個數    
  9. #define MIN_MATCHES 2       //#min指數    
  10. #define RADIUS 20       //Sqthere半徑    
  11. #define SUBSAMPLE_FACTOR 16 //子采樣概率,決定背景更新的概率  
  12.     
  13.     
  14. class ViBe_BGS    
  15. {    
  16. public:    
  17.     ViBe_BGS(void);  //構造函數  
  18.     ~ViBe_BGS(void);  //析構函數,對開辟的內存做必要的清理工作  
  19.     
  20.     void init(const Mat _image);   //初始化    
  21.     void processFirstFrame(const Mat _image); //利用第一幀進行建模   
  22.     void testAndUpdate(const Mat _image);  //判斷前景與背景,並進行背景跟新   
  23.     Mat getMask(void){return m_mask;};  //得到前景  
  24.     
  25. private:    
  26.     Mat m_samples[NUM_SAMPLES];  //每一幀圖像的每一個像素的樣本集  
  27.     Mat m_foregroundMatchCount;  //統計像素被判斷為前景的次數,便於跟新  
  28.     Mat m_mask;  //前景提取后的一幀圖像  
  29. };    

ViBe.cpp(上面所提到的申明的具體定義)

 

 

[cpp]  view plaincopy
  1. #include <opencv2/opencv.hpp>    
  2. #include <iostream>    
  3. #include "ViBe.h"    
  4.     
  5. using namespace std;    
  6. using namespace cv;    
  7.     
  8. int c_xoff[9] = {-1,  0,  1, -1, 1, -1, 0, 1, 0};  //x的鄰居點,9宮格  
  9. int c_yoff[9] = {-1,  0,  1, -1, 1, -1, 0, 1, 0};  //y的鄰居點    
  10.     
  11. ViBe_BGS::ViBe_BGS(void)    
  12. {    
  13.     
  14. }    
  15. ViBe_BGS::~ViBe_BGS(void)    
  16. {    
  17.     
  18. }    
  19.     
  20. /**************** Assign space and init ***************************/    
  21. void ViBe_BGS::init(const Mat _image)  //成員函數初始化  
  22. {    
  23.      for(int i = 0; i < NUM_SAMPLES; i++) //可以這樣理解,針對一幀圖像,建立了20幀的樣本集  
  24.      {    
  25.          m_samples[i] = Mat::zeros(_image.size(), CV_8UC1);  //針對每一幀樣本集的每一個像素初始化為8位無符號0,單通道  
  26.      }    
  27.      m_mask = Mat::zeros(_image.size(),CV_8UC1); //初始化   
  28.      m_foregroundMatchCount = Mat::zeros(_image.size(),CV_8UC1);  //每一個像素被判斷為前景的次數,初始化  
  29. }    
  30.     
  31. /**************** Init model from first frame ********************/    
  32. void ViBe_BGS::processFirstFrame(const Mat _image)    
  33. {    
  34.     RNG rng;    //隨機數產生器                                      
  35.     int row, col;    
  36.     
  37.     for(int i = 0; i < _image.rows; i++)    
  38.     {    
  39.         for(int j = 0; j < _image.cols; j++)    
  40.         {    
  41.              for(int k = 0 ; k < NUM_SAMPLES; k++)    
  42.              {    
  43.                  // Random pick up NUM_SAMPLES pixel in neighbourhood to construct the model    
  44.                  int random = rng.uniform(0, 9);  //隨機產生0-9的隨機數,主要用於定位中心像素的鄰域像素  
  45.     
  46.                  row = i + c_yoff[random]; //定位中心像素的鄰域像素   
  47.                  if (row < 0)   //下面四句主要用於判斷是否超出邊界  
  48.                      row = 0;    
  49.                  if (row >= _image.rows)    
  50.                      row = _image.rows - 1;    
  51.     
  52.                  col = j + c_xoff[random];    
  53.                  if (col < 0)    //下面四句主要用於判斷是否超出邊界  
  54.                      col = 0;    
  55.                  if (col >= _image.cols)    
  56.                      col = _image.cols - 1;    
  57.     
  58.                  m_samples[k].at<uchar>(i, j) = _image.at<uchar>(row, col);  //將相應的像素值復制到樣本集中  
  59.              }    
  60.         }    
  61.     }    
  62. }    
  63.     
  64. /**************** Test a new frame and update model ********************/    
  65. void ViBe_BGS::testAndUpdate(const Mat _image)    
  66. {    
  67.     RNG rng;    
  68.     
  69.     for(int i = 0; i < _image.rows; i++)    
  70.     {    
  71.         for(int j = 0; j < _image.cols; j++)    
  72.         {    
  73.             int matches(0), count(0);    
  74.             float dist;    
  75.     
  76.             while(matches < MIN_MATCHES && count < NUM_SAMPLES) //逐個像素判斷,當匹配個數大於閥值MIN_MATCHES,或整個樣本集遍歷完成跳出  
  77.             {    
  78.                 dist = abs(m_samples[count].at<uchar>(i, j) - _image.at<uchar>(i, j)); //當前幀像素值與樣本集中的值做差,取絕對值   
  79.                 if (dist < RADIUS)  //當絕對值小於閥值是,表示當前幀像素與樣本值中的相似  
  80.                     matches++;   
  81.   
  82.                 count++;  //取樣本值的下一個元素作比較  
  83.             }    
  84.     
  85.             if (matches >= MIN_MATCHES)  //匹配個數大於閥值MIN_MATCHES個數時,表示作為背景  
  86.             {    
  87.                 // It is a background pixel    
  88.                 m_foregroundMatchCount.at<uchar>(i, j) = 0;  //被檢測為前景的個數賦值為0  
  89.     
  90.                 // Set background pixel to 0    
  91.                 m_mask.at<uchar>(i, j) = 0;  //該像素點值也為0  
  92.     
  93.                 // 如果一個像素是背景點,那么它有 1 / defaultSubsamplingFactor 的概率去更新自己的模型樣本值    
  94.                 int random = rng.uniform(0, SUBSAMPLE_FACTOR);   //以1 / defaultSubsamplingFactor概率跟新背景  
  95.                 if (random == 0)    
  96.                 {    
  97.                     random = rng.uniform(0, NUM_SAMPLES);    
  98.                     m_samples[random].at<uchar>(i, j) = _image.at<uchar>(i, j);    
  99.                 }    
  100.     
  101.                 // 同時也有 1 / defaultSubsamplingFactor 的概率去更新它的鄰居點的模型樣本值    
  102.                 random = rng.uniform(0, SUBSAMPLE_FACTOR);    
  103.                 if (random == 0)    
  104.                 {    
  105.                     int row, col;    
  106.                     random = rng.uniform(0, 9);    
  107.                     row = i + c_yoff[random];    
  108.                     if (row < 0)   //下面四句主要用於判斷是否超出邊界  
  109.                         row = 0;    
  110.                     if (row >= _image.rows)    
  111.                         row = _image.rows - 1;    
  112.     
  113.                     random = rng.uniform(0, 9);    
  114.                     col = j + c_xoff[random];    
  115.                     if (col < 0)   //下面四句主要用於判斷是否超出邊界  
  116.                         col = 0;    
  117.                     if (col >= _image.cols)    
  118.                         col = _image.cols - 1;    
  119.     
  120.                     random = rng.uniform(0, NUM_SAMPLES);    
  121.                     m_samples[random].at<uchar>(row, col) = _image.at<uchar>(i, j);    
  122.                 }    
  123.             }   
  124.   
  125.             else  //匹配個數小於閥值MIN_MATCHES個數時,表示作為前景  
  126.             {    
  127.                 // It is a foreground pixel    
  128.                 m_foregroundMatchCount.at<uchar>(i, j)++;    
  129.     
  130.                 // Set background pixel to 255    
  131.                 m_mask.at<uchar>(i, j) =255;    
  132.     
  133.                 //如果某個像素點連續N次被檢測為前景,則認為一塊靜止區域被誤判為運動,將其更新為背景點    
  134.                 if (m_foregroundMatchCount.at<uchar>(i, j) > 50)    
  135.                 {    
  136.                     int random = rng.uniform(0, SUBSAMPLE_FACTOR);    
  137.                     if (random == 0)    
  138.                     {    
  139.                         random = rng.uniform(0, NUM_SAMPLES);    
  140.                         m_samples[random].at<uchar>(i, j) = _image.at<uchar>(i, j);    
  141.                     }    
  142.                 }    
  143.             }    
  144.         }    
  145.     }    
  146. }    

main.cpp(你懂的……大笑

 

 

[cpp]  view plaincopy
  1. #include <opencv2/opencv.hpp>  
  2. #include "ViBe.h"    
  3. #include <iostream>    
  4. #include <cstdio>    
  5. #include<stdlib.h>  
  6. using namespace cv;    
  7. using namespace std;    
  8.     
  9. int main(int argc, char* argv[])    
  10. {    
  11.     Mat frame, gray, mask;    
  12.     VideoCapture capture;    
  13.     capture.open("E:\\overpass\\11.avi");    
  14.     
  15.     if (!capture.isOpened())    
  16.     {    
  17.         cout<<"No camera or video input!\n"<<endl;    
  18.         return -1;    
  19.     }    
  20.     
  21.     ViBe_BGS Vibe_Bgs; //定義一個背景差分對象  
  22.     int count = 0; //幀計數器,統計為第幾幀   
  23.     
  24.     while (1)    
  25.     {    
  26.         count++;    
  27.         capture >> frame;    
  28.         if (frame.empty())    
  29.             break;    
  30.         cvtColor(frame, gray, CV_RGB2GRAY); //轉化為灰度圖像   
  31.         
  32.         if (count == 1)  //若為第一幀  
  33.         {    
  34.             Vibe_Bgs.init(gray);    
  35.             Vibe_Bgs.processFirstFrame(gray); //背景模型初始化   
  36.             cout<<" Training GMM complete!"<<endl;    
  37.         }    
  38.         else    
  39.         {    
  40.             Vibe_Bgs.testAndUpdate(gray);    
  41.             mask = Vibe_Bgs.getMask();    
  42.             morphologyEx(mask, mask, MORPH_OPEN, Mat());    
  43.             imshow("mask", mask);    
  44.         }    
  45.     
  46.         imshow("input", frame);     
  47.     
  48.         if ( cvWaitKey(10) == 'q' )    
  49.             break;    
  50.     }    
  51.   system("pause");  
  52.     return 0;    
  53. }    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM