MATLAB

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}
                 Contrast: 32
               Brightness: 128
                 Exposure: -6
    BacklightCompensation: 1
               Saturation: 32
             WhiteBalance: 0
                Sharpness: 24
                     Gain: 32
             ExposureMode: 'auto'

Open Video File using VideoWriter

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

vidWriter = VideoWriter('frames.avi');
open(vidWriter);

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);
end

Clean up

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

close(vidWriter);
clear cam