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

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.

example

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

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