我试图使用OpenCV的videoccapture函数读取视频,提取帧,然后将它们放入数组中,这样我就有了一个3D图像堆栈。以下是目前为止的内容:
cap = cv2.VideoCapture('/home/jryan/Videos/RMS/20211019_234125-75mm.mkv')
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = cap.get(cv2.CAP_PROP_FPS)
image_array = []
for frame in cap:
if frame_count <= 1000: # only want the first 1000 frames
continue
capture = cap.read()
image_array += capture
然而,我得到错误'cv2.VideoCapture' object is not iterable
。据我所知,没有什么可以从视频捕获中迭代-否则我如何引用单个帧?
您只需在for
循环中迭代int
变量,而不使用cap
,这是不可迭代的。例如:
for i in range(1000): #only the first 1000 frames
ret, frame = cap.read()
#do your stuff