Get video dimension in python-opencv

后端 未结 5 1060
有刺的猬
有刺的猬 2021-01-07 16:35

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?

相关标签:
5条回答
  • 2021-01-07 16:43

    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

    Instead of cv2.cv.CV_CAP_PROP_FRAME_WIDTH use cv2.CAP_PROP_FRAME_WIDTH and others as necessary from the link above.

    0 讨论(0)
  • 2021-01-07 16:46
    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))
    
    0 讨论(0)
  • 2021-01-07 16:47

    You can use the vcap.get(i) method, where i can be between 0 and 21, according to the OpenCV docs.

    0 讨论(0)
  • 2021-01-07 17:01

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

    But it always gives me 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.


    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
    

    EDIT 2020: all properties in How do I get usb webcam property IDs for OpenCV

    0 讨论(0)
  • 2021-01-07 17:08
    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)
    
    0 讨论(0)
提交回复
热议问题