Main Content

snapshot

Acquire single image frame from a webcam

Add-On Required: This feature requires the MATLAB Support Package for USB Webcams add-on.

Description

example

img = snapshot(cam); acquires a single image from the webcam object cam and assigns it to the variable img. The snapshot function returns the current frame. Calling snapshot in a loop returns a new frame each time. The returned image is always an RGB image. snapshot uses the camera’s default resolution or another resolution that you specify using the Resolution property.

Examples

collapse all

Use the snapshot function to acquire one image frame from a webcam. You then show it using a display function such as imshow or image.

Find the name of your camera using the webcamlist function to ensure that MATLAB® is discovering your camera(s).

webcamlist
ans = 2×1 cell array
    {'Logitech Webcam 250'          }
    {'Microsoft® LifeCam Cinema(TM)'}

Use the webcam function with the name of the camera as the input argument to create the object and connect it to the camera with that name. You can use the exact name from the output of webcamlist, which is 'Logitech Webcam 250' in this example. Or you can also use a shortened version of the name, for example, the brand of the camera, which in this case is 'Logitech'. Use cam as the name of the object.

cam = webcam('Logitech')
cam = 
  webcam with properties:

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

It creates the object and connects it to the Logitech® webcam.

Preview the image from the webcam.

preview(cam)

The preview window opens and displays live video stream from your camera. The preview dynamically updates, so if you change a property while previewing, the image changes to reflect the property change.

Close the preview.

closePreview(cam)

The preview window closes.

Acquire a single image from the camera using the snapshot function and assign it to the variable img.

img = snapshot(cam);

Display the acquired image.

imshow(img)

You can also use the image function to display the acquired image.

image(img)

Clean up by clearing the object.

clear('cam');

Input Arguments

collapse all

Webcam hardware connection created using webcam, specified as a webcam object.

Version History

Introduced in R2014a