Main Content

webcam

Connection to a webcam

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

example

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.

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
cam = 
  webcam with properties:

                     Name: 'Logitech Webcam 250'
               Resolution: '640x480'
     AvailableResolutions: {'640x480'  '160x90'  '160x100'  '160x120'  '176x144'  '320x180'  '320x200'  '320x240'  '352x288'  '640x360'  '640x400'}
                Sharpness: 48
               Saturation: 32
    BacklightCompensation: 1
                 Contrast: 32
                     Gain: 63
             ExposureMode: 'auto'
                 Exposure: -6
               Brightness: 128
             WhiteBalance: 0

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)
cam = 
  webcam with properties:

                     Name: 'Microsoft® LifeCam Cinema(TM)'
               Resolution: '640x480'
     AvailableResolutions: {'640x480'  '640x360'  '424x240'  '352x288'  '320x240'  '176x144'  '160x120'  '1280x720'  '960x544'  '800x448'  '800x600'}
         WhiteBalanceMode: 'auto'
                FocusMode: 'auto'
                 Contrast: 5
             ExposureMode: 'auto'
                    Focus: 3
                 Exposure: -6
             WhiteBalance: 4500
                     Zoom: 0
                     Tilt: 0
               Saturation: 83
               Brightness: 143
                Sharpness: 25
                      Pan: 0
    BacklightCompensation: 1

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')
cam = 
  webcam with properties:

                     Name: 'Logitech Webcam 250'
               Resolution: '640x480'
     AvailableResolutions: {'640x480'  '160x90'  '160x100'  '160x120'  '176x144'  '320x180'  '320x200'  '320x240'  '352x288'  '640x360'  '640x400'}
               Brightness: 128
    BacklightCompensation: 1
                     Gain: 63
                 Contrast: 32
               Saturation: 32
             ExposureMode: 'auto'
                 Exposure: -6
             WhiteBalance: 0
                Sharpness: 48

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

Version History

Introduced in R2014a