在 python-opencv 中获取视频尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39953263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get video dimension in python-opencv
提问by karavanjo
I can get size of image, like this:
我可以获得图像的大小,如下所示:
import cv2
img = cv2.imread('my_image.jpg',0)
height, width = img.shape[:2]
How about video?
视频呢?
回答by furas
It gives width
and height
of file or camera as float (so you may have to convert to integer)
它提供了width
和height
文件或相机作为浮动的(所以你可能要转换为整数)
But it always gives me 0.0 FPS
.
但它总是给我0.0 FPS
。
import cv2
vcap = cv2.VideoCapture('video.avi') # 0=camera
if vcap.isOpened():
# get vcap property
width = vcap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH) # float
height = vcap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT) # float
# or
width = vcap.get(3) # float
height = vcap.get(4) # float
# it gives me 0.0 :/
fps = vcap.get(cv2.cv.CV_CAP_PROP_FPS)
It seems it can works fps = vcap.get(7)
but I checked this only on one file.
看起来它可以工作,fps = vcap.get(7)
但我只检查了一个文件。
EDIT 2019:Current cv2 uses little different names (but they have the same values: 3, 4, 5, 7)
编辑 2019:当前 cv2 使用的名称略有不同(但它们具有相同的值:3、4、5、7)
import cv2
vcap = cv2.VideoCapture('video.avi') # 0=camera
if vcap.isOpened():
width = vcap.get(cv2.CAP_PROP_FRAME_WIDTH) # float
height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT) # float
#print(cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT) # 3, 4
# or
width = vcap.get(3) # float
height = vcap.get(4) # float
print('width, height:', width, height)
fps = vcap.get(cv2.CAP_PROP_FPS)
print('fps:', fps) # float
#print(cv2.CAP_PROP_FPS) # 5
frame_count = vcap.get(cv2.CAP_PROP_FRAME_COUNT)
print('frames count:', frame_count) # float
#print(cv2.CAP_PROP_FRAME_COUNT) # 7
回答by GGEv
width = vcap.get(cv2.CAP_PROP_FRAME_WIDTH )
height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT )
fps = vcap.get(cv2.CAP_PROP_FPS)
or
或者
width = vcap.get(3)
height = vcap.get(4)
fps = vcap.get(5)
回答by Nikhil Kasukurthi
For the 3.3.1 version, the methods have changed. Check this link for the changes: https://docs.opencv.org/3.3.1/d4/d15/group__videoio__flags__base.html#ga023786be1ee68a9105bf2e48c700294d
对于 3.3.1 版本,方法已更改。检查此链接以了解更改:https: //docs.opencv.org/3.3.1/d4/d15/group__videoio__flags__base.html#ga023786be1ee68a9105bf2e48c700294d
Instead of cv2.cv.CV_CAP_PROP_FRAME_WIDTH
use cv2.CAP_PROP_FRAME_WIDTH
and others as necessary from the link above.
而不是从上面的链接中根据需要cv2.cv.CV_CAP_PROP_FRAME_WIDTH
使用cv2.CAP_PROP_FRAME_WIDTH
和其他。
回答by mrgloom
cv2.__version__
'3.4.3'
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))