Professional Documents
Culture Documents
Webcam
Webcam
snapshot
Acquire single image frame from a Webcam
Syntax
img = snapshot(cam); example
Description
img = snapshot(cam); acquires a single image from the Webcam cam and assigns it to the variable img. The example
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.
This example shows how to acquire and display one image frame from a Webcam.
Find the name of your camera using the webcamlist function to ensure that MATLAB® is discovering your camera(s).
webcamlist
ans =
Create the object. If you use the webcam function with the name of the camera (as a string) as the input argument, it creates the object
and connects it to the camera with that name. You can use the exact name that is displayed by the webcamlist function. In the
example above it would be 'Logitech Webcam 250'. You can also use a shortened version of the name, for example, the brand of the
camera. In this case you could simply use 'Logitech' and it would connect to the Logitech® Webcam. Use cam as the name of the
object.
cam = webcam('Logitech')
cam =
See that it creates the object and connects to the Logitech 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.
file:///C:/Program%20Files/MATLAB/R2014a/help/matlab/ref/snapshot.html 1/2
8/6/2018 snapshot
Close the preview.
closePreview(cam)
Acquire a single image from the camera using the snapshot function and assign it to the variable img.
img = snapshot(cam);
imshow(img)
The imshow function is part of the Image Processing Toolbox™. If you do not have that, you can use the image function that is part of
MATLAB.
image(img)
clear('cam');
Connecting to Webcams
Acquiring Images from Webcams
Setting Properties for Webcam Acquisition
file:///C:/Program%20Files/MATLAB/R2014a/help/matlab/ref/snapshot.html 2/2