opencv2中的背景减法 [英] Background subtraction in opencv2

查看:696
本文介绍了opencv2中的背景减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过删除静态(主要是)BG元素来使用opencv2检测前景运动。我使用的方法是基于一系列图像的平均值 - 代表背景。然后计算一个标准偏差高于和低于该平均值。使用它作为检测前景运动的窗口。

I am trying to detect foreground motion using opencv2 by removing static (mostly) BG elements. The method I am using is based on taking the mean of a series of images - representing the background. Then calculating one Standard deviation above and below that mean. Using that as a window to detect foreground motion.

据报道,这种机制适用于中等嘈杂的环境,如在BG中挥动树木。

This mechanism reportedly works well for moderately noisy environments like waving trees in the BG.

所需的输出是可以在后续操作中使用的掩码,以便最小化进一步处理。具体来说,我将在该区域内使用光流检测。

The desired output is a mask that can be used in a subsequent operation so as to minimise further processing. Specifically I am going to use optical flow detection within that region.

cv2使这更容易,代码更易于阅读和理解。谢谢cv2和numpy。

cv2 has made this much easier and the code is much simpler to read and understand. Thanks cv2 and numpy.

但是我在进行正确的FG检测时遇到了困难。

But I am having difficulty doing the correct FG detection.

理想情况下我也想要侵蚀/扩张BG均值以消除1像素噪声。

Ideally I also want to erode/dilate the BG mean so as to eleminate 1 pixel noise.

代码全部都是如此,所以你在开始时有多个帧(BGsample)来收集FG检测开始前的BG数据。唯一的依赖项是opencv2(> 2.3.1)和numpy(应该包含在> opencv 2.3.1中)

The code is all togethr so you have a number of frames at the start (BGsample) to gather the BG data before FG detection starts. the only dependencies are opencv2 (> 2.3.1 ) and numpy (which should be included in > opencv 2.3.1 )

import cv2
import numpy as np


if __name__ == '__main__': 
    cap = cv2.VideoCapture(0) # webcam
    cv2.namedWindow("input")
    cv2.namedWindow("sig2")
    cv2.namedWindow("detect")
    BGsample = 20 # number of frames to gather BG samples from at start of capture
    success, img = cap.read()
    width = cap.get(3)
    height = cap.get(4)
    # can use img.shape(:-1) # cut off extra channels
    if success:
        acc = np.zeros((height, width), np.float32) # 32 bit accumulator
        sqacc = np.zeros((height, width), np.float32) # 32 bit accumulator
        for i in range(20): a = cap.read() # dummy to warm up sensor
        # gather BG samples
        for i in range(BGsample):
            success, img = cap.read()
            frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            cv2.accumulate(frame, acc)
            cv2.accumulateSquare(frame, sqacc)
        #
        M = acc/float(BGsample)
        sqaccM = sqacc/float(BGsample)
        M2 = M*M
        sig2 = sqaccM-M2
        # have BG samples now
        # start FG detection
        key = -1
        while(key < 0):
            success, img = cap.read()
            frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            #Ideally we create a mask for future use that is B/W for FG objects
            # (using erode or dilate to remove noise)
            # this isn't quite right
            level = M+sig2-frame
            grey = cv2.morphologyEx(level, cv2.MORPH_DILATE,
                                    cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3)), iterations=2)
            cv2.imshow("input", frame)
            cv2.imshow("sig2", sig2/60)
            cv2.imshow("detect", grey/20)
            key = cv2.waitKey(1)
    cv2.destroyAllWindows()


推荐答案

我认为您不需要手动计算均值和标准差使用 cv2.meanStdDev 。在下面的代码中,我使用的是你的平均背景矩阵

I don't think you need to manually compute the mean and standard deviation use cv2.meanStdDev instead. In the code below, I'm using your average background matrix computed from

M = acc/float(BGsample) 

所以,现在我们可以计算平均背景图像的均值和标准差,最后 inRange 用于提取您想要的范围(即平均值+/- 1标准偏差)。

So, now we can compute the mean and standard deviation of the average background image, and finally inRange is used to pull out the range that you wanted (i.e., the mean +/- 1 standard deviation).

(mu, sigma) = cv2.meanStdDev(M)
fg = cv2.inRange(M, (mu[0] - sigma[0]), (mu[0] + sigma[0]))
# proceed with morphological clean-up here...

希望帮助!

这篇关于opencv2中的背景减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