运动检测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 字符串中的字符倒转
Sep 06 Python
Django中URLconf和include()的协同工作方法
Jul 20 Python
Python通过future处理并发问题
Oct 17 Python
Python编程scoketServer实现多线程同步实例代码
Jan 29 Python
pycharm新建一个python工程步骤
Jul 16 Python
使用Python实现图像标记点的坐标输出功能
Aug 14 Python
VSCode中自动为Python文件添加头部注释
Nov 14 Python
Python 时间戳之获取整点凌晨时间戳的操作方法
Jan 28 Python
python GUI库图形界面开发之PyQt5菜单栏控件QMenuBar的详细使用方法与实例
Feb 28 Python
PyTorch-GPU加速实例
Jun 23 Python
python实现无边框进度条的实例代码
Dec 30 Python
python爬虫框架feapde的使用简介
Apr 20 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 程序员也要学会使用“异常”
2009/06/16 PHP
php stream_get_meta_data返回值
2013/09/29 PHP
初识PHP中的Swoole
2016/04/05 PHP
[原创]图片分页查看
2006/08/28 Javascript
不错的JS中变量相关的细节分析
2007/08/13 Javascript
ASP Json Parser修正版
2009/12/06 Javascript
JavaScript实现快速排序(自已编写)
2012/12/19 Javascript
JS实现仿QQ聊天窗口抖动特效
2015/05/10 Javascript
javascript基本数据类型及类型检测常用方法小结
2016/12/14 Javascript
JS验证input输入框(字母,数字,符号,中文)
2017/03/23 Javascript
ionic3 懒加载
2017/08/16 Javascript
javascript高级模块化require.js的具体使用方法
2017/10/31 Javascript
jQuery+SpringMVC中的复选框选择与传值实例
2018/01/08 jQuery
Vue中render函数的使用方法
2018/01/31 Javascript
详解从NodeJS搭建中间层再谈前后端分离
2018/11/13 NodeJs
微信小程序页面渲染实现方法
2019/11/06 Javascript
JS实现盒子拖拽效果
2020/02/06 Javascript
解决VUE项目使用Element-ui 下拉组件的验证失效问题
2020/11/07 Javascript
[03:56]DOTA2完美大师赛趣味视频之小鸽子和Mineski打台球
2017/11/24 DOTA
django 2.0更新的10条注意事项总结
2018/01/05 Python
python 实现登录网页的操作方法
2018/05/11 Python
对python中的高效迭代器函数详解
2018/10/18 Python
python验证身份证信息实例代码
2019/05/06 Python
详解pytorch 0.4.0迁移指南
2019/06/16 Python
一行python实现树形结构的方法
2019/08/09 Python
基于pandas向csv添加新的行和列
2020/05/25 Python
pytorch 常用函数 max ,eq说明
2020/06/28 Python
matplotlib 多个图像共用一个colorbar的实现示例
2020/09/10 Python
python 实现围棋游戏(纯tkinter gui)
2020/11/13 Python
python使用requests库爬取拉勾网招聘信息的实现
2020/11/20 Python
J2SDK1.5与J2SDK5.0有什么区别
2012/09/19 面试题
MBA推荐信怎么写
2015/03/25 职场文书
2015年党建工作总结
2015/03/30 职场文书
配置nginx 重定向到系统维护页面
2021/06/08 Servers
Html5获取用户当前位置的几种方式
2022/01/18 HTML / CSS
英国数字版游戏销量周榜公布 《小缇娜的奇幻之地》登顶
2022/04/03 其他游戏