The Wayback Machine - https://web.archive.org/web/20240529014728/https://www.geeksforgeeks.org/how-to-get-properties-of-python-cv2-videocapture-object/
Open In App

How to get properties of Python cv2.VideoCapture object ?

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Let us see how we can get the properties from the cv2.VideoCapture objects and understand how they work. cv2.VideoCapture is a function of openCV library(used for computer vision, machine learning, and image processing) which allows working with video either by capturing via live webcam or by a video file. To know more about this function, refer to this link.  

Installation

Before we start, make sure to install the OpenCV library on Python 3.X. You can install these using the pip command:

pip install opencv-python

Properties of cv2.VideoCapture Object

By knowing the properties of the cv2.VideoCapture object facilitates to do Video Processing ultimately by doing processing on the frames. 

1) Width: This property is used to get the width of the frames in the video stream.  The measuring unit is in pixels. 

Syntax: cv2.CAP_PROP_FRAME_WIDTH

2) Height: This property is used to get the height of the frames in the video stream. The measuring unit is in pixels.

Syntax: cv2.CAP_PROP_FRAME_HEIGHT 

3) Fps: FPS stands for frames per second. This property is used to get the frame rate of the video.

Syntax: cv2.CAP_PROP_FPS

4) Current Position: This property is used to find what is the current position of the video, its measuring unit is milliseconds.

Syntax: cv2.CAP_PROP_POS_MSEC

5) Total number of frame: This property is used to calculate the total number of frames in the video file.

  Syntax: cv2.CAP_PROP_FRAME_COUNT

6) Brightness: This property is not for video files. It only works with a camera or webcam. Used to find out the brightness.

Syntax: cv2.CAP_PROP_BRIGHTNESS

7) Contrast: This property also works with the camera or webcam only. Used to find out the contrast on images captured.

Syntax: cv2.CAP_PROP_CONTRAST

8) Saturation Value: This is used to get the saturation of live frames capturing via cameras. This also doesn’t work on the video file.

Syntax: cv2.CAP_PROP_SATURATION

9) HUE Value: This is for knowing the HUE value of the image. Only for cameras.

Syntax: cv2.CAP_PROP_HUE

10) GAIN:  This property is used to get the gain of the image. Wouldn’t work with the video file, simply return “0” if applied on a video file.

Syntax: cv2.CAP_PROP_GAIN

11) Need to convert into RGB: This property returns a Boolean value which indicates whether the images should be converted to RGB colorspace or not.

Syntax: cv2.CAP_PROP_CONVERT_RGB

These properties will be much clear on using these directly on the code. Here’s its implementation:

Python3




# importing cv2
import cv2
  
#For Video File
#capture=cv2.VideoCapture("sample.webm")
  
#For webcam
capture=cv2.VideoCapture(0)
  
# showing values of the properties
print("CV_CAP_PROP_FRAME_WIDTH: '{}'".format(capture.get(cv2.CAP_PROP_FRAME_WIDTH)))
print("CV_CAP_PROP_FRAME_HEIGHT : '{}'".format(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print("CAP_PROP_FPS : '{}'".format(capture.get(cv2.CAP_PROP_FPS)))
print("CAP_PROP_POS_MSEC : '{}'".format(capture.get(cv2.CAP_PROP_POS_MSEC)))
print("CAP_PROP_FRAME_COUNT  : '{}'".format(capture.get(cv2.CAP_PROP_FRAME_COUNT)))
print("CAP_PROP_BRIGHTNESS : '{}'".format(capture.get(cv2.CAP_PROP_BRIGHTNESS)))
print("CAP_PROP_CONTRAST : '{}'".format(capture.get(cv2.CAP_PROP_CONTRAST)))
print("CAP_PROP_SATURATION : '{}'".format(capture.get(cv2.CAP_PROP_SATURATION)))
print("CAP_PROP_HUE : '{}'".format(capture.get(cv2.CAP_PROP_HUE)))
print("CAP_PROP_GAIN  : '{}'".format(capture.get(cv2.CAP_PROP_GAIN)))
print("CAP_PROP_CONVERT_RGB : '{}'".format(capture.get(cv2.CAP_PROP_CONVERT_RGB)))
  
# release window
capture.release()
cv2.destroyAllWindows()


Output is as follows (For webcam):



Similar Reads

Faster video file FPS with cv2.VideoCapture and OpenCV
Sometimes we need to speed up the FPS(Frame Per Second) of a video file to perform certain operations on the video such as surveillance footage analysis, video editing, and video game recording. In this article, we will learn to increase the FPS of our input video and download the resultant output video with the help of cv2.VideoCapture in Python.
4 min read
Python OpenCV | cv2.imshow() method
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.imshow() method is used to display an image in a window. The window automatically fits the image size. Syntax: cv2.imshow(window_name, image)Parameters: window_name: A string representing the name of the window in which image to be displayed. image: It is
2 min read
Python OpenCV | cv2.imwrite() method
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in current working directory. Syntax: cv2.imwrite(filename, image) Parameters: filename: A string representing the file name. The fi
1 min read
Python OpenCV | cv2.line() method
OpenCV-Python is a library of Python bindings designed to solve computer vision problems.cv2.line() method is used to draw a line on any image. Syntax: cv2.line(image, start_point, end_point, color, thickness) Parameters: image: It is the image on which line is to be drawn. start_point: It is the starting coordinates of the line. The coordinates ar
3 min read
Python OpenCV | cv2.rectangle() method
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.rectangle() method is used to draw a rectangle on any image. Syntax: cv2.rectangle(image, start_point, end_point, color, thickness) Parameters: image: It is the image on which rectangle is to be drawn. start_point: It is the starting coordinates of rectang
3 min read
Python OpenCV | cv2.circle() method
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.circle() method is used to draw a circle on any image. The syntax of cv2.circle() method is: Syntax: cv2.circle(image, center_coordinates, radius, color, thickness) Parameters: image: It is the image on which the circle is to be drawn. center_coordinates:
3 min read
Python OpenCV | cv2.putText() method
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.putText() method is used to draw a text string on any image. Syntax: cv2.putText(image, text, org, font, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) Parameters: image: It is the image on which text is to be drawn. text: Text string to be
3 min read
Python OpenCV | cv2.ellipse() method
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.ellipse() method is used to draw a ellipse on any image. Syntax: cv2.ellipse(image, centerCoordinates, axesLength, angle, startAngle, endAngle, color [, thickness[, lineType[, shift]]]) Parameters: image: It is the image on which ellipse is to be drawn. ce
3 min read
Python OpenCV | cv2.cvtColor() method
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.cvtColor() method is used to convert an image from one color space to another. There are more than 150 color-space conversion methods available in OpenCV. We will use some of color space conversion codes below. Syntax: cv2.cvtColor(src, code[, dst[, dstCn]
2 min read
Python OpenCV | cv2.copyMakeBorder() method
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.copyMakeBorder() method is used to create a border around the image like a photo frame. Syntax: cv2.copyMakeBorder(src, top, bottom, left, right, borderType, value) Parameters: src: It is the source image. top: It is the border width in number of pixels in
2 min read
Article Tags :
Practice Tags :