opencv|opencv-保存视频操作

#代码完成的功能:对已存在的视频进行处理之后进行保存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-保存视频操作】

    推荐阅读