Acquiring a Single Image in a Loop
This example shows how to use the snapshot
function to acquire live images from USB webcams.
MATLAB® Support Package for USB Webcams provides the ability to bring live images from any USB video class (UVC) compliant webcam into MATLAB.
Identifying Available Webcams
The webcamlist
function provides a cell array of webcams on the current system that MATLAB can access.
camList = webcamlist
camList = 1×1 cell array
{'Logitech Webcam 250'}
Set up Connection to Webcam
A webcam object represents the connection between MATLAB® and the USB webcam. To create a connection to the webcam, use the webcam
function and indicate what camera to connect to. You can specify the camera either by name or index as returned by webcamlist
. This example uses the "Logitech Webcam 250" camera. Once the connection is established, you can access specific property values by using the dot(.) notation.
% Connect to the webcam.
cam = webcam(1);
Preview Video Stream
To open a video preview window, use the preview
function. The video preview window displays the live video stream from the device.
preview(cam);
Acquire a Frame
To acquire a single frame, use the snapshot
function.
img = snapshot(cam);
% Display the frame in a figure window.
image(img);
Acquiring Multiple Frames
A common task is to repeatedly acquire a single image, process it, and then store the result. To do this, snapshot
should be called in a loop.
for idx = 1:5 img = snapshot(cam); image(img); end
Clean Up
Once the connection is no longer needed, clear the associated variable.
clear cam