Main Content

preview

Preview live video data from webcam

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

Description

preview(cam) creates a preview window that displays live video data for the webcam object cam. The size of the preview image is determined by the value of the Resolution property for cam. The preview window shows live RGB image from the webcam and displays the camera name, resolution, frame rate, and the timestamp. Timestamp is the elapsed time since the object was created.

example

preview(cam,im) creates a preview window that displays live video data for webcam object cam. The preview window properties are defined by the image object im. The size of the preview image is determined by the value of the Resolution property for cam. The preview window shows a live RGB image from the webcam. This syntax is not supported in MATLAB® Online™.

example

Examples

collapse all

Preview video from your webcam before acquiring snapshots.

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

webcamlist
ans = 1×1 cell array
    {'Logitech Webcam 250'}

Use the webcam function with the name of the camera as the input argument to create the object and connect 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');

The webcam function creates the object and connects it to the Logitech® webcam.

Preview the live video stream from the webcam.

preview(cam)

The preview window displays the 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. The banner of the preview window shows the camera name. The lower portion of the window shows the timestamp in seconds, resolution, and the frame rate in frames per second. Timestamp is the elapsed time since the object was created.

After you preview the live video, the next steps are optionally changing any properties you need to set, and then acquiring images using the snapshot function.

Apply custom settings to the preview window that displays live video from your webcam. This feature is not supported in MATLAB® Online™.

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

webcamlist
ans = 1×1 cell array
    {'Logitech Webcam 250'}

Use the webcam function with the name of the camera as the input argument to create the object and connect to the camera with that name. Use cam as the name of the object.

cam = webcam('Logitech');

Create a figure object, and specify its properties. In this example, the figure number is not displayed in the title bar and menus are not displayed at the top of the figure window. Set the Name property to 'My Camera'. You can change these properties to further customize your figure window.

fig = figure('NumberTitle','off','MenuBar','none');
fig.Name = 'My Camera';

To display live video steam on your webcam in the figure, identify these additional properties. Create an Axes object from your figure. Call this object ax. Define the dimensions of the webcam images by capturing a single image from the webcam, and call it frame. Create an Image object using these two variables, and call this object im. Set the axis limits for your figure window with ax, and specify the style as 'image'.

ax = axes(fig); 
frame = snapshot(cam); 
im = image(ax,zeros(size(frame),'uint8')); 
axis(ax,'image');

Preview the live video stream from the webcam in the custom figure window you generated with the properties you set.

preview(cam,im)

Input Arguments

collapse all

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

Webcam image properties for a custom figure window created using image, specified as an image object.

Version History

Introduced in R2014a