[英]How to generate a mp4(not a avi) file from a number of frames using Python OpenCV?
[英]How to create array of frames from an mp4 with opencv for python
我正在尝试使用opencv_python将mp4文件分解为框架,以便以后可以用枕头打开它们,或者至少能够使用图像在它们上运行我自己的方法。
我了解以下代码段是从实况视频或录制的视频中获取帧。
import cv2
cap = cv2.VideoCapture("myfile.mp4")
boolean, frame = cap.read()
读取函数返回的确切信息是什么,以及如何创建可以修改的图像数组。
改编自《 如何使用Opencv python在视频流中逐帧处理视频图像》 。 未经测试。 但是,这些帧被读入numpy数组,并附加到一个列表,当所有帧都被读入时,该列表将转换为numpy数组。
import cv2
import numpy as np
images = []
cap = cv2.VideoCapture("./out.mp4")
while not cap.isOpened():
cap = cv2.VideoCapture("./out.mp4")
cv2.waitKey(1000)
print "Wait for the header"
pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
while True:
boolen, frame = cap.read() # get the frame
if flag:
# The frame is ready and already captured
# cv2.imshow('video', frame)
# store the current frame in as a numpy array
np_frame = cv2.imread('video', frame)
images.append(np_frame)
pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
else:
# The next frame is not ready, so we try to read it again
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1)
print "frame is not ready"
# It is better to wait for a while for the next frame to be ready
cv2.waitKey(1000)
if cv2.waitKey(10) == 27:
break
if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
# If the number of captured frames is equal to the total number of frames,
# we stop
break
all_frames = np.array(images)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.