cv.VideoCapture - MATLAB File Help Go to online doc for cv.VideoCapture
cv.VideoCapture

Class for video capturing from video files or cameras

The class provides an API for capturing video from cameras or for reading video files and image sequences.

Example

Here is how the class can be used:

cap = cv.VideoCapture(0);        % open the default camera
pause(3);                        % see note below
if ~cap.isOpened()               % check if we succeeded
    error('camera failed to initialized');
end
for t=1:30
    frame = cap.read();           % get a new frame from camera
    imshow(frame);
    pause(0.1);
end
% the camera will be deinitialized automatically in destructor
% when "cap" goes out of scope
% you can also explicitly call cap.release() to close the camera

Note: In some environments, there is a concurrency issue during camera initialization. To avoid unexpected crash, pause for a few seconds after the initialization of cv.VideoCapture object.

Video I/O with OpenCV Overview

The OpenCV "videoio" module is a set of classes and functions to read and write video or images sequence.

Basically, the module provides the cv.VideoCapture and cv.VideoWriter classes as 2-layer interface to many video I/O APIs used as backend.

image

Some backends such as (DirectShow) Direct Show, Video For Windows (VfW), Microsoft Media Foundation (MediaFoundation), Video 4 Linux (V4L), etc... are interfaces to the video I/O library provided by the operating system.

Some others backends like OpenNI2 for Kinect, Intel Perceptual Computing SDK, GStreamer, XIMEA Camera API, etc... are interfaces to proprietary drivers or to external library.

See the list of supported backends here: cv.VideoCapture.open

Warning: Some backends are experimental use them at your own risk. Note: Each backend supports devices properties (cv.VideoCapture.set) in a different way or might not support any property at all.

Select the backend at runtime

OpenCV automatically selects and uses first available backend (API='Any').

As advanced usage you can select the backend to use at runtime. Currently this option is available only with cv.VideoCapture.

For example to grab from default camera using Direct Show as backend

% declare a capture object
cap = cv.VideoCapture(0, 'API','DirectShow')

% or specify the API preference with open
cap.open(0, 'API','DirectShow');

If you want to grab from a file using the Direct Show as backend:

% declare a capture object
cap = cv.VideoCapture(filename, 'API','DirectShow')

% or specify the API preference with open
cap.open(filename, 'API','DirectShow');

Enable backends

Backends are available only if they have been built with your OpenCV binaries.

Check in opencv2/cvconfig.h to know which APIs are currently available (e.g. HAVE_MSMF, HAVE_VFW, HAVE_LIBV4L, etc...).

To enable/disable APIs, you have to:

  1. re-configure OpenCV using appropriates CMake switches (e.g. -DWITH_MSMF=ON -DWITH_VFW=ON ...) or checking related switch in cmake-gui
  2. rebuild OpenCV itself

Use 3rd party drivers or cameras

Many industrial cameras or some video I/O devices don't provide standard driver interfaces for the operating system. Thus you can't use cv.VideoCapture or cv.VideoWriter with these devices.

The FFmpeg library

OpenCV can use the FFmpeg library as backend to record, convert and stream audio and video. FFMpeg is a complete, cross-reference solution. If you enable FFmpeg while configuring OpenCV than CMake will download and install the binaries in OPENCV_SOURCE_CODE/3rdparty/ffmpeg/. To use FFMpeg at runtime, you must deploy the FFMepg binaries with your application.

Note: FFmpeg is licensed under the GNU Lesser General Public License (LGPL) version 2.1 or later. See OPENCV_SOURCE_CODE/3rdparty/ffmpeg/readme.txt and http://ffmpeg.org/legal.html for details and licensing information.

See also
Class Details
Superclasses handle
Sealed false
Construct on load false
Constructor Summary
VideoCapture Open video file or a capturing device or a IP video stream for video capturing 
Property Summary
Brightness of the image (only for those cameras that support it). 
Contrast of the image (only for cameras). 
ConvertRGB Boolean flags indicating whether images should be converted to RGB. 
Exposure (only for those cameras that support it). 
FPS Frame rate. 
Format of the frames returned by cv.VideoCapture.retrieve. 
FourCC 4-character code of codec. See cv.VideoWriter.open. 
FrameCount Number of frames in the video file. 
FrameHeight Height of the frames in the video stream. 
FrameWidth Width of the frames in the video stream. 
Gain of the image (only for those cameras that support it). 
Hue of the image (only for cameras). 
Mode Backend-specific value indicating the current capture mode. 
PosAviRatio Relative position of the video file: 
PosFrames 0-based index of the frame to be decoded/captured next. 
PosMsec Current position of the video file in milliseconds or video capture 
Rectification flag for stereo cameras 
Saturation of the image (only for cameras). 
id Object ID 
Method Summary
  addlistener Add listener for event. 
  delete Destructor 
  eq == (EQ) Test handle equality. 
  findobj Find objects matching specified conditions. 
  findprop Find property of MATLAB handle object. 
  ge >= (GE) Greater than or equal relation for handles. 
  get Returns the specified VideoCapture property 
  grab Grabs the next frame from video file or capturing device 
  gt > (GT) Greater than relation for handles. 
  isOpened Returns true if video capturing has been initialized already 
Sealed   isvalid Test handle validity. 
  le <= (LE) Less than or equal relation for handles. 
  listener Add listener for event without binding the listener to the source object. 
  lt < (LT) Less than relation for handles. 
  ne ~= (NE) Not equal relation for handles. 
  notify Notify listeners of event. 
  open Open video file or a capturing device or a IP video stream for video capturing 
  read Grabs, decodes and returns the next video frame 
  release Closes video file or capturing device 
  retrieve Decodes and returns the grabbed video frame 
  set Sets a property in the VideoCapture 
Event Summary
ObjectBeingDestroyed Notifies listeners that a particular object has been destroyed.