运动检测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 ORM框架SQLAlchemy学习笔记之映射类使用实例和Session会话介绍
Jun 10 Python
Python获取Windows或Linux主机名称通用函数分享
Nov 22 Python
Python使用multiprocessing创建进程的方法
Jun 04 Python
python-opencv在有噪音的情况下提取图像的轮廓实例
Aug 30 Python
Python3 加密(hashlib和hmac)模块的实现
Nov 23 Python
Python比较2个时间大小的实现方法
Apr 10 Python
Python实现的网页截图功能【PyQt4与selenium组件】
Jul 12 Python
对python函数签名的方法详解
Jan 22 Python
基于Python共轭梯度法与最速下降法之间的对比
Apr 02 Python
python实现人像动漫化的示例代码
May 17 Python
利用Python实时获取steam特惠游戏数据
Jun 25 Python
如何基于python实现单目三维重建详解
Jun 25 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
fleaphp下不确定的多条件查询的巧妙解决方法
2008/09/11 PHP
jQuery中的RadioButton,input,CheckBox取值赋值实现代码
2014/02/18 PHP
PHP常见字符串处理函数用法示例【转换,转义,截取,比较,查找,反转,切割】
2016/12/24 PHP
PHP读取CSV大文件导入数据库的实例
2017/07/24 PHP
phpStudy配置多站点多域名和多端口的方法
2017/09/01 PHP
php 多进程编程父进程的阻塞与非阻塞实例分析
2020/02/22 PHP
javascript 复杂的嵌套环境中输出单引号和双引号
2009/05/26 Javascript
js setTimeout opener的用法示例详解
2013/10/23 Javascript
js实现同一页面可多次调用的图片幻灯切换效果
2015/02/28 Javascript
微信小程序  Mustache语法详细介绍
2016/10/27 Javascript
js 文字超出长度用省略号代替,鼠标悬停并以悬浮框显示实例
2016/12/06 Javascript
jQuery实现的省市联动菜单功能示例【测试可用】
2017/01/13 Javascript
javascript 判断一个对象为数组的方法
2017/05/03 Javascript
原生JavaScript实现精美的淘宝轮播图效果示例【附demo源码下载】
2017/05/27 Javascript
微信小程序实现自动定位功能
2018/10/31 Javascript
postman自定义函数实现 时间函数的思路详解
2019/04/17 Javascript
webpack4 SplitChunks实现代码分隔详解
2019/05/23 Javascript
微信小程序图片加载失败时替换为默认图片的方法
2019/12/09 Javascript
python根据出生日期获得年龄的方法
2015/03/31 Python
pip命令无法使用的解决方法
2018/06/12 Python
Python-Seaborn热图绘制的实现方法
2019/07/15 Python
python使用socket 先读取长度,在读取报文内容示例
2019/09/26 Python
40个你可能不知道的Python技巧附代码
2020/01/29 Python
Python爬虫防封ip的一些技巧
2020/08/06 Python
如何用python免费看美剧
2020/08/11 Python
英文自荐信
2013/12/19 职场文书
教育技术学专业职业规划书
2014/03/03 职场文书
品牌宣传方案
2014/03/21 职场文书
乡镇消防工作实施方案
2014/03/27 职场文书
无刑事犯罪记录证明范本
2014/09/29 职场文书
2014年财政局工作总结
2014/12/09 职场文书
病人慰问信范文
2015/02/15 职场文书
工作自我评价范文
2015/03/05 职场文书
起诉状范本
2015/05/20 职场文书
战马观后感
2015/06/08 职场文书
python单元测试之pytest的使用
2021/06/07 Python