Main Content

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 Webcam 250" camera.

% Connect to the webcam.
cam = webcam
cam = 
  webcam with properties:

                     Name: 'Logitech Webcam 250'
               Resolution: '640x480'
     AvailableResolutions: {'640x480'  '160x90'  '160x100'  '160x120'  '176x144'  '320x180'  '320x200'  '320x240'  '352x288'  '640x360'  '640x400'}
    BacklightCompensation: 1
                Sharpness: 48
               Brightness: 128
                     Gain: 63
               Saturation: 32
                 Exposure: -6
             WhiteBalance: 0
             ExposureMode: 'auto'
                 Contrast: 32

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

Clean Up

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

clear cam