天天看點

運動檢測(前景檢測)之ViBe

申明:本文非筆者原創,原文轉載自:http://blog.csdn.net/zouxy09/article/details/9622285

因為監控發展的需求,目前前景檢測的研究還是很多的,也出現了很多新的方法和思路。個人了解的大概概括為以下一些:

       幀差、背景減除(GMM、CodeBook、 SOBS、 SACON、 VIBE、 W4、多幀平均……)、光流(稀疏光流、稠密光流)、運動競争(Motion Competition)、運動模版(運動曆史圖像)、時間熵……等等。如果加上他們的改進版,那就是很大的一個家族了。

      對于上一些方法的一點簡單的對比分析可以參考下:

http://www.cnblogs.com/ronny/archive/2012/04/12/2444053.html

       至于哪個最好,看使用環境吧,各有千秋,有一些适用的情況更多,有一些在某些情況下表現更好。這些都需要針對自己的使用情況作測試确定的。呵呵。

       推薦一個牛逼的庫:http://code.google.com/p/bgslibrary/裡面包含了各種背景減除的方法,可以讓自己少做很多力氣活。

       還有王先榮部落格上存在不少的分析:

http://www.cnblogs.com/xrwang/archive/2010/02/21/ForegroundDetection.html

       下面的部落格上轉載王先榮的上面幾篇,然後加上自己分析了兩篇:

http://blog.csdn.net/stellar0

       本文主要關注其中的一種背景減除方法:ViBe。stellar0的部落格上對ViBe進行了分析,我這裡就不再啰嗦了,具體的理論可以參考:

http://www2.ulg.ac.be/telecom/research/vibe/

http://blog.csdn.net/stellar0/article/details/8777283

http://blog.csdn.net/yongshengsilingsa/article/details/6659859

http://www2.ulg.ac.be/telecom/research/vibe/download.html

http://www.cvchina.info/2011/12/25/vibe/

《ViBe: A universal background subtraction algorithm for video sequences》

《ViBe: a powerful technique for background detection and subtraction in video sequences》

       ViBe是一種像素級視訊背景模組化或前景檢測的算法,效果優于所熟知的幾種算法,對硬體記憶體占用也少,很簡單。我之前根據stellar0的代碼(在這裡,非常感謝stellar0)改寫成一個Mat格式的代碼了,現在擺上來和大家交流,具體如下:(在VS2010+OpenCV2.4.2中測試通過)

ViBe.h

[cpp]  view plain copy

  1. #pragma once  
  2. #include <iostream>  
  3. #include "opencv2/opencv.hpp"  
  4. using namespace cv;  
  5. using namespace std;  
  6. #define NUM_SAMPLES 20      //每個像素點的樣本個數  
  7. #define MIN_MATCHES 2       //#min指數  
  8. #define RADIUS 20       //Sqthere半徑  
  9. #define SUBSAMPLE_FACTOR 16 //子采樣機率  
  10. class ViBe_BGS  
  11. {  
  12. public:  
  13.     ViBe_BGS(void);  
  14.     ~ViBe_BGS(void);  
  15.     void init(const Mat _image);   //初始化  
  16.     void processFirstFrame(const Mat _image);  
  17.     void testAndUpdate(const Mat _image);  //更新  
  18.     Mat getMask(void){return m_mask;};  
  19. private:  
  20.     Mat m_samples[NUM_SAMPLES];  
  21.     Mat m_foregroundMatchCount;  
  22.     Mat m_mask;  
  23. };  

ViBe.cpp

