学习OpenCV范例(二十四)—ViBe前景检测(二)

来源:互联网 发布:淘宝网天猫女麻套装 编辑:程序博客网 时间:2024/04/29 05:37

原文  http://blog.csdn.net/chenjiazhou12/article/details/30098145

最近导师没给什么项目做,所以有那么一点点小时间,于是就研究起了前景检测,既然前景检测有很多种算法,那干脆就把这些模型都学起来吧,以后用到前景检测时至少还有那么几种方法可以选择,上次介绍的是GMM模型,其实GMM模型本身就是一个很不错的模型,现在也很多人在研究,并且做改进,主要是OpenCV有函数调用,用起来非常方便,当我们都在兴高采烈的讨论GMM各种好的时候,B哥不爽了,他说老子是搞前景检测的,怎么可能让你们这么嚣张,而且老子就不按照你那套路来,什么高斯模型,混合高斯模型,我统统不用,就来个简单的,气死你们,于是一挥笔,一篇著作就出来了ViBe: A Universal Background Subtraction Algorithm for Video Sequences,顿时震惊了所有人,现在就让我们一起来领略一下ViBe算法到底有什么过人之处吧。

1、原理

一讲到原理,本人就喜欢推荐其他博主的博客,因为我觉得他们都已经写得非常好了,好资源就是要被传播,被共享的,所以对于原理的问题,我一样推荐一个博客给大家。

①、背景建模或前景检测(Background Generation And Foreground Detection) 五 ViBe

这篇文章可以说是ViBe原文的精华版,对这个算法的理论基础和精髓理解的非常透彻,甚至连实验对比结果和效果图都给大家贴出来了,都是原文的图片。

也希望大家看完这篇精华版之后,有初步了解之后可以去看一下原文,原文第一部分概述了各种各样的前景检测算法,第二部分开始讲ViBe,其中作者说了一句,背景减法技术一般都要解决三个问题:1、用到什么模型,并且它的工作原理,2、如何初始化这个模型,3、随着时间的推移,如何更新这个模型,于是作者也就从这三个方面介绍了ViBe算法,最后一部分就是各种实验结果和对比。

接下来给大家推荐一个代码版本,现在的OpenCV也有ViBe库函数提供,但是是在CUDA平台下的。

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

这位博主主要的代码都是用到OpenCV的Mat格式存储数据和调用数据,所以速度有点慢,于是本人做了小小的修改,快了些许,代码会在下面贴出。

同样也贴出另外一位博主的代码,没仔细看,希望有空可以研究一下

③、VIBE运动目标检测算法实现

2、代码实现

Vibe.h

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include "opencv2/opencv.hpp"  
  3.   
  4. using namespace cv;  
  5. using namespace std;  
  6.   
  7. #define NUM_SAMPLES 20      //每个像素点的样本个数  
  8. #define MIN_MATCHES 2       //#min指数  
  9. #define RADIUS 20       //Sqthere半径  
  10. #define SUBSAMPLE_FACTOR 16 //子采样概率  
  11.   
  12.   
  13. class ViBe_BGS  
  14. {  
  15. public:  
  16.     ViBe_BGS(void);  
  17.     ~ViBe_BGS(void);  
  18.   
  19.     void init(const Mat _image);   //初始化  
  20.     void processFirstFrame(const Mat _image);  
  21.     void testAndUpdate(const Mat _image);  //更新  
  22.     Mat getMask(void){return m_mask;};  
  23.     void deleteSamples(){delete samples;};  
  24.   
  25. private:  
  26.     unsigned char ***samples;  
  27. //  float samples[1024][1024][NUM_SAMPLES+1];//保存每个像素点的样本值  
  28.   
  29. /* 
  30.     Mat m_samples[NUM_SAMPLES]; 
  31.     Mat m_foregroundMatchCount;*/  
  32.   
  33.     Mat m_mask;  
  34. };  

