Skip to content

Instantly share code, notes, and snippets.

@non117
Created January 2, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save non117/9ac93d4942bcfa998a7f to your computer and use it in GitHub Desktop.
Save non117/9ac93d4942bcfa998a7f to your computer and use it in GitHub Desktop.
cvで背景差分
# -*- coding: utf-8 -*-
import cv2
def main():
cap = cv2.VideoCapture(0)
#cap = cv2.VideoCapture('opatan.mp4')
ret, frame = cap.read()
if not ret:
return
width = len(frame[0])
height = len(frame)
fourcc = cv2.cv.CV_FOURCC('m','p','4','v')
out = cv2.VideoWriter('output.mp4', fourcc, 10, (width, height), True)
fgbg = cv2.BackgroundSubtractorMOG2()
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = fgbg.apply(frame)
#out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(10) > 0:
break
cap.release()
out.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment