資源簡介
該.py文件為基于vibe原理的目標檢測Python代碼。由于第一次寫Python,程序運行比較慢,歡迎交流。
運行方式:將該文件和名稱為video的avi文件放在同一個文件夾,然后運行即可。當然也可以在程序中修改待檢測的視頻文件名稱。
代碼片段和文件信息
#!?/usr/bin/env?python
from?cProfile?import?Profile
import?pstats
import?numpy?as?np
import?cv2
import?random
import?time
import?matplotlib.pyplot?as?plt
defaultNbSamples?=?20
defaultReqMatches?=?2
defaultRadius?=?20
defaultSubsamplingFactor?=?16
background?=?0
foreground?=?255
def?Initial_ViBe(gray?samples):
????#?gray?=?cv2.cvtColor(img?cv2.COLOR_BGR2GRAY)
????height?=?gray.shape[0]
????width?=?gray.shape[1]
????print(“*****Initlize?Time******“)
????start1?=?time.clock()
????for?i?in?range(height):
????????for?j?in?range(width):
????????????for?k?in?range(defaultNbSamples):
????????????????rand?=?round(random.uniform(-1?1))
????????????????r?=?i?+?rand
????????????????if?r?0:
????????????????????r?=?0
????????????????if?r?>?height:
????????????????????r?=?height?-?1
????????????????rand?=?round(random.uniform(-1?1))
????????????????c?=?j?+?rand
????????????????if?c?0:
????????????????????c?=?0
????????????????if?c?>?width:
????????????????????c?=?width?-?1
????????????????samples[i][j][k]?=?gray[i][j]
????????????samples[i][j][defaultNbSamples?-?1]?=?0
????end?=?time.clock()
????print(end?-?start1)
????print(“******************************“)
????return?samples
def?update(gray?samples):
????height?=?gray.shape[0]
????width?=?gray.shape[1]
????#?print(height?width)
????#?segMat?=?np.zeros((gray.shape[0]?gray.shape[1]?1)?np.uint8)
????segMat?=?np.zeros((gray.shape[0]?gray.shape[1]?1))
????for?i?in?range(height):
????????for?j?in?range(width):
????????????count?=?0
????????????idex?=?0
????????????dist?=?0
????????????#?遍歷每個像素,判斷與背景樣本的相似程度
????????????while?count?????????????????dist?=?abs(gray[i][j]?-?samples[i][j][idex])
????????????????if?dist?????????????????????#?統計相似度小于閾值的個數
????????????????????count?=?count?+?1
????????????????idex?=?idex?+?1
????????????#?大于#min,則認為該點為背景點
????????????if?count?>=?defaultReqMatches:
????????????????#?判斷為背景像素,只有背景點才可以被用來傳播和更新存儲樣本值
????????????????samples[i][j][defaultNbSamples?-?1]?=?0
????????????????#?輸出二值圖像用
????????????????segMat[i][j]?=?background
????????????????rand?=?round(random.uniform(0?defaultSubsamplingFactor?-?1))
????????????????if?rand?==?0:
????????????????????rand?=?round(random.uniform(0?defaultNbSamples?-?1))
????????????????????samples[i][j][rand]?=?gray[i][j]
????????????????rand?=?round(random.uniform(0?defaultSubsamplingFactor?-?1))
????????????????if?rand?==?0:
????????????????????rand?=?round(random.uniform(-1?1))
????????????????????iN?=?i?+?rand
????????????????????#?以下是防止越界
????????????????????if?iN?0:
????????????????????????iN?=?0
????????????????????if?iN?>?height:
?
- 上一篇:python首次連接STK
- 下一篇:強化學習Q-learning算法
評論
共有 條評論