運(yùn)動(dòng)檢測(cè)ViBe算法python實(shí)現(xiàn)代碼
運(yùn)動(dòng)物體檢測(cè)一般分為背景建模和運(yùn)動(dòng)物體分析兩步。即構(gòu)建不包含運(yùn)動(dòng)物體的背景模型。然后將新的視頻幀和背景模型對(duì)比,找出其中的運(yùn)動(dòng)物體。目前比較好的背景建模算法有兩種:1)文章(Zivkovic Z. (2004) Improved adaptive Gausianmixture model for backgroundsubtraction, Proceedings of ICPR 2004, August 23-26, Cambridge, UK.)提出的高斯混合模型法。在此算法中,背景的每一個(gè)像素都被擬合到一個(gè)高斯混合模型。對(duì)于新的圖片,只需要判斷每個(gè)像素是否服從這個(gè)高斯混合模型就可以判斷出這個(gè)像素是背景還是前景。但混合高斯算法的缺點(diǎn)是計(jì)算量相對(duì)比較大,速度偏慢,對(duì)光照敏感。2)文章(ViBe: A universal backgroundsubtraction algorithm for video sequences.)提出的ViBe算法。該算法速度非常快,計(jì)算量比較小,而且對(duì)噪聲有一定的魯棒性,檢測(cè)效果不錯(cuò)。
由于最近在做一些跟蹤檢查的研究,就用到了ViBe算法,根據(jù)網(wǎng)上的c++版本編寫了這個(gè)python版的算法,在這分享給大家。
class ViBe:
'''''
classdocs
'''
__defaultNbSamples = 20 #每個(gè)像素點(diǎn)的樣本個(gè)數(shù)
__defaultReqMatches = 2 #min指數(shù)
__defaultRadius = 20; #Sqthere半徑
__defaultSubsamplingFactor = 16#子采樣概率
__BG = 0 #背景像素
__FG = 255 #前景像素
__c_xoff=[-1,0,1,-1,1,-1,0,1,0] #x的鄰居點(diǎn) len=9
__c_yoff=[-1,0,1,-1,1,-1,0,1,0] #y的鄰居點(diǎn) len=9
__samples=[] #保存每個(gè)像素點(diǎn)的樣本值,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):
#記錄隨機(jī)生成的 行(r) 和 列(c)
rand=0
r=0
c=0
#對(duì)每個(gè)像素樣本進(jìn)行初始化
for y in range(self.__Height):
for x in range(self.__Width):
for k in range(self.__defaultNbSamples):
#隨機(jī)獲取像素樣本值
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 #列
#存儲(chǔ)像素樣本值
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
#用于判斷一個(gè)點(diǎn)是否是背景點(diǎn),index記錄已比較的樣本個(gè)數(shù),count表示匹配的樣本個(gè)數(shù)
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:
#判斷為背景像素,只有背景點(diǎn)才能被用來(lái)傳播和更新存儲(chǔ)樣本值
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
我做的魚的跟蹤效果圖


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python opencv實(shí)現(xiàn)運(yùn)動(dòng)檢測(cè)
- python開發(fā)之thread實(shí)現(xiàn)布朗運(yùn)動(dòng)的方法
- 用Python寫飛機(jī)大戰(zhàn)游戲之pygame入門(4):獲取鼠標(biāo)的位置及運(yùn)動(dòng)
- 基于python OpenCV實(shí)現(xiàn)動(dòng)態(tài)人臉檢測(cè)
- Python3.6.0+opencv3.3.0人臉檢測(cè)示例
- Python基于OpenCV實(shí)現(xiàn)視頻的人臉檢測(cè)
- OpenCV-Python實(shí)現(xiàn)輪廓檢測(cè)實(shí)例分析
- Python+OpenCV目標(biāo)跟蹤實(shí)現(xiàn)基本的運(yùn)動(dòng)檢測(cè)
相關(guān)文章
Python調(diào)用百度AI實(shí)現(xiàn)圖片上文字識(shí)別功能實(shí)例
百度AI功能還是很強(qiáng)大的,百度AI開放平臺(tái)真的是測(cè)試接口的天堂,免費(fèi)接口很多,當(dāng)然有量的限制,但個(gè)人使用是完全夠用的,下面這篇文章主要給大家介紹了關(guān)于Python調(diào)用百度AI實(shí)現(xiàn)圖片上文字識(shí)別功能的相關(guān)資料,需要的朋友可以參考下2021-09-09
詳解Python并發(fā)編程之從性能角度來(lái)初探并發(fā)編程
這篇文章主要介紹了詳解Python并發(fā)編程之從性能角度來(lái)初探并發(fā)編程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Django使用redis緩存服務(wù)器的實(shí)現(xiàn)代碼示例
這篇文章主要介紹了Django使用redis緩存服務(wù)器的實(shí)現(xiàn)代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
關(guān)于pandas-profiling的降級(jí)之旅
這篇文章主要介紹了關(guān)于pandas-profiling的降級(jí)之旅,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11

