Python: Subtract the Background Image from A video
作者:XD / 发表: 2023年2月28日 07:08 / 更新: 2023年2月28日 07:08 / 编程笔记 / 阅读量:1068
Python: Subtract the Background Image from A video.
import cv2
import numpy as np
def get_video_median(video_path, save_path):
# Open Video
cap = cv2.VideoCapture(video_path)
# Randomly select 100 frames
frame_num = 100
frameIds = cap.get(cv2.CAP_PROP_FRAME_COUNT) * np.random.uniform(size=frame_num)
# Store selected frames in an array
frames = []
for fid in frameIds:
cap.set(cv2.CAP_PROP_POS_FRAMES, fid)
ret, frame = cap.read()
frames.append(frame)
# Calculate the pixel median along the time axis
medianFrame = np.median(frames, axis=0).astype(dtype=np.uint8)
img_save = '{}/median.jpg'.format(save_path)
cv2.imwrite(img_save, medianFrame)
if __name__ == '__main__':
video_path = 'test.mp4'
save_path = '/data/video'
get_video_median(video_path, save_path)