运动检测ViBe算法python实现代码


Posted in Python onJanuary 09, 2018

运动物体检测一般分为背景建模和运动物体分析两步。即构建不包含运动物体的背景模型。然后将新的视频帧和背景模型对比,找出其中的运动物体。目前比较好的背景建模算法有两种:1)文章(Zivkovic Z. (2004) Improved adaptive Gausianmixture model for  backgroundsubtraction, Proceedings of ICPR 2004, August 23-26, Cambridge, UK.)提出的高斯混合模型法。在此算法中,背景的每一个像素都被拟合到一个高斯混合模型。对于新的图片,只需要判断每个像素是否服从这个高斯混合模型就可以判断出这个像素是背景还是前景。但混合高斯算法的缺点是计算量相对比较大,速度偏慢,对光照敏感。2)文章(ViBe: A universal backgroundsubtraction algorithm for video sequences.)提出的ViBe算法。该算法速度非常快,计算量比较小,而且对噪声有一定的鲁棒性,检测效果不错。

由于最近在做一些跟踪检查的研究,就用到了ViBe算法,根据网上的c++版本编写了这个python版的算法,在这分享给大家。

class ViBe: 
 ''''' 
 classdocs 
 ''' 
 __defaultNbSamples = 20  #每个像素点的样本个数 
 __defaultReqMatches = 2  #min指数 
 __defaultRadius = 20;   #Sqthere半径 
 __defaultSubsamplingFactor = 16#子采样概率 
 __BG = 0      #背景像素 
 __FG = 255      #前景像素 
 __c_xoff=[-1,0,1,-1,1,-1,0,1,0] #x的邻居点 len=9 
 __c_yoff=[-1,0,1,-1,1,-1,0,1,0] #y的邻居点 len=9 
  
 __samples=[]    #保存每个像素点的样本值,len defaultNbSamples+1 
 __Height = 0 
 __Width = 0 
 
 def __init__(self, grayFrame): 
  ''''' 
  Constructor 
  ''' 
  self.__Height = grayFrame.shape[0] 
  self.__Width = grayFrame.shape[1] 
   
 
  for i in range(self.__defaultNbSamples+1): 
   self.__samples.insert(i,np.zeros((grayFrame.shape[0],grayFrame.shape[1]),dtype=grayFrame.dtype)); 
    
  self.__init_params(grayFrame) 
  
 def __init_params(self,grayFrame): 
  #记录随机生成的 行(r) 和 列(c) 
  rand=0 
  r=0 
  c=0 
 
  #对每个像素样本进行初始化 
  for y in range(self.__Height): 
   for x in range(self.__Width): 
    for k in range(self.__defaultNbSamples): 
     #随机获取像素样本值 
     rand=random.randint(0,8) 
     r=y+self.__c_yoff[rand] 
     if r<0: 
      r=0 
     if r>=self.__Height: 
      r=self.__Height-1 #行 
     c=x+self.__c_xoff[rand] 
     if c<0: 
      c=0 
     if c>=self.__Width: 
      c=self.__Width-1  #列 
     #存储像素样本值 
     self.__samples[k][y,x] = grayFrame[r,c] 
   self.__samples[self.__defaultNbSamples][y,x] = 0 
    
 def update(self,grayFrame,frameNo): 
  foreground = np.zeros((self.__Height,self.__Width),dtype=np.uint8) 
  for y in range(self.__Height): #Height 
   for x in range(self.__Width):  #Width 
    #用于判断一个点是否是背景点,index记录已比较的样本个数,count表示匹配的样本个数 
    count=0;index=0; 
    dist=0.0; 
    while (count<self.__defaultReqMatches) and (index<self.__defaultNbSamples): 
     dist= float(grayFrame[y,x]) - float(self.__samples[index][y,x]); 
     if dist<0: dist=-dist 
     if dist<self.__defaultRadius: count = count+1 
     index = index+1 
 
    if count>=self.__defaultReqMatches: 
     #判断为背景像素,只有背景点才能被用来传播和更新存储样本值 
     self.__samples[self.__defaultNbSamples][y,x]=0 
  
     foreground[y,x] = self.__BG 
  
     rand=random.randint(0,self.__defaultSubsamplingFactor) 
     if rand==0: 
      rand=random.randint(0,self.__defaultNbSamples) 
      self.__samples[rand][y,x]=grayFrame[y,x] 
     rand=random.randint(0,self.__defaultSubsamplingFactor) 
     if rand==0: 
      rand=random.randint(0,8) 
      yN=y+self.__c_yoff[rand] 
      if yN<0: yN=0 
      if yN>=self.__Height: yN=self.__Height-1 
      rand=random.randint(0,8) 
      xN=x+self.__c_xoff[rand] 
      if xN<0: xN=0 
      if xN>=self.__Width: xN=self.__Width-1 
      rand=random.randint(0,self.__defaultNbSamples) 
      self.__samples[rand][yN,xN]=grayFrame[y,x] 
    else: 
     #判断为前景像素 
     foreground[y,x] = self.__FG; 
     self.__samples[self.__defaultNbSamples][y,x] += 1 
     if self.__samples[self.__defaultNbSamples][y,x]>50: 
      rand=random.randint(0,self.__defaultNbSamples) 
      if rand==0: 
       rand=random.randint(0,self.__defaultNbSamples) 
       self.__samples[rand][y,x]=grayFrame[y,x] 
  return foreground

