在python-opencv中获取视频尺寸 [英] Get video dimension in python-opencv

查看:2169
本文介绍了在python-opencv中获取视频尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以得到图像的大小,像这样:

I can get size of image, like this:

import cv2

img = cv2.imread('my_image.jpg',0)
height, width = img.shape[:2]

视频怎么样?

推荐答案

它将文件或相机的widthheight设为浮点型(因此您可能必须转换为整数)

It gives width and height of file or camera as float (so you may have to convert to integer)

但是它总是给我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)


似乎可以使用fps = vcap.get(7),但我仅在一个文件上进行了检查.


It seems it can works fps = vcap.get(7) but I checked this only on one file.

EDIT 2019 :当前的cv2使用的名称几乎没有什么不同(但它们具有相同的值:3、4、5、7)

EDIT 2019: Current cv2 uses little different names (but they have the same values: 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

这篇关于在python-opencv中获取视频尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