67

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?

karavanjo
  • 1,626
  • 4
  • 18
  • 31

9 Answers9

109

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 `width`
    height = vcap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)  # float `height`
    # or
    width  = vcap.get(3)  # float `width`
    height = vcap.get(4)  # float `height`

    # 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: Currently cv2 uses little different names

cv2.CAP_PROP_FRAME_WIDTH   # 3
cv2.CAP_PROP_FRAME_HEIGHT  # 4

cv2.CAP_PROP_FPS           # 5
cv2.CAP_PROP_FRAME_COUNT   # 7

but they have the same values: 3, 4, 5, 7

import cv2

#vcap = cv2.VideoCapture(0)  # built-in webcamera

vcap = cv2.VideoCapture('video.avi')

if vcap.isOpened(): 
    width  = vcap.get(cv2.CAP_PROP_FRAME_WIDTH)   # float `width`
    height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float `height`
    # or
    width  = vcap.get(3)  # float `width`
    height = vcap.get(4)  # float `height`

    print('width, height:', width, height)
    
    fps = vcap.get(cv2.CAP_PROP_FPS)
    # or
    fps = vcap.get(5)
    
    print('fps:', fps)  # float `fps`
    
    frame_count = vcap.get(cv2.CAP_PROP_FRAME_COUNT)
    # or
    frame_count = vcap.get(7)
    
    print('frames count:', frame_count)  # float `frame_count`

    #print('cv2.CAP_PROP_FRAME_WIDTH :', cv2.CAP_PROP_FRAME_WIDTH)   # 3
    #print('cv2.CAP_PROP_FRAME_HEIGHT:', cv2.CAP_PROP_FRAME_HEIGHT)  # 4
    #print('cv2.CAP_PROP_FPS         :', cv2.CAP_PROP_FPS)           # 5
    #print('cv2.CAP_PROP_FRAME_COUNT :', cv2.CAP_PROP_FRAME_COUNT)   # 7

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

furas
  • 134,197
  • 12
  • 106
  • 148
  • 1
    It works fine for my files through CV_CAP_PROP_FRAME_WIDTH/CV_CAP_PROP_FRAME_HEIGHT, thank you. – karavanjo Oct 10 '16 at 09:11
  • @furas: Is fps 0 for just one video, or for many? If many, are videos from a single source(same camera)? – saurabheights Oct 10 '16 at 09:27
  • @saurabheights I checked and `vcap.get(cv2.cv.CV_CAP_PROP_FPS)` gives good results for `http stream` and `rtsp stream`, but not for file and camera. `vcap.get(7)` gives good result for file but not for camera and streams. `7` probably means `CV_CAP_PROP_FRAME_COUNT`. (doc: [VideoCapture::get](http://docs.opencv.org/2.4.13/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get)) – furas Oct 10 '16 at 09:37
  • @saurabheights I checked only one file - created from some stream using CV. – furas Oct 10 '16 at 09:40
  • @furas: Thanks for reply. Could you tell the opencv version you are using? "print cv2.__version__" - command for it. – saurabheights Oct 10 '16 at 09:46
  • @saurabheights CV2 2.4.8, Python 2.7.12, Linux Mint 17.3 (based on Ubuntu 14.04) – furas Oct 10 '16 at 10:03
  • @furas On Mac, CV: 3.0.0-dev, Py: 2.7.10. it seems to work for an mp4 I tested on, got FPS:- 23.8095238095. Should have been fixed upstream. Thank you for answer. :) – saurabheights Oct 10 '16 at 10:06
  • 7
    Note, the `cv2.cv.CV_*` namespace is now obsolute. Just reference `cv2.*` instead. – Cerin Mar 11 '18 at 01:47
  • @Gulzar you may prefer names instead of numbers in `vcap.get()` but I wanted to show all versions and I put back code which you removed – furas May 09 '20 at 16:32
  • 2
    @furas Please refer to https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad . As I see it, most of the people entering this page are not programmers by profession, but rather data scientists or other. Thus will not see the fault in this. Yes, writing like this works, but it leaves the code not readable. The names are there for a reason. – Gulzar May 09 '20 at 17:20
  • 1
    @furas Furthermore, `I wanted to show all versions` is not valid reasoning: you could add any arbitry number of `width = vcap.get(3+1-1+1-1)`. Still works, still "a version", obviously not good. I am not trying to be a prick about it, just want people to use the correct form, because no one is going to read our comments. – Gulzar May 09 '20 at 17:23
  • 1
    there is no real meaning to "`3`", and it isn't really an integer. It is an Enum, and should be treated as such. – Gulzar May 09 '20 at 17:27
  • in answer I have also "good version" before "bad version" so everyone can choose which version to use. – furas May 09 '20 at 17:40
  • 1
    @furas Sorry, I can't see why to let people copy paste a bad practice. I really would like yo know your thoughts about this. Do you think it is a good practice? Do you think there is value in it I don't see? Please explain, I really want to learn. – Gulzar May 09 '20 at 18:07
21
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)
GGEv
  • 843
  • 9
  • 23
10

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.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
3
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))
mrgloom
  • 20,061
  • 36
  • 171
  • 301
1

For OpenCV versions = 4.x.x:

    width  = vcap.get(cv2.CAP_PROP_FRAME_WIDTH)   
    height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT)  
YScharf
  • 1,638
  • 15
  • 20
1

for ret, frame = cv2.VideoCapture use frame.shape to get Width and Height.

height, width, channels = frame.shape
    print(width)
0

Though solved answers are here, in 2021, it got a little changed. (OpenCV version = 4.5.2)

import cv2
cap = cv2.VideoCapture('video.mp4') #0 for camera


if cap.isOpened(): 
    width  = cap.get(cv2.CAP_PROP_FRAME_WIDTH)   # float `width`
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float `height`
    fps = cap.get(cv2.CAP_PROP_FPS) # float `fps`
    total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT) # float `total_frame_in_the_video` (should not be applicable for camera)

Joyanta J. Mondal
  • 888
  • 1
  • 8
  • 20
-1

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

-1

You can get video width and height with opencv using below code.

import cv2

vidcap = cv2.VideoCapture('video.mp4')
frameWidth = vidcap.get(cv2.CAP_PROP_FRAME_WIDTH)
frameHeight = vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT)
afzalex
  • 8,598
  • 2
  • 34
  • 61