我试图使用opencv_python将mp4文件分解为框架,以便以后可以用枕头打开它们,或者至少能够使用图像在它们上运行我自己的方法。
我了解以下代码段是从实时视频或录制的视频中获取帧。
import cv2
cap = cv2.VideoCapture("myfile.mp4")
boolean, frame = cap.read()
读取函数返回的确切信息是什么,如何创建可以修改的图像数组。
最佳答案
改编自How to process images of a video, frame by frame in video streaming using 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)
关于python - 如何使用OpenCV从mp4从mp4创建帧数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41253939/