I'm trying to use the default functions to do background subtraction of a video file.
I'm using Python 2.7 and OpenCV.
I receive this error when using the 'BackgroundSubtractorMOG' module:
'module' object has no attribute 'BackgroundSubtractorMOG'
Now, if I try using the createBackgroundSubtractorMOG() module, I receive the same error.
If I change my code to:
bkgnd = cv2.bgsegm.BackgroundSubtractorMOG()
I get an error later on that tries to tell me I'm using OpenCV 3.1 (although I'm fairly certain I'm not).
img_sub_gray_image = cv2.cvtColor(img_sub, cv2.COLOR_BGR2GRAY) cv2.error:
/home/odroid/opencv-3.1.0/modules/imgproc/src/color.cpp:8000: error: (-215) scn == 3 || scn == 4 in function cvtColor
Here is the segment of code that is erroring out:
bkgnd = cv2.bgsegm.BackgroundSubtractorMOG()
cap = cv2.VideoCapture('video.mp4')
while(True):
ret, frame = cap.read()
#background subtraction
img_sub = bkgnd.apply(frame)
#convert to grayscale
img_sub_gray_image = cv2.cvtColor(img_sub, cv2.COLOR_BGR2GRAY)
#thresholding, forcing to binary image
ret,threshold1 = cv2.threshold(img_sub_gray_image, LOWER_BOUND, UPPER_BOUND, cv2.THRESH_BINARY)
Any ideas?
In opencv 3 they have changed some names of the functions, you must change:
bkgnd = cv2.bgsegm.BackgroundSubtractorMOG()
to:
cv2.bgsegm.createBackgroundSubtractorMOG()
Another observation is that the result of the apply function is a binary image, so it is not necessary to do the conversion from RGB
to gray
. You do not need to use the command: cv2.cvtColor(img_sub, cv2.COLOR_BGR2GRAY)