background subtraction not show anything

asked 2014-07-12 09:15:28 -0500

Shiloh gravatar image

updated 2018-02-25 02:34:46 -0500

I want to try the background subtraction from opencv python tutorial, using python 2.7 and opencv 2.4.. This is the code:

import numpy as np
import cv2

cap = cv2.VideoCapture("c:\\Users\Shiloh\Downloads\Test1.avi")

fgbg = cv2.createBackgroundSubtractorMOG()

    ret, frame =

    fgmask = fgbg.apply(frame)

    k = cv2.waitKey(30) & 0xff
    if k == 27:


It showing error "AttributeError: 'module' object has no attribute 'createBackgroundSubtractorMOG'"

I change

 fgbg = cv2.createBackgroundSubtractorMOG()


 fgbg = cv2.BackgroundSubtractorMOG()

No error, but I just get empty window like this: image description

I already tried play video using cv.CaptureFromFile it's work fine in mp4, avi, and flv... Can you tell me what's wrong with it?

edit retag flag offensive close merge delete



cv2.createBackgroundSubtractorMOG() is for opencv3.0,

for opencv 2-4 use: cv2.BackgroundSubtractorMOG()

berak gravatar imageberak ( 2014-07-12 11:20:46 -0500 )edit

didn't you see all my code?? I already tried it, no error, but I only get empty window..

Shiloh gravatar imageShiloh ( 2014-07-13 11:57:28 -0500 )edit

@Shiloh, you are using guides from Python, which are made for interface 3.0.0 which is still under development BUT you are using openCV 2.4... as backend. This won't simply work. Probably your sample still has some 3.0 includes or definitions!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-07-14 06:13:22 -0500 )edit

@StevenPuttemans so how did I know, if the command I try have command for opencv 2.x or opencv 3.x, I bought a book, it's used opencv 2.4 same as mine but the code didn't work too.. There is no explicit reference so how I know it??

Shiloh gravatar imageShiloh ( 2014-07-14 11:51:42 -0500 )edit

I'm sorry, I find the documentation of 2.4.9. still trying, and thanks for help me

Shiloh gravatar imageShiloh ( 2014-07-16 06:00:05 -0500 )edit