如何从视频调整帧大小,然后检索最终大小?

std*_*err 4 python video opencv image-resizing cv2

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

我试过这个:

为真:

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)
Run Code Online (Sandbox Code Playgroud)

但我得到:

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'
Run Code Online (Sandbox Code Playgroud)

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

Moh*_*ani 5

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)
Run Code Online (Sandbox Code Playgroud)

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