Main Content

webcam

Connection to a webcam

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

Description

A webcam object represents a connection to a webcam. Use the webcam function to establish a connection with a webcam on your system and acquire images from it. After you create the object, you can access images using the Object Functions.

Creation

Description

cam = webcam creates the webcam object cam and connects to the single webcam on your system. If you have multiple cameras and you use the webcam function with no input argument, it creates the object and connects it to the first camera it finds listed in the output of the webcamlist function.

When the webcam object is created, it connects to the camera, establishes exclusive access, and starts streaming data. You can then preview the data and acquire images using the snapshot function.

example

cam = webcam(devicenumber) creates a webcam object cam where devicenumber is a numeric scalar value that identifies a particular webcam by its index number. When you use the webcam function with an index as the input argument, it creates the object corresponding to that index and connects it to that camera.

example

cam = webcam('cameraname') creates a webcam object cam where cameraname is a character vector value that identifies a particular webcam by its name. When you use the webcam function with the name of the camera as the input argument, it creates the object and connects it to the camera with that name.

example

Input Arguments

expand all

Device number of your webcam, specified as a numeric scalar. This number identifies a particular webcam by its index. The index corresponds to the order of cameras in the cell array returned by webcamlist when you have multiple cameras connected. It creates the object corresponding to that index and connects it to that camera.

Example: cam = webcam(2)

Data Types: double

Name of your webcam, specified as a character vector. This argument identifies a particular webcam by its name. You can use the exact name that is returned by the webcamlist function, such as 'Logitech Webcam 250'. You can also use a shortened version of the name, for example, the brand of the camera. In this example you could simply use 'Logitech' and it connects to the Logitech® webcam. When you use the webcam function with the name of the camera as the input argument, it creates the object and connects it to the camera with that name.

Example: cam = webcam('Logitech')

Data Types: char | string

Properties

expand all

Most of the properties of a webcam object are specific to the camera model. You can use the following properties for any webcam.

See Set Properties for Webcam Acquisition for a full list of possible webcam properties and how to specify them using dot notation.

Note

The only properties available in MATLAB® Online™ are Name, AvailableResolutions, and Resolution. The default resolution of the webcam is the only resolution supported in MATLAB Online.

This property is read-only.

Name of your webcam, specified as a character vector. This is the name returned by the webcamlist function. It is specific to each camera.

Resolution of incoming video stream, specified as a character vector. The default that is used is the default resolution of the camera. You can change the resolution to another one listed in the AvailableResolutions property.

This property is read-only.

Resolutions available on your webcam, specified as a cell array of character vectors.

This property is read-only.

Frame rate of incoming video stream in frames per second, specified as a double. Some cameras do not support this property.

Object Functions

closePreviewClose webcam preview window
previewPreview live video data from webcam
snapshotAcquire single image frame from a webcam

Examples

collapse all

Use the webcam function with no input arguments to create the object and connect it to the first camera it finds. If you only have one camera connected to your system, it connects to that one. If you have multiple cameras, it connects to the first camera listed in the output of the webcamlist function. In the example shown here, it creates the object using the Logitech® camera, since that appears in the webcamlist output first.

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

Create an object, cam.

cam = webcam;

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

Use the webcam function with an index as the input argument to create the object corresponding to that index and connect it to that camera. If you have multiple cameras connected, index corresponds to the order of cameras in the cell array returned by webcamlist. In the example shown here, device 1 is the Logitech camera and device 2 is the Microsoft® camera.

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

Create an object, cam.

cam = webcam(2);

It creates the object and connects it to the Microsoft camera. If you only have one camera, you do not need to use the index.

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.

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

Create an object, cam. 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'.

cam = webcam('Logitech');

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

Version History

Introduced in R2014a