VideoCapture()读取多个视频和帧分辨率问题

2024-06-11 03:23:07 发布

您现在位置:Python中文网/ 问答频道 /正文

根据这个article的答案,它指的是将单个图像组合成4面。从那里,我想从只使用一个视频到使用4个视频作为输入。在

这是我用单个视频作为输入的代码

import cv2
import numpy as np


def make4side(image, scale=0.5):

   # image = cv2.imread(image)
   h = int((scale*image.shape[0])) #height
   w = int((scale*image.shape[1])) #width
   image = cv2.resize(image, (w,h ), interpolation = cv2.INTER_AREA) #shrink image to half

   output = np.zeros((w+h+h , w + h + h, 3), dtype="uint8")

   # top 
   output[0:h, h:h+w] = image 
   # left >> rotate 90
   output[h:h+w, 0:h] = np.rot90(image,1) 
   # right >> rotate 270
   output[h:h + w, h + w:h +w +h] = np.rot90(image,3)  
   # bottom >> rotate 180
   output[h+w:h+w+h, h:h+w] = np.rot90(image,2) 

   return output
   #cv2.imwrite('test.jpg', output)

def process(video):
   cap = cv2.VideoCapture(video)
   fourcc = cv2.VideoWriter_fourcc(*'XVID')
   holo = None
   ret = False
   while(not ret):
    ret, frame = cap.read()
    if ret:
        frame = cv2.resize(frame, (640, 480), interpolation = cv2.INTER_AREA)
        holo = make4side(frame)
   out = cv2.VideoWriter('hologram640x480.avi',fourcc, 23.98, (holo.shape[0],holo.shape[1]))
   total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
   count = 0
   print("Processing %d frames"%(total_frames))
   while(True):
       # Capture frame-by-frame
       ret, frame = cap.read()
       if ret:
           frame = cv2.resize(frame, (640, 480), interpolation = cv2.INTER_AREA)
           holo = make4side(frame)
           out.write(holo)
           count += 1
           print("Total:%d of %d"%(count,total_frames))
       if(count>=total_frames-1):
           break

   cap.release()
   out.release()
   return

process('g.mov')

结果是这样的。在

4 sided video from single video

在这段代码中,整个帧的高度和宽度都是基于输入的,这也是我所关注的单个视频,因为我使用了4个视频,当然帧分辨率不一样(但都是横向的)。函数make4side()中的变量h和w是帮助定位每个小框架的主要部分。那么对于这种情况,大帧(可变输出)的分辨率应该是多少?在

我必须读4个视频并将其写入一个,那么,我如何使用VideoCapture对象来实现这一目的呢

为了让我的问题更清楚,我想有一个单独的视频,由4个输入视频组成,每个视频将被放置在每个位置(顶部、底部、左侧和右侧)。我有一个大帧分辨率的问题,我不知道使用什么,如果我有4个视频,而不是一个。另一个问题是关于VideoCapture对象。我怎样才能同时读取所有视频的帧,或者以其他方式读取?在

谢谢你

编辑: top 顶部

left 左侧

back 背面

right 右侧

这些不是我将要使用的真正的帧,但只是一个简单的想法,我将使用我的视频。另外,输入文件的分辨率可能不同。如何使用多个videocapture对象来读取每个对象并将其放置在大帧的每一侧以编写单个视频


Tags: imageoutputframes视频countnp分辨率cv2
1条回答
网友
1楼 · 发布于 2024-06-11 03:23:07

所以一切都取决于你想做什么,所以这取决于你要处理的图像类型。首先,您可以始终有4个VideoCapture类的实例,每个实例都加载一个新的视频,如下所示:

videoTop = cv2.VideoCapture(videoTopFileName)
videoLeft = cv2.VideoCapture(videoLeftFileName)
videoRight = cv2.VideoCapture(videoRightFileName)
videoBottom = cv2.VideoCapture(videoBottomFileName)

readCorrect = True
while( readCorrect ):
  readCorrect , topFrame = videoTop.read()
  ret, leftFrame = videoLeft.read()
  readCorrect = readCorrect and ret
  ret, rightFrame = videoRight.read()
  readCorrect  = readCorrect and ret
  ret, bottomFrame = videoBottom.read()
  readCorrect = readCorrect and ret
  if readCorrect :
     holo = make4side(topFrame, leftFrame, rightFrame, bottomFrame )

你可以在这个循环中把你的图像保存在录像机里。在

现在是棘手的部分,你的图像大小不一样。。。你可以这样做:

^{pr2}$

但这会产生这样的“坏”形象:

enter image description here

为了使其不失真,您应该找到纵横比并尝试调整它们的大小,使之类似。如果纵横比不同,那么您将不得不填充图像。这部分取决于您的任务,您可以裁剪图像或填充它。在

但基本上这就是我们应该做的。我希望这对你有帮助。在


更新:

为了澄清回路部分:

^{3}$

在我分配给readCorrect变量的第一行中,read的布尔返回值。然后在下一幅图像中,我分配给ret,并用前面的结果做一个逻辑and。这样你就能知道他们是真是假。在

我还更正了循环中有错误的内容(我put while not readCorrect,它应该没有not)。在

在循环之前还有一件事需要创建VideoWriter对象,在使用^{}参数CV_CAP_PROP_FRAME_WIDTH和{}读取之前,您可以获得每个视频的大小,比如videoTop.get(CV_CAP_PROP_FRAME_WIDTH)。在

然后在循环中,特别是在获得图像后的if中,你可以将其写入。在

相关问题 更多 >