I am using Enthought Canopy as my IDE(python with opencv-masters), i want to do Background Subtraction, but the problem is, the webcam(window which starts with webcam) is not responding if I run the code. My code is:
import cv2
import numpy as np
cam=cv2.VideoCapture(0)
fgbg = cv2.BackgroundSubtractorMOG()
while(cam.isOpened):
f,img=cam.read()
if f==True:
#img=cv2.flip(img,1)
#img=cv2.medianBlur(img,3)
fgmask = fgbg.apply(img)
cv2.imshow('track',fgmask)
if(cv2.waitKey(27)!=-1):
cam.release()
cv2.destroyAllWindows()
#break
I have no idea whether I can achieve this Background Subtraction in any other ways.
Thanks!
if f==True
and if(cv2.waitKey(27)!=-1)
should be inside the while
loop, then it works fine
import cv2
import numpy as np
cam=cv2.VideoCapture(0)
fgbg = cv2.BackgroundSubtractorMOG()
while(cam.isOpened):
f,img=cam.read()
if f==True:
#img=cv2.flip(img,1)
#img=cv2.medianBlur(img,3)
fgmask = fgbg.apply(img)
cv2.imshow('track',fgmask)
if(cv2.waitKey(27)!=-1):
cam.release()
cv2.destroyAllWindows()
#break