Class for video capturing from video files, image sequences or cameras.
More...
#include <opencv2/videoio.hpp>
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 <iostream>
#include <stdio.h>
int main(int, char**)
{
int deviceID = 0;
cap.
open(deviceID + apiID);
cerr << "ERROR! Unable to open camera\n";
return -1;
}
cout << "Start grabbing" << endl
<< "Press any key to terminate" << endl;
for (;;)
{
cerr << "ERROR! blank frame grabbed\n";
break;
}
break;
}
return 0;
}
- Note
- In C API the black-box structure
CvCapture
is used instead of VideoCapture.
-
- (C++) A basic sample on using the VideoCapture interface can be found at
OPENCV_SOURCE_CODE/samples/cpp/videocapture_starter.cpp
- (Python) A basic sample on using the VideoCapture interface can be found at
OPENCV_SOURCE_CODE/samples/python/video.py
- (Python) A multi threaded video processing sample can be found at
OPENCV_SOURCE_CODE/samples/python/video_threaded.py
- (Python) VideoCapture sample showcasing some features of the Video4Linux2 backend
OPENCV_SOURCE_CODE/samples/python/video_v4l2.py
- Examples:
- samples/cpp/camshiftdemo.cpp, samples/cpp/facedetect.cpp, samples/cpp/laplace.cpp, samples/cpp/lkdemo.cpp, samples/cpp/peopledetect.cpp, samples/cpp/polar_transforms.cpp, samples/cpp/segment_objects.cpp, samples/cpp/train_HOG.cpp, samples/cpp/tutorial_code/videoio/video-write/video-write.cpp, samples/cpp/videowriter_basic.cpp, samples/dnn/classification.cpp, samples/dnn/object_detection.cpp, samples/dnn/segmentation.cpp, samples/dnn/text_detection.cpp, and samples/tapi/hog.cpp.
◆ VideoCapture() [1/5]
cv::VideoCapture::VideoCapture |
( |
| ) |
|
Python: |
---|
| <VideoCapture object> | = | cv.VideoCapture( | | ) |
| <VideoCapture object> | = | cv.VideoCapture( | filename | ) |
| <VideoCapture object> | = | cv.VideoCapture( | filename, apiPreference | ) |
| <VideoCapture object> | = | cv.VideoCapture( | index | ) |
| <VideoCapture object> | = | cv.VideoCapture( | index, apiPreference | ) |
◆ VideoCapture() [2/5]
cv::VideoCapture::VideoCapture |
( |
const String & |
filename | ) |
|
Python: |
---|
| <VideoCapture object> | = | cv.VideoCapture( | | ) |
| <VideoCapture object> | = | cv.VideoCapture( | filename | ) |
| <VideoCapture object> | = | cv.VideoCapture( | filename, apiPreference | ) |
| <VideoCapture object> | = | cv.VideoCapture( | index | ) |
| <VideoCapture object> | = | cv.VideoCapture( | index, apiPreference | ) |
Open video file or a capturing device or a IP video stream for video capturing.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Same as VideoCapture(const String& filename, int apiPreference) but using default Capture API backends
◆ VideoCapture() [3/5]
cv::VideoCapture::VideoCapture |
( |
const String & |
filename, |
|
|
int |
apiPreference |
|
) |
| |
Python: |
---|
| <VideoCapture object> | = | cv.VideoCapture( | | ) |
| <VideoCapture object> | = | cv.VideoCapture( | filename | ) |
| <VideoCapture object> | = | cv.VideoCapture( | filename, apiPreference | ) |
| <VideoCapture object> | = | cv.VideoCapture( | index | ) |
| <VideoCapture object> | = | cv.VideoCapture( | index, apiPreference | ) |
Open video file or a capturing device or a 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 ). 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. cv::CAP_FFMPEG or cv::CAP_IMAGES or cv::CAP_DSHOW. |
- See also
- The list of supported API backends cv::VideoCaptureAPIs
◆ VideoCapture() [4/5]
cv::VideoCapture::VideoCapture |
( |
int |
index | ) |
|
Python: |
---|
| <VideoCapture object> | = | cv.VideoCapture( | | ) |
| <VideoCapture object> | = | cv.VideoCapture( | filename | ) |
| <VideoCapture object> | = | cv.VideoCapture( | filename, apiPreference | ) |
| <VideoCapture object> | = | cv.VideoCapture( | index | ) |
| <VideoCapture object> | = | cv.VideoCapture( | index, apiPreference | ) |
Open a camera for video capturing.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
- Parameters
-
index | camera_id + domain_offset (CAP_*) id of the video capturing device to open. To open default camera using default backend just pass 0. Use a domain_offset to enforce a specific reader implementation if multiple are available like cv::CAP_FFMPEG or cv::CAP_IMAGES or cv::CAP_DSHOW. e.g. to open Camera 1 using the MS Media Foundation API use index = 1 + cv::CAP_MSMF |
- See also
- The list of supported API backends cv::VideoCaptureAPIs
◆ VideoCapture() [5/5]
cv::VideoCapture::VideoCapture |
( |
int |
index, |
|
|
int |
apiPreference |
|
) |
| |
Python: |
---|
| <VideoCapture object> | = | cv.VideoCapture( | | ) |
| <VideoCapture object> | = | cv.VideoCapture( | filename | ) |
| <VideoCapture object> | = | cv.VideoCapture( | filename, apiPreference | ) |
| <VideoCapture object> | = | cv.VideoCapture( | index | ) |
| <VideoCapture object> | = | cv.VideoCapture( | index, apiPreference | ) |
Opens a camera for video capturing.
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_V4L2. |
- See also
- The list of supported API backends cv::VideoCaptureAPIs
◆ ~VideoCapture()
virtual cv::VideoCapture::~VideoCapture |
( |
| ) |
|
|
virtual |
Default destructor.
The method first calls VideoCapture::release to close the already opened file or camera.
◆ get()
virtual double cv::VideoCapture::get |
( |
int |
propId | ) |
const |
|
virtual |
Python: |
---|
| retval | = | cv.VideoCapture.get( | propId | ) |
Returns the specified VideoCapture property.
- Parameters
-
- Returns
- Value for the specified property. Value 0 is returned when querying a property that is not supported by the backend used by the VideoCapture instance.
- Note
- Reading / writing properties involves many layers. Some unexpected result might happens along this chain. The returned value might be different from what really used by the device or it could be encoded using device dependent rules (eg. steps or percentage). Effective behaviour depends from device driver and API Backend
- Examples:
- samples/cpp/laplace.cpp, and samples/cpp/tutorial_code/videoio/video-write/video-write.cpp.
◆ getBackendName()
String cv::VideoCapture::getBackendName |
( |
| ) |
const |
Python: |
---|
| retval | = | cv.VideoCapture.getBackendName( | | ) |
Returns used backend API name.
- Note
- Stream should be opened.
◆ grab()
virtual bool cv::VideoCapture::grab |
( |
| ) |
|
|
virtual |
Python: |
---|
| retval | = | cv.VideoCapture.grab( | | ) |
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.
Using Kinect and other OpenNI compatible depth sensors
◆ isOpened()
virtual bool cv::VideoCapture::isOpened |
( |
| ) |
const |
|
virtual |
Python: |
---|
| retval | = | cv.VideoCapture.isOpened( | | ) |
Returns true if video capturing has been initialized already.
If the previous call to VideoCapture constructor or VideoCapture::open() succeeded, the method returns true.
- Examples:
- samples/cpp/camshiftdemo.cpp, samples/cpp/facedetect.cpp, samples/cpp/laplace.cpp, samples/cpp/lkdemo.cpp, samples/cpp/peopledetect.cpp, samples/cpp/polar_transforms.cpp, samples/cpp/segment_objects.cpp, samples/cpp/train_HOG.cpp, samples/cpp/tutorial_code/videoio/video-write/video-write.cpp, samples/cpp/videowriter_basic.cpp, and samples/tapi/hog.cpp.
◆ open() [1/4]
virtual bool cv::VideoCapture::open |
( |
const String & |
filename | ) |
|
|
virtual |
Python: |
---|
| retval | = | cv.VideoCapture.open( | filename | ) |
| retval | = | cv.VideoCapture.open( | index | ) |
| retval | = | cv.VideoCapture.open( | cameraNum, apiPreference | ) |
| retval | = | cv.VideoCapture.open( | filename, apiPreference | ) |
Open video file or a capturing device or a IP video stream for video capturing.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters are same as the constructor VideoCapture(const String& filename)
- Returns
true
if the file has been successfully opened
The method first calls VideoCapture::release to close the already opened file or camera.
- Examples:
- samples/cpp/camshiftdemo.cpp, samples/cpp/facedetect.cpp, samples/cpp/laplace.cpp, samples/cpp/lkdemo.cpp, samples/cpp/peopledetect.cpp, samples/cpp/polar_transforms.cpp, samples/cpp/segment_objects.cpp, samples/cpp/train_HOG.cpp, samples/dnn/classification.cpp, samples/dnn/object_detection.cpp, samples/dnn/segmentation.cpp, samples/dnn/text_detection.cpp, and samples/tapi/hog.cpp.
◆ open() [2/4]
virtual bool cv::VideoCapture::open |
( |
int |
index | ) |
|
|
virtual |
Python: |
---|
| retval | = | cv.VideoCapture.open( | filename | ) |
| retval | = | cv.VideoCapture.open( | index | ) |
| retval | = | cv.VideoCapture.open( | cameraNum, apiPreference | ) |
| retval | = | cv.VideoCapture.open( | filename, apiPreference | ) |
Open a camera for video capturing.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters are same as the constructor VideoCapture(int index)
- Returns
true
if the camera has been successfully opened.
The method first calls VideoCapture::release to close the already opened file or camera.
◆ open() [3/4]
bool cv::VideoCapture::open |
( |
int |
cameraNum, |
|
|
int |
apiPreference |
|
) |
| |
Python: |
---|
| retval | = | cv.VideoCapture.open( | filename | ) |
| retval | = | cv.VideoCapture.open( | index | ) |
| retval | = | cv.VideoCapture.open( | cameraNum, apiPreference | ) |
| retval | = | cv.VideoCapture.open( | filename, apiPreference | ) |
Open a camera for video capturing.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters are similar as the constructor VideoCapture(int index),except it takes an additional argument apiPreference. Definitely, is same as open(int index) where index=cameraNum + apiPreference
- Returns
true
if the camera has been successfully opened.
◆ open() [4/4]
virtual bool cv::VideoCapture::open |
( |
const String & |
filename, |
|
|
int |
apiPreference |
|
) |
| |
|
virtual |
Python: |
---|
| retval | = | cv.VideoCapture.open( | filename | ) |
| retval | = | cv.VideoCapture.open( | index | ) |
| retval | = | cv.VideoCapture.open( | cameraNum, apiPreference | ) |
| retval | = | cv.VideoCapture.open( | filename, apiPreference | ) |
Open video file or a capturing device or a 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 are same as the constructor VideoCapture(const String& filename, int apiPreference)
- Returns
true
if the file has been successfully opened
The method first calls VideoCapture::release to close the already opened file or camera.
◆ operator>>() [1/2]
Stream operator to read the next video frame.
- See also
- read()
◆ operator>>() [2/2]
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
- See also
- read()
◆ read()
virtual bool cv::VideoCapture::read |
( |
OutputArray |
image | ) |
|
|
virtual |
Python: |
---|
| retval, image | = | cv.VideoCapture.read( | [, image] | ) |
Grabs, decodes and returns the next video frame.
- Parameters
-
[out] | image | 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()).
- Note
- 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.
- Examples:
- samples/cpp/videowriter_basic.cpp.
◆ release()
virtual void cv::VideoCapture::release |
( |
| ) |
|
|
virtual |
Python: |
---|
| None | = | cv.VideoCapture.release( | | ) |
Closes video file or capturing device.
The method is automatically called by subsequent VideoCapture::open and by VideoCapture destructor.
The C function also deallocates memory and clears *capture pointer.
◆ retrieve()
virtual bool cv::VideoCapture::retrieve |
( |
OutputArray |
image, |
|
|
int |
flag = 0 |
|
) |
| |
|
virtual |
Python: |
---|
| retval, image | = | cv.VideoCapture.retrieve( | [, image[, flag]] | ) |
Decodes and returns the grabbed video frame.
- Parameters
-
[out] | image | 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()).
- See also
- read()
- Note
- 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.
◆ set()
virtual bool cv::VideoCapture::set |
( |
int |
propId, |
|
|
double |
value |
|
) |
| |
|
virtual |
Python: |
---|
| retval | = | cv.VideoCapture.set( | propId, value | ) |
◆ cap
◆ icap
Ptr<IVideoCapture> cv::VideoCapture::icap |
|
protected |
The documentation for this class was generated from the following file: