python - 发布 BG 减法(使用 opencv-python)并保存,输出视频为 2 GB。如何确保它不爆炸尺寸?
问题描述
# 下面这段代码是读取一个叫做v1.mp4(大小约14MB)的视频文件,对其进行背景减法,旋转帧并保存。
import cv2
import argparse
import datetime
import imutils
import time
import numpy as np
cap = cv2.VideoCapture("C:\\Users\\212458792\\Music\\Downloads\\BasketVideos\\v1.mp4") #Open video file
fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows = True) #Create the background substractor
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# now creating video writer object
bgsubvideo = cv2.VideoWriter('C:\\Users\\212458792\\Music\\Downloads\\BasketVideos\\bgsubvideo.avi', -1, 20.0,size)
#using any other codec like divx or xvid or mpeg for fourcc did create and video #file and write it, but i couldn't play the file. hence using -1
逐帧执行背景减法
while True:
ret, frame = cap.read()#read a frame
if ret==True:
fgmask = fgbg.apply(frame) #Use the substractor
cv.Flip(fgmask, flipMode=-1)
bgsubvideo.write(fgmask)
else:
break
cap.release() #release video file
cv2.destroyAllWindows() #close all openCV windows
bgsubvideo.release()
解决方案
推荐阅读
- stack - 性能分析显示错误的调用图
- java - 如何在 Spring 中使用 HttpRequestInterceptor?
- bash - 用于从存储在配置文件中的类似 JSON 的格式中获取具有特定键的结构的脚本
- vba - 创建具有相同字母的序列号
- java - Tibco 的 Appdynamics 工具 - 如何为每个 JVM 动态配置唯一节点名称
- sql - 将未定义设置为 BIT 值
- javascript - 匹配带有前导星号或尾随星号的行的正则表达式
- c# - C# Null 合并运算符在 Null 和空集合之间与 OR 运算符结合时表现不同
- python - 带有判别器编译问题的 VAE
- jquery - 提交表单外的div内容