Python implementation code of the ViBe algorithm and python code of the vibe Algorithm
Motion Object detection is generally divided into two steps: Background Modeling and Motion Object analysis. That is, to build a background model that does not contain motion objects. Then compare the new video frame and background model to find the moving object. At present, there are two good background modeling algorithms: 1) Article (Zivkovic Z. (2004) Improved adaptive Gausianmixture model for backgroundsubtraction, Proceedings of ICPR 2004, August 23-26, Cambridge, UK .) gaussian mixture model method. In this algorithm, each pixel of the background is fitted to a Gaussian mixture model. For a new image, you only need to determine whether each pixel complies with the Gaussian mixture model to determine whether the pixel is the background or foreground. However, the disadvantage of the Gaussian mixture algorithm is that the computing workload is relatively large, the speed is slow, and the algorithm is sensitive to light. 2) ViBe algorithm proposed in the article (ViBe: A universal backgroundsubtraction algorithm for video sequences. The algorithm is fast, has a small amount of computing, and has a certain robustness to noise, and the detection effect is good.
I recently used the ViBe Algorithm for some tracking checks. I wrote this python Algorithm Based on the c ++ version on the Internet. I will share it with you here.
Class ViBe: ''' classdocs ''' _ defaultNbSamples = 20 # number of samples per pixel _ defaultReqMatches = 2 # min index _ defaultRadius = 20; # Sqthere radius _ defaultSubsamplingFactor = 16 # subsampling probability _ BG = 0 # background pixel _ FG = 255 # foreground pixel _ c_xoff = [-, 1, -,-,] # neighbor len of x = 9 _ c_yoff = [-, 1, 1, 0] # y's neighbor len = 9 _ samples = [] # Save the sample value of each pixel, 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): # record randomly generated rows (r) and columns (c) rand = 0 r = 0 c = 0 # initialize each pixel sample for y in range (self. _ Height): for x in range (self. _ Width): for k in range (self. _ defaultNbSamples): # obtain the pixel sample value rand = random randomly. randint (0, 8) r = y + self. _ c_yoff [rand] if r <0: r = 0 if r> = self. _ Height: r = self. _ Height-1 # Row c = x + self. _ c_xoff [rand] if c <0: c = 0 if c> = self. _ Width: c = self. _ Width-1 # column # store the pixel sample value 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 # used to determine whether a vertex is a background vertex. index records the number of compared samples. count indicates the number of matched samples. 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: # It is determined as a background pixel. Only the background points can be used to spread and update the stored sample value self. _ samples [self. _ defaultNbSamples] [y, x] = 0 foreground [y, x] = self. _ BG rand = random. randint (0, self. _ defasubsubsamplingfactor) if rand = 0: rand = random. randint (0, self. _ defaultNbSamples) self. _ samples [rand] [y, x] = grayFrame [y, x] rand = random. randint (0, self. _ defasubsubsamplingfactor) 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: # determine as foreground pixel 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
My fish tracking
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.