OpenCV-- background modeling

Background modeling

The Frame

Because target motion in the scene, a different position of the target image at different image frame. Such algorithm on two consecutive frame image difference operation time, pixels corresponding to different frames subtraction gradation difference absolute value is determined, when the absolute value exceeds a certain threshold, the moving object can be determined that, to achieve the objectives the detection function. Frame difference method is very simple, but will

The introduction of noise and empty question.

 

 Gaussian mixture model

Before performing foreground detection, background prior to training, for each of the background image using a Gaussian mixture model to simulate, the number of each Gaussian mixture background may be adaptive. Then in the testing phase, the new pixel to be GMM match, if the pixel values ​​can match one of Gauss, it is considered to be the background, otherwise considered a prospect. Since the whole process

GMM model is continually updated study, so there are certain robustness of dynamic background. Finally, through a dynamic background branches sway foreground detection, and achieved good results.

In the video for the change pixel should be Gaussian distribution

 

 Background of the actual distribution of a plurality of Gaussian distribution should be mixed together, with each Gaussian model to be entitled weight

 

 Gaussian mixture model learning method

- 1 first initializes each Gaussian model matrix parameters.

- T 2. Take the video image frame data used to train the Gaussian mixture model. After a first pixel to use it as the first Gaussian distribution.

- back to 3. When the pixel value of the previous Gaussian conventional means comparison, if the pixel value of the mean difference in its model 3 times the variance, that belong to the profile, and subjected to the parameter update.

- 4. If a pixel does not satisfy current to a Gaussian distribution, use it to create a new Gaussian distribution.

Gaussian mixture model test method

During the testing phase of the new pixel value and the Gaussian mixture model to compare each mean, if the difference between 2 times the variance, it is considered to be the background, otherwise considered a prospect. The outlook assigned to 255, background assigned to 0. Thus forming a binary foreground FIG.

import numpy as np
import cv2

#经典的测试视频
cap = cv2.VideoCapture('test.avi')
#形态学操作需要使用
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
#创建混合高斯模型用于背景建模
fgbg = cv2.createBackgroundSubtractorMOG2()

while(True):
    ret, frame = cap.read()
    fgmask = fgbg.apply(frame)
    #形态学开运算去噪点
    fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
    #寻找视频中的轮廓
    im, contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for c in contours:
        #计算各轮廓的周长
        perimeter = cv2.arcLength(c,True)
        if perimeter > 188:
            #找到一个直矩形(不会旋转)
            x,y,w,h = cv2.boundingRect(c)
            #画出这个矩形
            cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)    

    cv2.imshow('frame',frame)
    cv2.imshow('fgmask', fgmask)
    k = cv2.waitKey(150) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()

效果:

Guess you like

Origin www.cnblogs.com/SCCQ/p/12304808.html