我编译了这段代码,用于保存从网络摄像头捕获的视频,采用灰色格式。视频最后保存但不在任何视频播放器上播放。请帮助..
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(gray)
cv2.imshow('frame',gray)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
答案 0 :(得分:1)
您正尝试以灰度格式保存视频。为此,您需要将其他参数传递给cv2.VideoWriter
,如下所示
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480), 0)
最后一个参数是0
,因为您没有保存彩色帧。希望这能解决您的问题