我做的鱼的跟踪效果图

运动检测ViBe算法python实现代码

运动检测ViBe算法python实现代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
深入理解Python中各种方法的运作原理
Jun 15 Python
CentOS安装pillow报错的解决方法
Jan 27 Python
使用Eclipse如何开发python脚本
Apr 11 Python
python读取一个目录下所有txt里面的内容方法
Jun 23 Python
pycharm 配置远程解释器的方法
Oct 28 Python
在Python 不同级目录之间模块的调用方法
Jan 19 Python
在python tkinter中Canvas实现进度条显示的方法
Jun 14 Python
在Python中画图(基于Jupyter notebook的魔法函数)
Oct 28 Python
pytorch方法测试——激活函数(ReLU)详解
Jan 15 Python
tensorflow的ckpt及pb模型持久化方式及转化详解
Feb 12 Python
python 实现ping测试延迟的两种方法
Dec 10 Python
Python3 用matplotlib绘制sigmoid函数的案例
Dec 11 Python
python+opencv实现动态物体识别
Jan 09 #Python
Python设计模式之门面模式简单示例
Jan 09 #Python
Python和Java进行DES加密和解密的实例
Jan 09 #Python
Python设计模式之中介模式简单示例
Jan 09 #Python
python+opencv实现动态物体追踪
Jan 09 #Python
全面了解Nginx, WSGI, Flask之间的关系
Jan 09 #Python
Python设计模式之代理模式简单示例
Jan 09 #Python
You might like
Windows PHP5和Apache的安装与配置
2009/06/08 PHP
php daddslashes()和 saddslashes()有哪些区别分析
2012/10/26 PHP
PHP图像处理类库MagickWand用法实例分析
2015/05/21 PHP
PHP7标量类型declare用法实例分析
2016/09/26 PHP
js实现的map方法示例代码
2014/01/13 Javascript
js使用正则实现ReplaceAll全部替换的方法
2014/07/18 Javascript
使用jquery解析XML示例代码
2014/09/05 Javascript
JavaScript中的方法重载实例
2015/03/16 Javascript
javascript事件冒泡实例分析
2015/05/13 Javascript
JavaScript中解析JSON数据的三种方法
2015/07/03 Javascript
jQuery获取页面及个元素高度、宽度的总结——超实用
2015/07/28 Javascript
ES6中的数组扩展方法
2016/08/26 Javascript
JavaScript中使用参数个数实现重载功能
2017/09/01 Javascript
JS实现去除数组中重复json的方法示例
2017/12/21 Javascript
js推箱子小游戏步骤代码解析
2018/01/10 Javascript
js实现左右两侧浮动广告
2018/07/09 Javascript
vue视图不更新情况详解
2019/05/16 Javascript
vue spa应用中的路由缓存问题与解决方案
2019/05/31 Javascript
不刷新网页就能链接新的js文件方法总结
2020/03/01 Javascript
如何在postman测试用例中实现断言过程解析
2020/07/09 Javascript
通过实例解析jQ Ajax操作相关原理
2020/09/23 Javascript
[46:55]Ti4 冒泡赛第二轮 LGD vs C9
2014/07/14 DOTA
[54:43]DOTA2-DPC中国联赛 正赛 CDEC vs Dynasty BO3 第一场 2月22日
2021/03/11 DOTA
插入排序_Python与PHP的实现版(推荐)
2017/05/11 Python
python 函数传参之传值还是传引用的分析
2017/09/07 Python
获取Django项目的全部url方法详解
2017/10/26 Python
python+pyqt实现右下角弹出框
2017/10/26 Python
python日期相关操作实例小结
2019/06/24 Python
TensorFlow学习之分布式的TensorFlow运行环境
2020/02/05 Python
Keds加拿大官网:购买帆布运动鞋和皮鞋
2019/09/26 全球购物
德国户外装备、登山运动和攀岩商店:tapir store
2020/02/12 全球购物
模具专业毕业推荐信
2014/03/08 职场文书
《欢乐的泼水节》教学反思
2014/04/22 职场文书
办公室日常管理制度
2015/08/04 职场文书
2016年村干部公开承诺书(公开承诺事项)
2016/03/25 职场文书
如何制作自己的原生JavaScript路由
2021/05/05 Javascript