运动检测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中的对象,方法,类,实例,函数用法分析
Jan 15 Python
举例讲解如何在Python编程中进行迭代和遍历
Jan 19 Python
Python获取linux主机ip的简单实现方法
Apr 18 Python
使用Pyinstaller的最新踩坑实战记录
Nov 08 Python
用Pygal绘制直方图代码示例
Dec 07 Python
Python3 Tkinter选择路径功能的实现方法
Jun 14 Python
解决pycharm运行程序出现卡住scanning files to index索引的问题
Jun 27 Python
python输出决策树图形的例子
Aug 09 Python
springboot配置文件抽离 git管理统 配置中心详解
Sep 02 Python
django框架中间件原理与用法详解
Dec 10 Python
python 实现提取log文件中的关键句子,并进行统计分析
Dec 24 Python
pytorch MSELoss计算平均的实现方法
May 12 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
一步一步学习PHP(8) php 数组
2010/03/05 PHP
PHP文件操作实例总结【文件上传、下载、分页】
2018/12/08 PHP
js实现兼容IE6与IE7的DIV高度
2010/05/13 Javascript
JS DOM 操作实现代码
2010/08/01 Javascript
给超链接添加特效鼠标移动展示提示信息且随鼠标移动
2013/10/17 Javascript
jquery cookie的用法总结
2013/11/18 Javascript
js实现ifram取父窗口URL地址的方法
2015/02/09 Javascript
基于JavaScript实现全屏透明遮罩div层锁屏效果
2016/01/26 Javascript
解决mpvue + vuex 开发微信小程序vuex辅助函数mapState、mapGetters不可用问题
2018/08/03 Javascript
微信小程序支付PHP代码
2018/08/23 Javascript
vue-cli 3.0 版本与3.0以下版本在搭建项目时的区别详解
2018/12/11 Javascript
JavaScript之scrollTop、scrollHeight、offsetTop、offsetHeight等属性学习笔记
2020/07/15 Javascript
JavaScript实现筛选数组
2021/03/02 Javascript
[08:54]DOTA2-DPC中国联赛 正赛 Aster vs LBZS 选手采访
2021/03/11 DOTA
python创建线程示例
2014/05/06 Python
Python使用稀疏矩阵节省内存实例
2014/06/27 Python
Python中的并发编程实例
2014/07/07 Python
详解Python中的循环语句的用法
2015/04/09 Python
python中异常捕获方法详解
2017/03/03 Python
浅谈django开发者模式中的autoreload是如何实现的
2017/08/18 Python
一文秒懂python读写csv xml json文件各种骚操作
2019/07/04 Python
Python基于read(size)方法读取超大文件
2020/03/12 Python
python使用梯度下降算法实现一个多线性回归
2020/03/24 Python
基于python实现监听Rabbitmq系统日志代码示例
2020/11/28 Python
Otticanet英国:最顶尖的世界名牌眼镜, 能得到打折季的价格
2019/02/10 全球购物
印度手工编织服装和家居用品商店:Fabindi
2019/10/07 全球购物
Douglas意大利官网:购买香水和化妆品
2020/05/27 全球购物
写好自荐信要注意的问题
2013/11/10 职场文书
化学学院毕业生自荐信范文
2013/12/17 职场文书
《两个铁球同时着地》教学反思
2014/02/13 职场文书
圣贤教育改变命运观后感
2015/06/16 职场文书
钓鱼岛事件感想
2015/08/11 职场文书
事业单位工作人员2015年度思想工作总结
2015/10/15 职场文书
动画电影《擅长捉弄人的高木同学》6月10日上映!
2022/03/20 日漫
一文了解MYSQL三大范式和表约束
2022/04/03 MySQL
如何更改Win11声音输出设备?Win11声音输出设备四种更改方法
2022/04/08 数码科技