Skip to content

Instantly share code, notes, and snippets.

@anujonthemove
Last active January 24, 2025 09:04
Show Gist options
  • Save anujonthemove/e879ef6e2bd3f0ee1743c4c48ef779c1 to your computer and use it in GitHub Desktop.
Save anujonthemove/e879ef6e2bd3f0ee1743c4c48ef779c1 to your computer and use it in GitHub Desktop.
A handy list of VideoCapture object parameters taken from official OpenCV docs.
CAP_PROP_POS_MSEC =0, //!< Current position of the video file in milliseconds.
CAP_PROP_POS_FRAMES =1, //!< 0-based index of the frame to be decoded/captured next.
CAP_PROP_POS_AVI_RATIO =2, //!< Relative position of the video file: 0=start of the film, 1=end of the film.
CAP_PROP_FRAME_WIDTH =3, //!< Width of the frames in the video stream.
CAP_PROP_FRAME_HEIGHT =4, //!< Height of the frames in the video stream.
CAP_PROP_FPS =5, //!< Frame rate.
CAP_PROP_FOURCC =6, //!< 4-character code of codec. see VideoWriter::fourcc .
CAP_PROP_FRAME_COUNT =7, //!< Number of frames in the video file.
CAP_PROP_FORMAT =8, //!< Format of the %Mat objects returned by VideoCapture::retrieve().
CAP_PROP_MODE =9, //!< Backend-specific value indicating the current capture mode.
CAP_PROP_BRIGHTNESS =10, //!< Brightness of the image (only for those cameras that support).
CAP_PROP_CONTRAST =11, //!< Contrast of the image (only for cameras).
CAP_PROP_SATURATION =12, //!< Saturation of the image (only for cameras).
CAP_PROP_HUE =13, //!< Hue of the image (only for cameras).
CAP_PROP_GAIN =14, //!< Gain of the image (only for those cameras that support).
CAP_PROP_EXPOSURE =15, //!< Exposure (only for those cameras that support).
CAP_PROP_CONVERT_RGB =16, //!< Boolean flags indicating whether images should be converted to RGB.
CAP_PROP_WHITE_BALANCE_BLUE_U =17, //!< Currently unsupported.
CAP_PROP_RECTIFICATION =18, //!< Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently).
CAP_PROP_MONOCHROME =19,
CAP_PROP_SHARPNESS =20,
CAP_PROP_AUTO_EXPOSURE =21, //!< DC1394: exposure control done by camera, user can adjust reference level using this feature.
CAP_PROP_GAMMA =22,
CAP_PROP_TEMPERATURE =23,
CAP_PROP_TRIGGER =24,
CAP_PROP_TRIGGER_DELAY =25,
CAP_PROP_WHITE_BALANCE_RED_V =26,
CAP_PROP_ZOOM =27,
CAP_PROP_FOCUS =28,
CAP_PROP_GUID =29,
CAP_PROP_ISO_SPEED =30,
CAP_PROP_BACKLIGHT =32,
CAP_PROP_PAN =33,
CAP_PROP_TILT =34,
CAP_PROP_ROLL =35,
CAP_PROP_IRIS =36,
CAP_PROP_SETTINGS =37, //!< Pop up video/camera filter dialog (note: only supported by DSHOW backend currently. The property value is ignored)
CAP_PROP_BUFFERSIZE =38,
CAP_PROP_AUTOFOCUS =39,
CAP_PROP_SAR_NUM =40, //!< Sample aspect ratio: num/den (num)
CAP_PROP_SAR_DEN =41, //!< Sample aspect ratio: num/den (den)
@Zion-Dev1
Copy link

how do u access them tho

@anujonthemove
Copy link
Author

how do u access them tho

@ProTechZ I think the code below should help:

import cv2

