opencv image processing - background modeling

The course comes from the beep station Tang Yudi [Computer Vision-OpenCV] Dr. Tang Yudi taught me a full set of OpenCV OpenCV computer vision practical courses that I haven't learned in four years in college (with courseware materials + courseware notes + source code)_哔哩哔哩_bilibili


Table of contents

 background modeling

frame difference method 

 mixed Gaussian model

 Hybrid Gaussian Model Learning Method

Mixed Gaussian Model Test Method 


 background modeling

frame difference method 

Because the target in the scene is moving in the cloud, the position of the target's image in different image frames is different. This type of algorithm performs differential operations on two consecutive frames of images in time, and subtracts the pixels corresponding to different frames to determine the absolute value of the gray level difference. When the absolute value exceeds a certain threshold, it can be judged as a moving target, so as to achieve the goal detection function.

 

 The previous frame is subtracted from the next frame, and the grayscale difference is greater than a threshold, then the value 255 is reserved, otherwise it is set to 0.

  • The frame difference method is very simple, but it will introduce noise and hole problems

 mixed Gaussian model

 Before the foreground detection, the background is trained first, and a mixed Gaussian model is used for each background in the image to simulate, and the number of mixed Gaussians for each background can be adaptive. Then in the test phase, GMM matching is performed on the new pixel. If the pixel value can match one of the Gaussians, it is considered as the background, otherwise it is considered as the foreground. Since the GMM model is constantly updating and learning throughout the process, it is robust to dynamic backgrounds. Finally, by performing foreground detection on a dynamic background with swaying branches, better results have been achieved.

The change of pixel points in the video should conform to the Gaussian distribution.

 The actual distribution of the background should be a mixture of multiple Gaussian distributions, and each Gaussian model can also have weights

 Hybrid Gaussian Model Learning Method

  1.  First initialize each Gaussian model matrix parameter
  2. Take the T frame data image in the video to train the Gaussian mixture model. After the first pixel comes, use it as the first Gaussian distribution.
  3. When the pixel value comes later, compared with the previous Gaussian mean, if the difference between the value of the pixel point and the mean value of the model is within 3 times the variance, it belongs to the distribution, and its parameters are updated.
  4. If the next faint pixel does not satisfy the current Gaussian distribution, use it to create a new Gaussian distribution.

Mixed Gaussian Model Test Method 

 In the test phase, the value of the new pixel is compared with each mean in the mixed Gaussian model. If the difference is within 2 times the variance, it is considered as the background, otherwise it is considered as the foreground. The foreground is assigned a value of 255, and the background is assigned a value of 0. This forms a foreground binary image.

import numpy as np
import cv2

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

while(True):
    ret, frame = cap.read()#一帧一帧读取视频
    fgmask = fgbg.apply(frame)#将获取到的每一帧图像都应用到当前的背景提取当中,前景置为255,背景置为0
    #形态学中开运算去掉噪音点
    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:#表示Esc退出
        break

cap.release()
cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/weixin_58176527/article/details/125463785