国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

運動檢測ViBe算法python實現代碼

2020-02-16 11:33:23
字體:
來源:轉載
供稿:網友

運動物體檢測一般分為背景建模和運動物體分析兩步。即構建不包含運動物體的背景模型。然后將新的視頻幀和背景模型對比,找出其中的運動物體。目前比較好的背景建模算法有兩種: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             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁安市| 桐城市| 若羌县| 乌兰察布市| 黄山市| 渭源县| 米易县| 眉山市| 获嘉县| 峨山| 峨眉山市| 浦东新区| 盐池县| 辰溪县| 富川| 古田县| 兖州市| 镇巴县| 兴国县| 民权县| 团风县| 桃源县| 扎囊县| 乌海市| 巴林右旗| 寻甸| 永寿县| 习水县| 潜山县| 桃园县| 广安市| 栾川县| 贵定县| 兰西县| 玉屏| 阳泉市| 库伦旗| 苗栗县| 浦北县| 信宜市| 章丘市|