如何创建多个VideoCapture对象

问题描述 投票:-1回答:1

我想创建多个VideoCapture对象,用于将来自多个摄像头的视频拼接到单个视频混搭。

例如:我有三个视频的路径,我想使用下面显示的视频捕获对象来读取,以从各个视频中获取帧,因此它们可用于写入。

预期:对于N个视频路径

   cap0=cv2.VideoCapture(path1)
   cap1=cv2.VideoCapture(path2)
   cap2=cv2.VideoCapture(path3)
   .
   . 
   capn=cv2.VideoCapture(path4)

同样我也想创建框架对象来读取框架

ret,frame0=cap0.read()
ret,frame1=cap1.read()
.
.
ret,frameN=capn.read()

我尝试在存储路径的列表中使用for循环但是每次只读取一个路径并且仅为该特定视频存储帧。我在许多论坛中看到可以在C ++中创建多个捕获对象但不在动态场景中的python,其中手头的视频数量不详。这是我的代码,直到现在

frames=[]
for path in videoList:
    indices=[]
    cap = cv2.VideoCapture(path)

    while(cap.isOpened()):
        ret,frame=cap.read()
        if not ret:
           break
        indices.append(cap.get(1))
    frames.append(indices)
    cap.release()
    cv2.destroyAllWindows()
python opencv video video-capture video-processing
1个回答
1
投票

我不是python程序员,但可能解决方案是这样的:

frames = []
caps = []
for path in videoList:
    caps.append(cv2.VideoCapture(path))

for cap in caps:
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        frames.append(frame)

# now "frames" holds your captured images.
© www.soinside.com 2019 - 2024. All rights reserved.