Skip to content
Search

Cv2.VideoCapture

lastmod 2024-10-04created 2023-01-072 edits

Class for video capturing from video files, image sequences or cameras. The class provides C++ API for capturing video from cameras or for reading video files and image sequences.

Detailed Description

Class for video capturing from video files, image sequences or cameras. The class provides C++ API for capturing video from cameras or for reading video files and image sequences. Here is how the class can be used:

#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;
    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, COLOR_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

In C API the black-box structure CvCapture is used instead of VideoCapture.

  • A basic sample on using the VideoCapture interface can be found at opencv_source_code/samples/cpp/starter_video.cpp
  • Another basic video processing sample can be found at opencv_source_code/samples/cpp/video_dmtx.cpp
  • (Python) A basic sample on using the VideoCapture interface can be found at opencv_source_code/samples/python/video.py
  • (Python) Another basic video processing sample can be found at opencv_source_code/samples/python/video_dmtx.py
  • (Python) A multi threaded video processing sample can be found at opencv_source_code/samples/python/video_threaded.py

Identifiers for key camera properties

Python constant

Property constant description

cv2.CAP_PROP_POS_MSEC

Current position in the video file, in milliseconds

cv2.CAP_PROP_POS_FRAMES

Index of the frame being captured

cv2.CAP_PROP_POS_AVI_RATIO

Relative position in the video file (0 - start, 1 - end)

cv2.CAP_PROP_FRAME_WIDTH

Frame width

cv2.CAP_PROP_FRAME_HEIGHT

Frame height

cv2.CAP_PROP_FPS

Frames per second

cv2.CAP_PROP_FOURCC

Four-character code of the codec

cv2.CAP_PROP_FRAME_COUNT

Total number of frames in the video file

cv2.CAP_PROP_FORMAT

Format of the matrix returned by cv2.VideoCapture.retieve()

cv2.CAP_PROP_BRIGHTNESS

Brightness of the image from the camera

cv2.CAP_PROP_CONTRAST

Contrast of the image from the camera

cv2.CAP_PROP_SATURATION

Saturation of the image from the camera

cv2.CAP_PROP_HUE

Hue of the image from the camera

cv2.CAP_PROP_GAIN

Gain of the image from the camera

cv2.CAP_PROP_EXPOSURE

Exposure of the camera

cv2.CAP_PROP_AUTOFOCUS

Autofocus control

See also

Favorive site

author Zer0 sitemap