Vibe.cpp

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "ViBe.h"  
  2.   
  3. using namespace std;  
  4. using namespace cv;  
  5.   
  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.   
  9. ViBe_BGS::ViBe_BGS(void)  
  10. {  
  11.   
  12. }  
  13. ViBe_BGS::~ViBe_BGS(void)  
  14. {  
  15.   
  16. }  
  17.   
  18. /**************** Assign space and init ***************************/  
  19. void ViBe_BGS::init(const Mat _image)  
  20. {  
  21.     //动态分配三维数组,samples[][][NUM_SAMPLES]存储前景被连续检测的次数  
  22.     samples=new unsigned char **[_image.rows];  
  23.     for (int i=0;i<_image.rows;i++)  
  24.     {  
  25.         samples[i]=new unsigned char *[1024];  
  26.         for (int j=0;j<_image.cols;j++)  
  27.         {  
  28.             samples[i][j]=new unsigned char [NUM_SAMPLES+1];      
  29.             for (int k=0;k<NUM_SAMPLES+1;k++)  
  30.             {  
  31.                 samples[i][j][k]=0;  
  32.             }  
  33.               
  34.         }  
  35.           
  36.     }  
  37.     m_mask = Mat::zeros(_image.size(),CV_8UC1);  
  38. }  
  39.   
  40. /**************** Init model from first frame ********************/  
  41. void ViBe_BGS::processFirstFrame(const Mat _image)  
  42. {  
  43.     RNG rng;  
  44.     int row, col;  
  45.   
  46.     for(int i = 0; i < _image.rows; i++)  
  47.     {  
  48.         for(int j = 0; j < _image.cols; j++)  
  49.         {  
  50.             for(int k = 0 ; k < NUM_SAMPLES; k++)  
  51.             {  
  52.                 // Random pick up NUM_SAMPLES pixel in neighbourhood to construct the model  
  53.                 int random = rng.uniform(0, 9);  
  54.   
  55.                 row = i + c_yoff[random];  
  56.                 if (row < 0)   
  57.                     row = 0;  
  58.                 if (row >= _image.rows)  
  59.                     row = _image.rows - 1;  
  60.   
  61.                 col = j + c_xoff[random];  
  62.                 if (col < 0)   
  63.                     col = 0;  
  64.                 if (col >= _image.cols)  
  65.                     col = _image.cols - 1;  
  66.   
  67.                 samples[i][j][k]=_image.at<uchar>(row, col);  
  68.             }  
  69.         }  
  70.     }  
  71. }  
  72.   
  73. /**************** Test a new frame and update model ********************/  
  74. void ViBe_BGS::testAndUpdate(const Mat _image)  
  75. {  
  76.     RNG rng;  
  77.   
  78.     for(int i = 0; i < _image.rows; i++)  
  79.     {  
  80.         for(int j = 0; j < _image.cols; j++)  
  81.         {  
  82.             int matches(0), count(0);  
  83.             int dist;  
  84.   
  85.             while(matches < MIN_MATCHES && count < NUM_SAMPLES)  
  86.             {  
  87.                 dist = abs(samples[i][j][count] - _image.at<uchar>(i, j));  
  88.                 if (dist < RADIUS)  
  89.                     matches++;  
  90.                 count++;  
  91.             }  
  92.   
  93.             if (matches >= MIN_MATCHES)  
  94.             {  
  95.                 // It is a background pixel  
  96.                 samples[i][j][NUM_SAMPLES]=0;  
  97.   
  98.                 // Set background pixel to 0  
  99.                 m_mask.at<uchar>(i, j) = 0;  
  100.   
  101.                 // 如果一个像素是背景点,那么它有 1 / defaultSubsamplingFactor 的概率去更新自己的模型样本值  
  102.                 int random = rng.uniform(0, SUBSAMPLE_FACTOR);  
  103.                 if (random == 0)  
  104.                 {  
  105.                     random = rng.uniform(0, NUM_SAMPLES);  
  106.                     samples[i][j][random]=_image.at<uchar>(i, j);  
  107.                 }  
  108.   
  109.                 // 同时也有 1 / defaultSubsamplingFactor 的概率去更新它的邻居点的模型样本值  
  110.                 random = rng.uniform(0, SUBSAMPLE_FACTOR);  
  111.                 if (random == 0)  
  112.                 {  
  113.                     int row, col;  
  114.                     random = rng.uniform(0, 9);  
  115.                     row = i + c_yoff[random];  
  116.                     if (row < 0)   
  117.                         row = 0;  
  118.                     if (row >= _image.rows)  
  119.                         row = _image.rows - 1;  
  120.   
  121.                     random = rng.uniform(0, 9);  
  122.                     col = j + c_xoff[random];  
  123.                     if (col < 0)   
  124.                         col = 0;  
  125.                     if (col >= _image.cols)  
  126.                         col = _image.cols - 1;  
  127.   
  128.                     random = rng.uniform(0, NUM_SAMPLES);  
  129.                     samples[i][j][random]=_image.at<uchar>(i, j);  
  130.                 }  
  131.             }  
  132.             else  
  133.             {  
  134.                 // It is a foreground pixel  
  135.                 samples[i][j][NUM_SAMPLES]++;  
  136.   
  137.                 // Set background pixel to 255  
  138.                 m_mask.at<uchar>(i, j) = 255;  
  139.   
  140.                 //如果某个像素点连续N次被检测为前景,则认为一块静止区域被误判为运动,将其更新为背景点  
  141.                 if(samples[i][j][NUM_SAMPLES]>50)  
  142.                 {  
  143.                     int random = rng.uniform(0, NUM_SAMPLES);  
  144.                     if (random == 0)  
  145.                     {  
  146.                         random = rng.uniform(0, NUM_SAMPLES);  
  147.                         samples[i][j][random]=_image.at<uchar>(i, j);  
  148.                     }  
  149.                 }  
  150.             }  
  151.         }  
  152.     }  
  153. }  

