Documentation Center

  • Trial Software
  • Product Updates

Acquiring Images from Webcams

Creating a Webcam Object

Use the webcam function to create a Webcam object. You can use it in one of three ways:

Connect to the first or only camera, by using no input arguments
Specify a camera by name, by using the Webcam name (as a string) as an input argument
Specify a camera by the list order, by using an index number as the input argument

    Note:   Webcam support is available only through a Hardware Support Package. You must download and install the necessary files using the Support Package Installer. For instructions, see Installing the Webcam Support Package.

Find the name of your camera by using the webcamlist function. Run webcamlist first to make sure that MATLAB® can discover your camera(s). In this example, it discovers the built-in Webcam in the Dell® computer, and a connected Logitech® Webcam.

webcamlist

ans = 

		 'Logitech Webcam 250'
		 'Dell Camera C250' 

No Input Argument

If you use the webcam function with no input argument, it creates the object and connects to the first camera returned by webcamlist. This will be the first camera shown on the list if you have multiple cameras. If you only have one camera connected to your system, it will use that one. So in the example shown above, it will create the object using the Logitech camera, since that appears in the webcamlist output first.

% Use cam as the name of the object.

cam = webcam

cam = 

webcam with properties:

                     Name: 'Logitech Webcam 250'
               Resolution: '640x480'
     AvailableResolutions: {1x11 cell}
                 Exposure: -4
                     Gain: 253
               Saturation: 32
             WhiteBalance: 8240
             ExposureMode: 'auto'
                Sharpness: 48
               Brightness: 128
    BacklightCompensation: 1
                 Contrast: 32

You can see that it created the object and connected to the Logitech Webcam.

Index as Input Argument

If you use the webcam function with an index as the input argument, it creates the object corresponding to that index and connects to that camera. The index corresponds to the order of cameras in the cell array returned by webcamlist when you have multiple cameras connected. So in the example shown above, device 1 is the Logitech camera and device 2 is the built-in Dell Webcam.

% Use cam as the name of the object. Use 2 to connect to the Dell camera.

cam = webcam(2)

cam = 

webcam with properties:

                Name: 'Dell Camera C250'
          Resolution: '320x240'
AvailableResolutions: ('320x240' '160x120' '80x60')
          Brightness: 128
            Contrast: 32
                Gain: 0

You can see that it created the object and connected to the Dell Webcam. If you only have one camera, you do not need to use the index. You can use the webcam function with no input argument and it creates the object with the single camera that is connected. The index is useful when you have multiple cameras.

Camera Name as Input Argument

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 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. Use 'Logitech' to connect to the Logitech camera.
cam = webcam('Logitech')

cam = 

webcam with properties:

                     Name: 'Logitech Webcam 250'
               Resolution: '640x480'
     AvailableResolutions: {1x11 cell}
                 Exposure: -4
                     Gain: 253
               Saturation: 32
             WhiteBalance: 8240
             ExposureMode: 'auto'
                Sharpness: 48
               Brightness: 128
    BacklightCompensation: 1
                 Contrast: 32

You can see that it created the object and connected to the Logitech Webcam.

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

Acquiring Webcam Images

