如何调整视频帧的大小并获取最终大小?

3

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

我尝试了以下代码:

while True:

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/中的一行代码?我该如何进行必要的操作?

2个回答

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)

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


1
我认为您传递的frame变量不是一个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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接