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/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>
usingnamespacecv;
usingnamespacestd;
intmain(int, char**)
{
Matframe;
//--- INITIALIZE VIDEOCAPTUREVideoCapturecap;
// open the default camera using default API// cap.open(0);// OR advance usage: select any API backendintdeviceID = 0; // 0 = open default cameraintapiID = cv::CAP_ANY; // 0 = autodetect default API// open selected camera using selected APIcap.open(deviceID, apiID);
// check if we succeededif (!cap.isOpened()) {
cerr << "ERROR! Unable to open camera\n";
return -1;
}
//--- GRAB AND WRITE LOOPcout << "Start grabbing" << endl
<< "Press any key to terminate" << endl;
for (;;)
{
// wait for a new frame from camera and store it into 'frame'cap.read(frame);
// check if we succeededif (frame.empty()) {
cerr << "ERROR! blank frame grabbed\n";
break;
}
// show live and wait for a key with timeout long enough to show imagesimshow("Live", frame);
if (waitKey(5) >= 0)
break;
}
// the camera will be deinitialized automatically in VideoCapture destructorreturn0;
}
Opens a video file or a capturing device or an IP video stream for video capturing with API Preference.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters
filename
it can be:
name of video file (eg. video.avi)
or image sequence (eg. img_%02d.jpg, which will read samples like img_00.jpg, img_01.jpg, img_02.jpg, ...)
or URL of video stream (eg. protocol://host:port/script_name?script_params|auth)
or GStreamer pipeline string in gst-launch tool format in case if GStreamer is used as backend Note that each video stream or IP camera feed has its own URL scheme. Please refer to the documentation of source stream to know the right URL.
apiPreference
preferred Capture API backends to use. Can be used to enforce a specific reader implementation if multiple are available: e.g. or or .
Opens a video file or a capturing device or an IP video stream for video capturing with API Preference and parameters.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
The params parameter allows to specify extra parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ...). See cv::VideoCaptureProperties
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters
index
id of the video capturing device to open. To open default camera using default backend just pass 0. (to backward compatibility usage of camera_id + domain_offset (CAP_*) is valid when apiPreference is CAP_ANY)
apiPreference
preferred Capture API backends to use. Can be used to enforce a specific reader implementation if multiple are available: e.g. cv::CAP_DSHOW or cv::CAP_MSMF or cv::CAP_V4L.
VideoCapture() [5/6]
cv::VideoCapture::VideoCapture
(
int
index,
int
apiPreference,
const std::vector< int > &
params )
explicit
Python:
cv.VideoCapture(
) ->
<VideoCapture object>
cv.VideoCapture(
filename[, apiPreference]
) ->
<VideoCapture object>
cv.VideoCapture(
filename, apiPreference, params
) ->
Opens a camera for video capturing with API Preference and parameters.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
The params parameter allows to specify extra parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ...). See cv::VideoCaptureProperties
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
The params parameter allows to specify extra parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ...). See cv::VideoCaptureProperties
~VideoCapture()
virtual cv::VideoCapture::~VideoCapture
(
)
virtual
Default destructor.
The method first calls VideoCapture::release to close the already opened file or camera.
Grabs the next frame from video file or capturing device.
Returns
true (non-zero) in the case of success.
The method/function grabs the next frame from video file or camera and returns true (non-zero) in the case of success.
The primary use of the function is in multi-camera environments, especially when the cameras do not have hardware synchronization. That is, you call VideoCapture::grab() for each camera and after that call the slower method VideoCapture::retrieve() to decode and get frame from each camera. This way the overhead on demosaicing or motion jpeg decompression etc. is eliminated and the retrieved frames from different cameras will be closer in time.
Also, when a connected camera is multi-head (for example, a stereo camera or a Kinect device), the correct way of retrieving data from it is to call VideoCapture::grab() first and then call VideoCapture::retrieve() one or more times with different values of the channel parameter.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
The params parameter allows to specify extra parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ...). See cv::VideoCaptureProperties
Returns
true if the file has been successfully opened
The method first calls VideoCapture::release to close the already opened file or camera.
Opens a video file or a capturing device or an IP video stream for video capturing with API Preference and parameters.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
The params parameter allows to specify extra parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ...). See cv::VideoCaptureProperties
Returns
true if the file has been successfully opened
The method first calls VideoCapture::release to close the already opened file or camera.
Opens a camera for video capturing with API Preference and parameters.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
The params parameter allows to specify extra parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ...). See cv::VideoCaptureProperties
Returns
true if the camera has been successfully opened.
The method first calls VideoCapture::release to close the already opened file or camera.
the video frame is returned here. If no frames has been grabbed the image will be empty.
Returns
false if no frames has been grabbed
The method/function combines VideoCapture::grab() and VideoCapture::retrieve() in one call. This is the most convenient method for reading video files or capturing data from decode and returns the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the method returns false and the function returns empty image (with cv::Mat, test it with Mat::empty()).
the video frame is returned here. If no frames has been grabbed the image will be empty.
flag
it could be a frame index or a driver specific flag
Returns
false if no frames has been grabbed
The method decodes and returns the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the method returns false and the function returns an empty image (with cv::Mat, test it with Mat::empty()).
In C API, functions cvRetrieveFrame() and cv.RetrieveFrame() return image stored inside the video capturing structure. It is not allowed to modify or release the image! You can copy the frame using cvCloneImage and then do whatever you want with the copy.