Acquiring images from Webcams and bringing them into MATLAB is a simple process. The general procedure is to make sure your camera is connected, create a webcam object, preview the image, and acquire snapshots. This typical workflow is outlined here.

  1. See what cameras are connected to your system and make sure MATLAB can detect them.

    webcamlist
    
    ans = 
    
    		 'Logitech Webcam 250'
    		 'Dell Camera C250' 

    The output is a list of any Webcams that are connected to your system. In this example, it discovers a built-in Webcam in the Dell computer, and a connected Logitech Webcam.

  2. Create a webcam object called cam, using the Logitech camera.

    cam = webcam('Logitech')
    
    cam = 
    
    webcam with properties:
    
                         Name: 'Logitech Webcam 250'
                   Resolution: '640x480'
         AvailableResolutions: {1x11 cell}
                     Exposure: -4
                         Gain: 253
                   Saturation: 32
                 WhiteBalance: 8240
                 ExposureMode: 'auto'
                    Sharpness: 48
                   Brightness: 128
        BacklightCompensation: 1
                     Contrast: 32
  3. Preview the image. The size of the preview image is determined by the value of the resolution property. The preview window shows a live RGB image from the Webcam. The preview window also displays the camera name, resolution, frame rate, and the timestamp in seconds. Timestamp is the elapsed time since the object was created. To preview your image, call the preview function on the object name, which is cam in this example.

    preview(cam)
    

    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.

    The preview updates dynamically, so if you change a property while previewing, the image changes to reflect the property change.

  4. Set any properties that you need to change. For example, you might want to change the resolution.

    First you can see the resolutions your camera supports using the AvailableResolutions property.

    cam.AvailableResolutions
    
    ans = 
    
       Columns 1 through 6
    
          '640x480'  '160x90'  '160x100'  '160x120'  '176x144' '320x180'
    
       Columns 7 through 11
    
          '320x200'  '320x240'  '352x288'  '640x360'  '640x400'

    Change the resolution.

    set(cam, 'Resolution', '320x240');
    

    For information on which properties you can set for Webcams and how to set them, see Setting Properties for Webcam Acquisition.

  5. You can close the preview at any time using the closePreview function.

    closePreview(cam)
    

    If you do not explicitly close the preview, it closes when you clear the webcam object.

  6. Acquire a single image from the camera using the snapshot function and assign it to the variable img.

    img = snapshot(cam);
    
  7. Display the acquired image.

    imshow(img)

    The imshow function is part of the Image Processing Toolbox™. If you do not have that product, you can use the image function that is part of MATLAB.

    image(img)
  8. Clean up by clearing the object.

    clear('cam');

Acquiring Webcam Images in a Loop

The snapshot function acquires a single image from a Webcam. If you want to acquire images in a loop, you can do that with some extra programming.

This example uses MATLAB and Image Processing Toolbox to find circles in a video stream from a Webcam.

  1. Create a webcam object called cam, using a Logitech Webcam.

    cam = webcam('Logitech')
    
    cam = 
    
    webcam with properties:
    
                         Name: 'Logitech Webcam 250'
                   Resolution: '640x480'
         AvailableResolutions: {1x11 cell}
                     Exposure: -4
                         Gain: 253
                   Saturation: 32
                 WhiteBalance: 8240
                 ExposureMode: 'auto'
                    Sharpness: 48
                   Brightness: 128
        BacklightCompensation: 1
                     Contrast: 32
  2. Preview the image.

    preview(cam)
    
  3. Set any properties that you need to change. For example, you might want to change the brightness, if the camera supports that device-specific property.

    set(cam, 'Brightness', 150);
    

    For more information on what properties you can set for Webcams and how to set the properties, see Setting Properties for Webcam Acquisition.

  4. Create the loop and perform processing.

    for idx = 1:100
       % Acquire a single image.
       rgbImage = snapshot(cam);
    
       % Convert RGB to grayscale.
       grayImage = rgb2gray(rgbImage);
    
       % Find circles.
       [centers, radii] = imfindcircles(grayImage, [60 80]);
    
       % Display the image.
       imshow(rgbImage);
       hold on;
       viscircles(centers, radii);
       drawnow
    end
  5. Clean up by clearing the object.

    clear('cam');

Supported Functions for Webcam

You can use these functions with the MATLAB Webcam feature.

FunctionPurpose
webcamlistReturns list of Webcams that are connected to your system.

webcamlist

webcamCreates webcam object and connects to the single camera 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.

cam = webcam

For information on how to create the object with an input argument if you have multiple cameras connected, see Creating a Webcam Object.

previewPreview the images from the Webcam. Use name of object as input argument, which is cam in this example.

preview(cam)
snapshotAcquire a single image from the Webcam. Use name of object as input argument, which is cam in this example.

img = snapshot(cam);

closePreviewClose the preview window.

closePreview(cam)

set/getSet and get property values. Object name is the first argument, followed by property/value pairs, which can be strings or numerics. This example sets the camera's resolution to the value shown.

set(cam, 'Resolution', '320x240');

For more information on how to set object- or device-specific properties for the acquisition and what properties can be set, see Setting Properties for Webcam Acquisition.

Was this topic helpful?