【问题标题】:how to resize frame from video and then retrieve the final size?如何从视频中调整帧大小,然后检索最终大小?
【发布时间】:2019-06-07 19:53:21
【问题描述】:

我想读取一个视频文件,将其分成单独的帧,将每个帧的大小调整为最大宽度,然后检索最终图像的宽度和高度。

我试过了:

当真时:

vs = cv2.VideoCapture(args["video"])
# grab the frame from the threaded video stream and resize it
# to have a maximum width of 400 pixels
frame = vs.read()
frame = imutils.resize(frame, width=400)

# grab the frame dimensions and convert it to a blob
w, h = cv.GetSize(frame)

但我得到了:

Traceback (most recent call last):
  File "real_time_object_detection.py", line 52, in <module>
    frame = imutils.resize(frame, width=400)
  File "/home/pi/.virtualenvs/cv/lib/python3.5/site-packages/imutils/convenience.py", line 69, in resize
    (h, w) = image.shape[:2]
AttributeError: 'tuple' object has no attribute 'shape'

为什么它抱怨imutils/ 中的一行?我该怎么做?

【问题讨论】:

    标签: python opencv video image-resizing cv2


    【解决方案1】:

    read 方法返回两个变量,第一个是成功变量,它是一个布尔值(如果捕获帧则为 True,否则为 False),第二个是帧。您可能正在阅读具有 3 个通道帧的视频,并且该帧通常是一个 numpy 数组,因此您可以使用 shape 属性。

    我建议使用cv2.resize 调整大小。

    vs = cv2.VideoCapture(args["video"])
    # grab the frame from the threaded video stream and resize it
    # to have a maximum width of 400 pixels
    
    _, frame = vs.read()
    (w, h, c) = frame.shape
    
    #syntax: cv2.resize(img, (width, height))
    img = cv2.resize(frame,(400, h))
    
    print(w, h)
    print(img.shape)
    >> 480 640
     (640, 400, 3) #rows(height), columns(width), channels(BGR)
    

    wh 存储视频帧的原始宽度和高度,img.shape 具有调整后的宽度和高度

    【讨论】:

      【解决方案2】:

      我认为你传递的框架变量不是一个 numpy 数组,它是一个元组。因此错误。检查视频是否被正确读取。执行print(type(frame)) 并检查它是否是 numpy 以验证您的图像是否被正确读取。 imutils.resize() 是一个使用 cv2.resize 函数的类。这就是它的工作原理

      vs = cv2.VideoCapture(args["video"])
      # grab the frame from the threaded video stream and resize it
      # to have a maximum width of 400 pixels
      ret, frame = vs.read()
      
      
      #inside of imutils.resize()
      w,h,c=frame.shape
      r = 400 / float(w)
      dim = (400, int(h * r))
      new_frame=cv2.resize(image,dim)
      

      【讨论】:

        猜你喜欢
        • 2011-08-20
        • 2015-07-03
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 2017-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多