MATLAB Examples

Logging Video to Disk

This example shows how to use the snapshot function to acquire live images and log the video to disk.

MATLAB® Support Package for USB Webcams provides ability to bring live images from any USB Video Class (UVC) compliant Webcam into MATLAB®.


Set up Connection to Webcam

Use the webcam function to create a connection to the camera. This example uses "Logitech HD Webcam C310" camera.

% Connect to the webcam.
cam = webcam
cam = 

  webcam with properties:

                     Name: 'Logitech HD Webcam C310'
               Resolution: '640x480'
     AvailableResolutions: {1x19 cell}
             WhiteBalance: 0
                 Contrast: 32
                 Exposure: -6
    BacklightCompensation: 1
                     Gain: 32
               Brightness: 128
               Saturation: 32
             ExposureMode: 'auto'
                Sharpness: 24

Open Video File using VideoWriter

Create the VideoWriter object to open an AVI file for writing.

vidWriter = VideoWriter('frames.avi');

Acquire and store frames

The following loop writes the acquired frames to the specified AVI file for future processing.

for index = 1:20
    % Acquire frame for processing
    img = snapshot(cam);

    % Write frame to video
    writeVideo(vidWriter, img);

Clean up

Once the connection is no longer needed, clear the associated variable.

clear cam