Sunday, November 17, 2024
Google search engine
HomeLanguagesHow to get properties of Python cv2.VideoCapture object ?

How to get properties of Python cv2.VideoCapture object ?

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):

RELATED ARTICLES

Most Popular

Recent Comments