[cpp]  view plain copy

  1. #include <opencv2/opencv.hpp>  
  2. #include <iostream>  
  3. #include "ViBe.h"  
  4. using namespace std;  
  5. using namespace cv;  
  6. int c_xoff[9] = {-1,  0,  1, -1, 1, -1, 0, 1, 0};  //x的鄰居點  
  7. int c_yoff[9] = {-1,  0,  1, -1, 1, -1, 0, 1, 0};  //y的鄰居點  
  8. ViBe_BGS::ViBe_BGS(void)  
  9. {  
  10. }  
  11. ViBe_BGS::~ViBe_BGS(void)  
  12. {  
  13. }  
  14. void ViBe_BGS::init(const Mat _image)  
  15. {  
  16.      for(int i = 0; i < NUM_SAMPLES; i++)  
  17.      {  
  18.          m_samples[i] = Mat::zeros(_image.size(), CV_8UC1);  
  19.      }  
  20.      m_mask = Mat::zeros(_image.size(),CV_8UC1);  
  21.      m_foregroundMatchCount = Mat::zeros(_image.size(),CV_8UC1);  
  22. }  
  23. void ViBe_BGS::processFirstFrame(const Mat _image)  
  24. {  
  25.     RNG rng;  
  26.     int row, col;  
  27.     for(int i = 0; i < _image.rows; i++)  
  28.     {  
  29.         for(int j = 0; j < _image.cols; j++)  
  30.         {  
  31.              for(int k = 0 ; k < NUM_SAMPLES; k++)  
  32.              {  
  33.                  // Random pick up NUM_SAMPLES pixel in neighbourhood to construct the model  
  34.                  int random = rng.uniform(0, 9);  
  35.                  row = i + c_yoff[random];  
  36.                  if (row < 0)   
  37.                      row = 0;  
  38.                  if (row >= _image.rows)  
  39.                      row = _image.rows - 1;  
  40.                  col = j + c_xoff[random];  
  41.                  if (col < 0)   
  42.                      col = 0;  
  43.                  if (col >= _image.cols)  
  44.                      col = _image.cols - 1;  
  45.                  m_samples[k].at<uchar>(i, j) = _image.at<uchar>(row, col);  
  46.              }  
  47.         }  
  48.     }  
  49. }  
  50. void ViBe_BGS::testAndUpdate(const Mat _image)  
  51. {  
  52.     RNG rng;  
  53.     for(int i = 0; i < _image.rows; i++)  
  54.     {  
  55.         for(int j = 0; j < _image.cols; j++)  
  56.         {  
  57.             int matches(0), count(0);  
  58.             float dist;  
  59.             while(matches < MIN_MATCHES && count < NUM_SAMPLES)  
  60.             {  
  61.                 dist = abs(m_samples[count].at<uchar>(i, j) - _image.at<uchar>(i, j));  
  62.                 if (dist < RADIUS)  
  63.                     matches++;  
  64.                 count++;  
  65.             }  
  66.             if (matches >= MIN_MATCHES)  
  67.             {  
  68.                 // It is a background pixel  
  69.                 m_foregroundMatchCount.at<uchar>(i, j) = 0;  
  70.                 // Set background pixel to 0  
  71.                 m_mask.at<uchar>(i, j) = 0;  
  72.                 // 如果一個像素是背景點,那麼它有 1 / defaultSubsamplingFactor 的機率去更新自己的模型樣本值  
  73.                 int random = rng.uniform(0, SUBSAMPLE_FACTOR);  
  74.                 if (random == 0)  
  75.                 {  
  76.                     random = rng.uniform(0, NUM_SAMPLES);  
  77.                     m_samples[random].at<uchar>(i, j) = _image.at<uchar>(i, j);  
  78.                 }  
  79.                 // 同時也有 1 / defaultSubsamplingFactor 的機率去更新它的鄰居點的模型樣本值  
  80.                 random = rng.uniform(0, SUBSAMPLE_FACTOR);  
  81.                 if (random == 0)  
  82.                 {  
  83.                     int row, col;  
  84.                     random = rng.uniform(0, 9);  
  85.                     row = i + c_yoff[random];  
  86.                     if (row < 0)   
  87.                         row = 0;  
  88.                     if (row >= _image.rows)  
  89.                         row = _image.rows - 1;  
  90.                     random = rng.uniform(0, 9);  
  91.                     col = j + c_xoff[random];  
  92.                     if (col < 0)   
  93.                         col = 0;  
  94.                     if (col >= _image.cols)  
  95.                         col = _image.cols - 1;  
  96.                     random = rng.uniform(0, NUM_SAMPLES);  
  97.                     m_samples[random].at<uchar>(row, col) = _image.at<uchar>(i, j);  
  98.                 }  
  99.             }  
  100.             else  
  101.             {  
  102.                 // It is a foreground pixel  
  103.                 m_foregroundMatchCount.at<uchar>(i, j)++;  
  104.                 // Set background pixel to 255  
  105.                 m_mask.at<uchar>(i, j) = 255;  
  106.                 //如果某個像素點連續N次被檢測為前景,則認為一塊靜止區域被誤判為運動,将其更新為背景點  
  107.                 if (m_foregroundMatchCount.at<uchar>(i, j) > 50)  
  108.                 {  
  109.                     int random = rng.uniform(0, NUM_SAMPLES);  
  110.                     if (random == 0)  
  111.                     {  
  112.                         random = rng.uniform(0, NUM_SAMPLES);  
  113.                         m_samples[random].at<uchar>(i, j) = _image.at<uchar>(i, j);  
  114.                     }  
  115.                 }  
  116.             }  
  117.         }  
  118.     }  
  119. }  

Main.cpp

[cpp]  view plain copy

  1. // This is based on   
  2. // "VIBE: A POWERFUL RANDOM TECHNIQUE TO ESTIMATE THE BACKGROUND IN VIDEO SEQUENCES"  
  3. // by Olivier Barnich and Marc Van Droogenbroeck  
  4. // Author : zouxy  
  5. // Date   : 2013-4-13  
  6. // HomePage : http://blog.csdn.net/zouxy09  
  7. // Email  : [email protected]  
  8. #include "opencv2/opencv.hpp"  
  9. #include "ViBe.h"  
  10. #include <iostream>  
  11. #include <cstdio>  
  12. using namespace cv;  
  13. using namespace std;  
  14. int main(int argc, char* argv[])  
  15. {  
  16.     Mat frame, gray, mask;  
  17.     VideoCapture capture;  
  18.     capture.open("video.avi");  
  19.     if (!capture.isOpened())  
  20.     {  
  21.         cout<<"No camera or video input!\n"<<endl;  
  22.         return -1;  
  23.     }  
  24.     ViBe_BGS Vibe_Bgs;  
  25.     int count = 0;  
  26.     while (1)  
  27.     {  
  28.         count++;  
  29.         capture >> frame;  
  30.         if (frame.empty())  
  31.             break;  
  32.         cvtColor(frame, gray, CV_RGB2GRAY);  
  33.         if (count == 1)  
  34.         {  
  35.             Vibe_Bgs.init(gray);  
  36.             Vibe_Bgs.processFirstFrame(gray);  
  37.             cout<<" Training GMM complete!"<<endl;  
  38.         }  
  39.         else  
  40.         {  
  41.             Vibe_Bgs.testAndUpdate(gray);  
  42.             mask = Vibe_Bgs.getMask();  
  43.             morphologyEx(mask, mask, MORPH_OPEN, Mat());  
  44.             imshow("mask", mask);  
  45.         }  
  46.         imshow("input", frame);   
  47.         if ( cvWaitKey(10) == 'q' )  
  48.             break;  
  49.     }  
  50.     return 0;  
  51. }