Documentation Center

  • Trial Software
  • Product Updates


Acquire single image frame from a Webcam




img = snapshot(cam); acquires a single image from the Webcam cam and assigns it to the variable img. The snapshot function returns the current frame. Calling snapshot in a loop returns a new frame each time. The returned image is always an RGB image. snapshot uses the camera's default resolution or another resolution that you specify using the Resolution property.


expand all

Acquire One Image Frame from Webcam

This example shows how to acquire and display one image frame from a Webcam.

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

ans = 

		 'Logitech Webcam 250'
		 'Dell Camera C250'

Create the object. 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 it 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.

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

See that it creates the object and connects to the Logitech Webcam.

Preview the image from the Webcam.


The preview window opens and displays 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.

Close the preview.


The preview window closes.

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

img = snapshot(cam);

Display the acquired image.


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


Clean up by clearing the object.

Was this topic helpful?