#代码完成的功能:对已存在的视频进行处理之后进行保存import cv2video = cv2.VideoCapture("v_CricketShot_g04_c01.avi")# 打开视频fourcc = cv2.VideoWriter_fourcc(*'MP4V')# 视频编解码器
fps = video.get(cv2.CAP_PROP_FPS)# 帧数
width, height = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))# 宽高
out = cv2.VideoWriter('save_opening.avi', fourcc, fps, (width, height))# 写入视频bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)
es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))while video.isOpened():
ret, frame = video.read()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#gray = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
fgmask = bs.apply(gray)
KNN_medianBlur = cv2.medianBlur(fgmask, 5)# 中值滤波
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4))# 创建矩形结构单元
opening = cv2.morphologyEx(KNN_medianBlur, cv2.MORPH_OPEN, kernel)# 形态学处理,开运算
opening = cv2.cvtColor(opening, cv2.COLOR_GRAY2RGB)#不加这一句会报错,因为这一句导致了一天的bug
out.write(opening)# 写入帧
cv2.imshow('opening', opening)
if cv2.waitKey(1) & 0xFF == ord('q'):# q退出
break
else:
breakvideo.release()
out.release()
cv2.destroyAllWindows()
#保存视频通用操作import cv2video = cv2.VideoCapture("v_CricketShot_g04_c01.avi")# 打开视频fourcc = cv2.VideoWriter_fourcc(*'MP4V')# 视频编解码器
fps = video.get(cv2.CAP_PROP_FPS)# 帧数
width, height = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))# 宽高
out = cv2.VideoWriter('save_video.avi', fourcc, fps, (width, height))# 写入视频while video.isOpened():
ret, frame = video.read()
if ret == True:
out.write(frame)# 写入帧
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):# q退出
break
else:
breakvideo.release()
out.release()
cv2.destroyAllWindows()
【opencv|opencv-保存视频操作】
推荐阅读
- openCV视频操作(C++版本)
- 深度学习|基于opencv的人脸检测
- GAN学习笔记|生成式对抗网络(GANs)及变体
- 论文笔记|[论文笔记] YOLO5Face 阅读笔记
- c++|基于C++的OpenCV(八)图像处理
- SLAM|【SLAM】6非线性优化
- 计算机视觉/CV|CV(计算机视觉)(概述)
- python|stn在mnist上的实现
- FFMPEG音视频系列文章|QT软件开发-基于FFMPEG设计视频播放器-支持软解与硬解-完整例子(六)