Traditional target tracking - background segmentation method

Table of contents

1. Background Segmentation

2. Process

3. Code

Four. Summary


1. Background Segmentation

        The traditional foreground and background segmentation methods include GrabCut, watershed algorithm, and of course some threshold segmentation algorithms. However, these algorithms often appear to be less robust in application, and cannot achieve a good segmentation effect.

        Modern background segmentation algorithms incorporate some methods of machine learning to improve the classification effect. Such as KNN, mixed Gaussian (MOG2), Geometric Multigrid . The basic principle of these algorithms is to learn the environment of each frame image, so as to infer the background area.

        OpenCV's BackgroundSubtractor provides these modern background segmentation algorithms.

        The principle is to delete things that don't move.

2. Process

Currently opencv4 implements a variety of background segmentation functions,

What types of video background/foreground segmentation (background modeling/foreground extraction) algorithms are there in OpenCV4, what are their respective algorithm principles and characteristics, and sample codes are attached

3. Code

        Take KNN as an example to implement

import numpy as np
import cv2
from skimage.segmentation import slic, mark_boundaries


camera = cv2.VideoCapture("E:\Python-Code/videodataset/enn.mp4")

bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)  # 类实例一个对象
# mog1 = cv2.bgsegm.createBackgroundSubtractorMOG()  # 创建mog对象
# mog2 = cv2.createBackgroundSubtractorMOG2()
# mog3 = cv2.bgsegm.createBackgroundSubtractorGMG(30)  # 初始化帧数取30吧

while (camera.isOpened()):
    ret, frame = camera.read()
    if ret:
        fgmask = bs.apply(frame)  # 前景掩码的获取

        th = cv2.threshold(fgmask.copy(), 224, 255, cv2.THRESH_BINARY)[1]  # 阈值得到黑白图
        dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)),
                             iterations=2)  # 膨胀操作
        contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        for c in contours:
            if cv2.contourArea(c) > 1600:  # 用面积来限制显示的识别对象,面积大于1600时画矩形
                (x, y, w, h) = cv2.boundingRect(c)
                cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2)

        cv2.imshow('mog', fgmask)
        cv2.imshow('thresh', th)
        cv2.imshow('detection', frame)
    else:
        break
    #    cv2.imshow('frame',fgmask)

    if cv2.waitKey(100) & 0xff == 27:  # ESC退出键的ASCII码是27
        break

camera.release()
cv2.destroyAllWindows()

Four. Summary

It seems to work very poorly.

It will automatically recognize moving targets, no need to use the mouse to draw a target, (it seems that because of this, the effect is very poor)

Guess you like

Origin blog.csdn.net/weixin_45823221/article/details/128491519