运动检测(前景检测)之(一)ViBe (转)

运动检测(前景检测)之(一)ViBe

zouxy09@qq.com

http://blog.csdn.net/zouxy09

 

      因为监控发展的需求,目前前景检测的研究还是很多的,也出现了很多新的方法和思路。个人了解的大概概括为以下一些:

      帧差、背景减除(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

  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

  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. 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++)  

  24.     {  

  25.         m_samples[i] = Mat::zeros(_image.size(), CV_8UC1);  

  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);  

  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)  

  77.            {  

  78.                dist = abs(m_samples[count].at<uchar>(i, j) - _image.at<uchar>(i, j));  

  79.                if (dist < RADIUS)  

  80.                    matches++;  

  81.                count++;  

  82.            }  

  83.  

  84.            if (matches >= MIN_MATCHES)  

  85.            {  

  86.                // It is a background pixel  

  87.                m_foregroundMatchCount.at<uchar>(i, j) = 0;  

  88.  

  89.                // Set background pixel to 0  

  90.                m_mask.at<uchar>(i, j) = 0;  

  91.  

  92.                // 如果一个像素是背景点,那么它有 1 / defaultSubsamplingFactor 的概率去更新自己的模型样本值  

  93.                int random = rng.uniform(0, SUBSAMPLE_FACTOR);  

  94.                if (random == 0)  

  95.                {  

  96.                    random = rng.uniform(0, NUM_SAMPLES);  

  97.                    m_samples[random].at<uchar>(i, j) = _image.at<uchar>(i, j);  

  98.                }  

  99.  

  100.                // 同时也有 1 / defaultSubsamplingFactor 的概率去更新它的邻居点的模型样本值  

  101.                random = rng.uniform(0, SUBSAMPLE_FACTOR);  

  102.                if (random == 0)  

  103.                {  

  104.                    int row, col;  

  105.                    random = rng.uniform(0, 9);  

  106.                    row = i + c_yoff[random];  

  107.                    if (row < 0)  

  108.                        row = 0;  

  109.                    if (row >= _image.rows)  

  110.                        row = _image.rows - 1;  

  111.  

  112.                    random = rng.uniform(0, 9);  

  113.                    col = j + c_xoff[random];  

  114.                    if (col < 0)  

  115.                        col = 0;  

  116.                    if (col >= _image.cols)  

  117.                        col = _image.cols - 1;  

  118.  

  119.                    random = rng.uniform(0, NUM_SAMPLES);  

  120.                    m_samples[random].at<uchar>(row, col) = _image.at<uchar>(i, j);  

  121.                }  

  122.            }  

  123.            else  

  124.            {  

  125.                // It is a foreground pixel  

  126.                m_foregroundMatchCount.at<uchar>(i, j)++;  

  127.  

  128.                // Set background pixel to 255  

  129.                m_mask.at<uchar>(i, j) = 255;  

  130.  

  131.                //如果某个像素点连续N次被检测为前景,则认为一块静止区域被误判为运动,将其更新为背景点  

  132.                if (m_foregroundMatchCount.at<uchar>(i, j) > 50)  

  133.                {  

  134.                    int random = rng.uniform(0, NUM_SAMPLES);  

  135.                    if (random == 0)  

  136.                    {  

  137.                        random = rng.uniform(0, NUM_SAMPLES);  

  138.                        m_samples[random].at<uchar>(i, j) = _image.at<uchar>(i, j);  

  139.                    }  

  140.                }  

  141.            }  

  142.        }  

  143.    }  

  144. }  

 

Main.cpp

  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  : zouxy09@qq.com  

  8.  

  9. #include "opencv2/opencv.hpp"  

  10. #include "ViBe.h"  

  11. #include <iostream>  

  12. #include <cstdio>  

  13.  

  14. using namespace cv;  

  15. using namespace std;  

  16.  

  17. int main(int argc, char* argv[])  

  18. {  

  19.    Mat frame, gray, mask;  

  20.    VideoCapture capture;  

  21.    capture.open("video.avi");  

  22.  

  23.    if (!capture.isOpened())  

  24.    {  

  25.        cout<<"No camera or video input!\n"<<endl;  

  26.        return -1;  

  27.    }  

  28.  

  29.    ViBe_BGS Vibe_Bgs;  

  30.    int count = 0;  

  31.  

  32.    while (1)  

  33.    {  

  34.        count++;  

  35.        capture >> frame;  

  36.        if (frame.empty())  

  37.            break;  

  38.        cvtColor(frame, gray, CV_RGB2GRAY);  

  39.      

  40.        if (count == 1)  

  41.        {  

  42.            Vibe_Bgs.init(gray);  

  43.            Vibe_Bgs.processFirstFrame(gray);  

  44.            cout<<" Training GMM complete!"<<endl;  

  45.        }  

  46.        else  

  47.        {  

  48.            Vibe_Bgs.testAndUpdate(gray);  

  49.            mask = Vibe_Bgs.getMask();  

  50.            morphologyEx(mask, mask, MORPH_OPEN, Mat());  

  51.            imshow("mask", mask);  

  52.        }  

  53.  

  54.        imshow("input", frame);  

  55.  

  56.        if ( cvWaitKey(10) == 'q' )  

  57.            break;  

  58.    }  

  59.  

  60.    return 0;  

  61. }  

 

posted @ 2014-12-04 14:44  视界君  阅读(329)  评论(0)    收藏  举报