This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Acquire 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

    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 for the R2018a release.

  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.

    cam.Brightness = 150;
    

    For more information on what properties you can set for webcams and how to set the properties, see Set 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');

For more information on creating a webcam object and acquiring single snapshots, see Acquire Images from Webcams. For a list of the functions you can use with the webcam support, see Supported Functions for Webcam.

See Also

| |

Related Topics