main.cpp

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "ViBe.h"  
  2. #include <cstdio>  
  3.   
  4. using namespace cv;  
  5. using namespace std;  
  6.   
  7. int main(int argc, char* argv[])  
  8. {  
  9.     Mat frame, gray, mask;  
  10.     VideoCapture capture;  
  11.     capture.open(0);  
  12.     capture.set(CV_CAP_PROP_FRAME_WIDTH,320);  
  13.     capture.set(CV_CAP_PROP_FRAME_HEIGHT,240);  
  14.     if (!capture.isOpened())  
  15.     {  
  16.         cout<<"No camera or video input!\n"<<endl;  
  17.         return -1;  
  18.     }  
  19.   
  20.     ViBe_BGS Vibe_Bgs;  
  21.     bool count =true;  
  22.   
  23.     while (1)  
  24.     {  
  25.         capture >> frame;  
  26.         if (frame.empty())  
  27.             continue;  
  28.   
  29.         cvtColor(frame, gray, CV_RGB2GRAY);  
  30.         if (count)  
  31.         {  
  32.             Vibe_Bgs.init(gray);  
  33.             Vibe_Bgs.processFirstFrame(gray);  
  34.             cout<<" Training ViBe complete!"<<endl;  
  35.             count=false;  
  36.         }  
  37.         else  
  38.         {  
  39.             Vibe_Bgs.testAndUpdate(gray);  
  40.             mask = Vibe_Bgs.getMask();  
  41.             morphologyEx(mask, mask, MORPH_OPEN, Mat());  
  42.             imshow("mask", mask);  
  43.         }  
  44.   
  45.   
  46.         imshow("input", frame);   
  47.   
  48.         if ( cvWaitKey(10) == 27 )  
  49.             break;  
  50.     }  
  51.   
  52.     return 0;  
  53. }  

3、实验结果


                                                                          图1、背景图


                                                                             图2、前景图

总结,这里就不再贴太多图出来了,大家可以下载代码自己去玩一玩,挺好玩的,这个算法在作者的论文中被说得各种好,各种极品,但是在我的电脑中没有体现那么神乎其神的效果,可能没有加上其他的一些预处理和后处理的缘故吧,也可能是电脑问题,但是总体来说,这个算法确实也不错,算法原理也容易理解,对Ghost区域也做了很好的处理,但是算法已经申请了专利,做做研究还是可以的,还是有很多发展空间,如果商用,那我就不知道会咋样咯。

0 0
原创粉丝点击