Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python opencv background subtraction

Tags:

python

opencv

What I need to do is fairly simple:

  1. load a 5 frames video file
  2. detect background
  3. On every frames, one by one :
    1. subtract background (create foreground mask)
    2. do some calculations on foreground mask
    3. save both original frame and foreground mask

Just to see the 5 frames and the 5 corresponding fgmasks :

import numpy as np
import cv2  
cap = cv2.VideoCapture('test.avi')
fgbg = cv2.BackgroundSubtractorMOG()

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    fgmask = fgbg.apply(frame)
    # Display the fgmask frame
    cv2.imshow('fgmask',fgmask)
    # Display original frame
    cv2.imshow('img', frame)

    k = cv2.waitKey(0) & 0xff
    if k == 5:
        break

cap.release()
cv2.destroyAllWindows()

Every frame gets opened and displayed correctly but the showed fgmask do not correspond to the showed original frame. Somewhere in the process, the order of the fgmasks gets mixed up.

The background does get subtracted correctly but I don't get the 5 expected fgmasks.

What am I missing ? I feel like this should be straightforward : the while loop runs over the 5 frames of the video and fgbg.apply apply the background subtraction function to each frame.

OpenCV version that I use is opencv-2.4.9-3

like image 943
Chargaff Avatar asked Aug 05 '26 05:08

Chargaff


1 Answers

As bikz05 suggested, running average method worked pretty good on my 5 images sets. Thanks for the tip !

import cv2
import numpy as np

c = cv2.VideoCapture('test.avi')
_,f = c.read()

avg1 = np.float32(f)
avg2 = np.float32(f)

# loop over images and estimate background 
for x in range(0,4):
    _,f = c.read()

    cv2.accumulateWeighted(f,avg1,1)
    cv2.accumulateWeighted(f,avg2,0.01)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)

    cv2.imshow('img',f)
    cv2.imshow('avg1',res1)
    cv2.imshow('avg2',res2)
    k = cv2.waitKey(0) & 0xff
    if k == 5:
        break
like image 54
Chargaff Avatar answered Aug 07 '26 19:08

Chargaff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!