Ask Your Question
1

backgroundsubtractormog with python

asked 2013-07-28 06:39:21 -0600

VoidMee gravatar image

updated 2013-07-28 07:00:25 -0600

berak gravatar image

Hello, I want to use backgroundsubtractormog with python so my code is this

import cv2
import numpy as np

winname = "GRS"

bgs_mog = cv2.BackgroundSubtractorMOG(500, 6, 0.9, 1)

capture = cv2.VideoCapture(0)

frame = capture.read()[1]

if __name__ == "__main__":
    while frame != None:
        #fgmask = bgs_mog.apply(frame)
        cv2.imshow(winname, frame)
        c = cv2.waitKey(1)
        if c == 27:
            cv2.destroyWindow(winname)
            break
        frame = capture.read()[1]
    cv2.destroyAllWindows()

The output I am having is some mask of binary images. But I want only coloured image of moving objects. I do not know how to implement this mask to get the coloured image of moving objects only. I have referred the opencv2refman doc about this function but I am no getting clear about how to determine the parameters for mog constructor. Can somebody explain how to do this, Plz. Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-07-28 07:23:45 -0600

berak gravatar image

updated 2013-07-28 07:48:23 -0600

so, you get the fg mask.

fgmask = bgs_mog.apply(frame)

now this is an 8bit 1channel mat. to apply it to the frame (rgb) image, we need to convert the mask to rbg, too:

mask_rbg = cv2.cvtColor(fgmask,cv2.COLOR_GRAY2BGR)

then we can "and" it into a drawing image:

draw = frame & mask_rbg

the whole thing, again:

import cv2
import numpy as np

winname = "GRS"
bgs_mog = cv2.BackgroundSubtractorMOG(500, 6, 0.9, .1)
capture = cv2.VideoCapture(0)

if __name__ == "__main__":
    while capture.isOpened():
        _,frame = capture.read()
        fgmask = bgs_mog.apply(frame)
        mask_rbg = cv2.cvtColor(fgmask,cv2.COLOR_GRAY2BGR)
        draw = frame & mask_rbg
        cv2.imshow(winname, draw)
        c = cv2.waitKey(1)
        if c == 27:
            break
    cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

thanx, really appreciated your help. what a silly thing I did not know. :)

VoidMee gravatar imageVoidMee ( 2013-07-28 07:37:24 -0600 )edit

nice structured answer :) we should really consider a code snippet area with stuff like this :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-07-28 10:28:36 -0600 )edit

Question Tools

Stats

Asked: 2013-07-28 06:39:21 -0600

Seen: 5,415 times

Last updated: Jul 28 '13