美文网首页
OpenCV+Python 基于背景分割的运动检测

OpenCV+Python 基于背景分割的运动检测

作者: 音符纸飞机 | 来源:发表于2018-11-01 23:21 被阅读116次

OpenCV提供了一个称为BackgroundSubtractor的类,在分割前景和背景时很方便。
在OpenCV3中有三种背景分割器:K-Nearest-Neighbors(KNN)、Mixture of Gaussians(MOG2)、Geometric Multigid(GMG)
背景分割博文1

OpenCV+Python图像分割区别

BackgroundSubtractor类专门为视频而设计的,在帧之间作比较,并且记录帧历史。该类的另外一个特性是可以计算出阴影区域。
各种背景分割算法比较paper

MOG2

An Improved Adaptive Background Mixture Model for Realtime Tracking with Shadow Detection 2002
Improved Adaptive Gaussian Mixture Model for Background Subtraction 2004 Zoran Zivkovi

def createBackgroundSubtractorMOG2(history=None, varThreshold=None, detectShadows=None): 
    """
    createBackgroundSubtractorMOG2([, history[, varThreshold[, detectShadows]]]) -> retval
    .   @brief Creates MOG2 Background Subtractor
    .   
    .   @param history Length of the history. history越大,learningRate越小,背景更新越慢
    .   @param varThreshold Threshold on the squared Mahalanobis distance between the pixel and the model
    .   to decide whether a pixel is well described by the background model. This parameter does not
    .   affect the background update. 方差阈值,用于判断当前像素是前景还是背景
    .   @param detectShadows If true, the algorithm will detect shadows and mark them. It decreases the
    .   speed a bit, so if you do not need this feature, set the parameter to false.
    """

0 背景 127 阴影 255 前景

GMG

Visual Tracking of Human Visitors under Variable-Lighting Conditions for a Responsive Audio Art Installation 2012

def createBackgroundSubtractorGMG(initializationFrames=None, decisionThreshold=None): 
    """
    createBackgroundSubtractorGMG([, initializationFrames[, decisionThreshold]]) -> retval
    .   @brief Creates a GMG Background Subtractor
    .   
    .   @param initializationFrames number of frames used to initialize the background models.
    .   @param decisionThreshold Threshold value, above which it is marked foreground, else background.
    """

KNN

Efficient Adaptive Density Estimation per Image Pixel for the Task of Background Subtraction 2006 Zoran Zivkovic

def createBackgroundSubtractorKNN(history=None, dist2Threshold=None, detectShadows=None): 
    """
    createBackgroundSubtractorKNN([, history[, dist2Threshold[, detectShadows]]]) -> retval
    .   @brief Creates KNN Background Subtractor
    .   
    .   @param history Length of the history.
    .   @param dist2Threshold Threshold on the squared distance between the pixel and the sample to decide
    .   whether a pixel is close to that sample. This parameter does not affect the background update.
    .   @param detectShadows If true, the algorithm will detect shadows and mark them. It decreases the
    .   speed a bit, so if you do not need this feature, set the parameter to false.
    """
实例
        mog = cv2.createBackgroundSubtractorMOG2()
        # gmg = cv2.bgsegm.createBackgroundSubtractorGMG(initializationFrames=1)
        # bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)
        while self._window_manager.is_window_created:
            self._capture_manager.enter_frame()
            frame = self._capture_manager.frame
            gmask = mog.apply(frame)
            binary = cv2.threshold(gmask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
            dilated = cv2.dilate(binary, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=2)
            image, contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
            for c in contours:
                if cv2.contourArea(c) > 1600:
                    (x, y, w, h) = cv2.boundingRect(c)
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2)
            self._capture_manager.frame = frame

相关文章

  • OpenCV+Python 基于背景分割的运动检测

    OpenCV提供了一个称为BackgroundSubtractor的类,在分割前景和背景时很方便。在OpenCV3...

  • DBNet 解析

    1. 背景介绍 文本检测分为基于回归和基于分割两种方法,DBNet 的原理是基于分割算法。对于一般分割算法流程:先...

  • OpenCV+Python实现图像运动模糊和高斯模糊!它是编程界

    运动模糊: 由于相机和物体之间的相对运动造成的模糊,又称为动态模糊 OpenCV+Python实现运动模糊,主要用...

  • 车道线检测算法-Ultra-Fast-Lane-Detectio

    车道线检测算法通常分为两种类型:一种是基于基于视觉特征来做语义分割或者实例分割,例如LaneNet和SCNN;另一...

  • 医学图像分割及应用

    截至目前,我们已经学习了很多关于图像分割的相关算法,就此,对图像的分割算法做以下总结: 基于边界驱动的分割边缘检测...

  • 生物视觉的目标检测研究

    生物视觉的目标检测研究目标检测,也叫目标提取,是一种基于目标几何和统计特征的图像分割,它将目标的分割和识别合二为一...

  • DB:Real-time Scene Text Detectio

    DBNet 简介 由于分割网络的结果可以准确描述诸如扭曲文本的场景,因而基于分割的自然场景文本检测方法变得流行起来...

  • [AAAI2020]论文翻译DB:Real-time Scene

    Abstract 近年来,基于分割的方法在场景文本检测中非常流行,因为分割结果可以更准确地描述各种形状的场景文本,...

  • MMDetection: Open MMLab Detectio

    MMDetection是一个基于PyTorch的开源物体检测工具包。包括物体检测、实例分割,以及相关的模块。不仅包...

  • OpenCV+Python图像分割

    分水岭算法 用于分割多个相邻的物体。 原理 灰度图像根据灰度值可以把像素之间的关系看成山峰和山谷的关系,高亮度(灰...

网友评论

      本文标题:OpenCV+Python 基于背景分割的运动检测

      本文链接:https://www.haomeiwen.com/subject/ejzntqtx.html