def demonstrate_properties(cap):
    """
    Test and display OpenCV properties of the video capture device.
    """
    # List of properties to demonstrate
    properties = {
        "CAP_PROP_POS_MSEC": cv2.CAP_PROP_POS_MSEC,
        "CAP_PROP_POS_FRAMES": cv2.CAP_PROP_POS_FRAMES,
        "CAP_PROP_POS_AVI_RATIO": cv2.CAP_PROP_POS_AVI_RATIO,
        "CAP_PROP_FRAME_WIDTH": cv2.CAP_PROP_FRAME_WIDTH,
        "CAP_PROP_FRAME_HEIGHT": cv2.CAP_PROP_FRAME_HEIGHT,
        "CAP_PROP_FPS": cv2.CAP_PROP_FPS,
        "CAP_PROP_FOURCC": cv2.CAP_PROP_FOURCC,
        "CAP_PROP_FRAME_COUNT": cv2.CAP_PROP_FRAME_COUNT,
        "CAP_PROP_BRIGHTNESS": cv2.CAP_PROP_BRIGHTNESS,
        "CAP_PROP_CONTRAST": cv2.CAP_PROP_CONTRAST,
        "CAP_PROP_SATURATION": cv2.CAP_PROP_SATURATION,
        "CAP_PROP_HUE": cv2.CAP_PROP_HUE,
        "CAP_PROP_GAIN": cv2.CAP_PROP_GAIN,
        "CAP_PROP_EXPOSURE": cv2.CAP_PROP_EXPOSURE,
        "CAP_PROP_CONVERT_RGB": cv2.CAP_PROP_CONVERT_RGB,
        "CAP_PROP_ZOOM": cv2.CAP_PROP_ZOOM,
        "CAP_PROP_FOCUS": cv2.CAP_PROP_FOCUS,
    }

    print("\nTesting Video Capture Properties:")
    for prop_name, prop_id in properties.items():
        value = cap.get(prop_id)
        if value == -1:
            print(f"{prop_name}: Not supported by this device.")
        else:
            print(f"{prop_name}: {value}")


def real_time_demo(cap):
    """
    Demonstrates real-time properties while playing the video/camera feed.
    """
    print("\nReal-Time Video Properties Demonstration (Press 'q' to exit):")
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            print("End of video or no frames to capture.")
            break

        # Display the current frame
        current_time = cap.get(cv2.CAP_PROP_POS_MSEC)
        current_frame = cap.get(cv2.CAP_PROP_POS_FRAMES)
        total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)

        # Show the video frame
        cv2.imshow("Video", frame)
        print(f"Time: {current_time:.2f} ms | Frame: {current_frame}/{total_frames:.0f}", end="\r")

        # Press 'q' to exit
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break


def unsupported_properties_section():
    """
    Highlights properties that are hard to demonstrate in a standard environment.
    """
    print("\nProperties Not Demonstrated Directly:")
    unsupported_properties = {
        "CAP_PROP_WHITE_BALANCE_BLUE_U": "Not widely supported.",
        "CAP_PROP_RECTIFICATION": "Specific to stereo cameras.",
        "CAP_PROP_MONOCHROME": "Device-dependent; not commonly supported.",
        "CAP_PROP_SHARPNESS": "Requires specific hardware.",
        "CAP_PROP_TRIGGER": "Relevant for hardware triggers (e.g., industrial cameras).",
        "CAP_PROP_TRIGGER_DELAY": "Hardware-specific.",
        "CAP_PROP_TEMPERATURE": "Depends on thermal cameras.",
        "CAP_PROP_GUID": "Used for device identification.",
        "CAP_PROP_IRIS": "Applicable to advanced cameras with iris control.",
        "CAP_PROP_SETTINGS": "Opens camera settings dialog (Windows-only).",
    }

    for prop, description in unsupported_properties.items():
        print(f"{prop}: {description}")


# Main script
if __name__ == "__main__":
    # Video source (0 for webcam, or provide a video file path)
    video_source = 0  # Replace with 'sample_video.mp4' for video file
    cap = cv2.VideoCapture(video_source)

    if not cap.isOpened():
        print("Error: Could not open video source.")
        exit()

    print(f"Using backend: {cap.getBackendName()}")

    # Demonstrate properties
    demonstrate_properties(cap)

    # Real-time demonstration
    real_time_demo(cap)

    # Close video capture and windows
    cap.release()
    cv2.destroyAllWindows()

    # Unsupported properties section
    unsupported_properties_section()

@Zion-Dev1
Copy link

ty! My problem was that I was trying to access CAM_PROP_FRAME_WIDTH instead of CAP_PROP_FRAME_WIDTH😂

@anujonthemove
Copy link
Author

ty! My problem was that I was trying to access CAM_PROP_FRAME_WIDTH instead of CAP_PROP_FRAME_WIDTH😂

I see 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment