Skip to content

Instantly share code, notes, and snippets.

@VieVie31
Created December 3, 2016 16:21
Show Gist options
  • Save VieVie31/94b81bb6fba767cb3bf48473ac89785d to your computer and use it in GitHub Desktop.
Save VieVie31/94b81bb6fba767cb3bf48473ac89785d to your computer and use it in GitHub Desktop.
Extract background (in grayscale) from video if the camera is not moving...
import cv2
import numpy as np
from skvideo.io import vreader
VIDEO_PATH = "room.mp4" #my room where i'm moving and i want to remove myself to get only the background...
cap = vreader(VIDEO_PATH)
L = list(cap)
L = map(lambda img: cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), L) #to gray scale
cv2.imshow("1st frame : ", L[0])
L = np.array(L)
#out will be the background image...
out = L[0]
#this can take a while... (depend of the size of frame & # fo frames)
for i in range(len(L[0])):
for j in range(len(L[0][0])):
out[i,j] = np.median(L[:,i,j]) #sorted(L[:,i,j])[len(L) // 2]
cv2.imshow("background", out) #this is the background... :D
cv2.imwrite("bg.png", out) #save the background as gray scale
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment