【发布时间】:2012-04-02 12:46:30
【问题描述】:
我正在尝试通过删除静态(大部分)BG 元素来使用 opencv2 检测前景运动。我使用的方法是基于一系列图像的平均值 - 代表背景。然后计算高于和低于该平均值的一个标准差。将其用作检测前景运动的窗口。
据报道,这种机制适用于中等噪声环境,例如 BG 中挥动的树木。
所需的输出是可以在后续操作中使用的掩码,以尽量减少进一步的处理。具体来说,我将在该区域内使用光流检测。
cv2 使这变得更容易,代码更易于阅读和理解。感谢 cv2 和 numpy。
但我在进行正确的 FG 检测时遇到了困难。
理想情况下,我还想侵蚀/扩大 BG 均值以消除 1 个像素的噪声。
代码全部在一起,因此在开始 FG 检测之前,您有许多帧 (BGsample) 来收集 BG 数据。唯一的依赖是 opencv2 (> 2.3.1 ) 和 numpy (应该包含在 > 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()
【问题讨论】:
-
看起来它与 thsi c++ 答案有关:stackoverflow.com/questions/7958786/…
-
很难理解你在这里问什么。您是否试图将图像的背景与前景分开?如果是这种情况,请使其更清晰:“我想将图像的背景与其前景分开。为此,我将值在 1 std.dev 之内的每个像素标记为来自图像的平均灰度为 bakground 。”
-
我已经按照您的建议重新提出了这个问题,谢谢。
标签: image-processing opencv background-subtraction