Sub topic 7
Reading and Setting Capture Properties
Assume you know basic Python loops, try/finally, and NumPy array shapes. This chapter also assumes that opencv-python is installed and that a video is a sequence of frames.
What capture properties describe
cv2.VideoCapture stores information about an input source. A property is a value such as frame width, frame height, frame rate, or the current frame position.
Use cap.get(property_id) to read a property. Use cap.set(property_id, value) to request a new value. OpenCV represents property identifiers with constants such as:
| Property | Meaning | Typical use |
|---|---|---|
| `CAP_PROP_FRAME_WIDTH` | Frame width in pixels | Check image size |
| `CAP_PROP_FRAME_HEIGHT` | Frame height in pixels | Check image size |
| `CAP_PROP_FPS` | Frames per second | Estimate playback rate |
| `CAP_PROP_FRAME_COUNT` | Total frames in a file | Estimate duration |
| `CAP_PROP_POS_FRAMES` | Position of the next frame | Seek or inspect progress |
A file usually reports stable values. A camera reports values chosen by its driver and backend. Some sources cannot provide every property accurately.
Reading properties from a video
Read properties after opening the source and checking isOpened(). Convert dimensions and frame counts to integers because OpenCV returns property values as floating-point numbers.
import cv2
cap = cv2.VideoCapture("input.mp4")
if not cap.isOpened():
raise RuntimeError("Could not open input.mp4")
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f"size: {width} x {height}")
print(f"fps: {fps:.2f}")
print(f"frames: {frame_count}")
if fps > 0:
print(f"estimated duration: {frame_count / fps:.1f} seconds")
cap.release()The duration is only an estimate. Variable-frame-rate videos may not play every frame at one constant rate. Some codecs also report zero or an approximate frame count.
Requesting camera settings
For a camera, call set() before reading frames:
import cv2
cap = cv2.VideoCapture(0)
if not cap.isOpened():
raise RuntimeError("Could not open camera 0")
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
cap.set(cv2.CAP_PROP_FPS, 30)
actual_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
actual_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
actual_fps = cap.get(cv2.CAP_PROP_FPS)
print(f"actual size: {actual_width} x {actual_height}")
print(f"actual fps: {actual_fps:.2f}")
cap.release()A call to set() is a request, not a guarantee. The camera may choose a supported size such as 640 × 480, or it may select a different frame rate. Always read the property again and use the returned value in later calculations.
Why the backend matters
A backend is the system-specific component OpenCV uses to communicate with a file format, camera, or operating-system media service. Different backends support different properties. If a property returns 0, -1, or an unexpected value, the source or backend may not support it.
When troubleshooting, print the values after opening the source. Do not assume that a requested setting was accepted.
A practical rule
Use capture properties to configure and monitor a pipeline, but treat them as measurements with limitations. Check the source first, verify requests afterward, and handle unsupported values explicitly.
Exercise
A camera request sets the width to 1920, but cap.get(cv2.CAP_PROP_FRAME_WIDTH) returns 1280. Which width should the program use for its processing and output calculations?
Show answer
Use 1280, because it is the actual width reported by the camera. The request was not accepted exactly.