how to capture an image using usb webcamera in matlab

Photo of author

By Jackson Taylor

Home » Software » how to capture an image using usb webcamera in matlab

As an Amazon Associate we earn from qualifying purchases.

Capturing a picture from a USB webcam in MATLAB takes only a few lines of code. Once you have the image as a variable, you can show it, save it or pass it to any image processing routine. This guide walks through the full process: installing the webcam support, finding your camera, previewing the video, taking a snapshot and saving it to disk.

The video below shows the process in action, and the steps that follow explain each command.

What you need

  • A computer with MATLAB installed.
  • A USB webcam (a built-in laptop camera usually works too).
  • The MATLAB Support Package for USB Webcams, a free add-on.

Step 1: Install the USB webcam support package

MATLAB does not talk to webcams out of the box. You need the support package first.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video
  1. On the Home tab, click Add-Ons and choose Get Add-Ons.
  2. Search for MATLAB Support Package for USB Webcams.
  3. Click Install and sign in with your MathWorks account if asked.

You only need to do this once per MATLAB installation. Plug in your webcam before you start the steps below so the system has already detected it.

Step 2: Check that MATLAB can see the camera

In the Command Window, type:

webcamlist

This returns the names of the webcams MATLAB found, for example a built-in camera and a USB one. If the list comes back empty, see the troubleshooting section further down.

Step 3: Create a webcam object

Connect to the camera by creating a webcam object. You can use the camera’s position in the list or its name:

cam = webcam(1);

or

cam = webcam(‘name of your camera’);

If you leave out the argument and only one camera is connected, MATLAB uses that one. When a laptop has a built-in camera as well, the USB webcam is often number 2, so check the order from webcamlist.

Step 4: Preview the video

Before taking a picture, it helps to see what the camera sees and frame the shot:

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
See also
CAPTURING AN IMAGE FROM CAMERA WHEN KEY 0 PRESSED

preview(cam);

A preview window opens with the live video. Close it with closePreview(cam); when you are done.

Step 5: Capture the image

The snapshot function grabs a single frame and returns it as an image array:

img = snapshot(cam);

The variable img is normally a height-by-width-by-3 array of 8-bit values (RGB). Its exact size depends on the resolution the camera is set to.

Step 6: Show and save the image

To look at the result, use:

imshow(img);

To write it to a file in the current folder:

imwrite(img, ‘capture.jpg’);

You can use other formats such as PNG by changing the file extension. If you want more detail on the display side, see our guide on displaying an image in MATLAB.

Step 7: Release the camera

When you finish, remove the webcam object so other programs can use the camera:

clear cam;

Complete example script

Here are the commands in order. Paste them into a script and run it:

  1. cam = webcam(1);
  2. preview(cam);
  3. pause(3); (gives the camera a moment to adjust exposure)
  4. img = snapshot(cam);
  5. closePreview(cam);
  6. imshow(img);
  7. imwrite(img, ‘capture.jpg’);
  8. clear cam;

The short pause matters more than it looks. Many webcams adjust brightness and focus after they start, so the very first frame can look dark or soft.

The Ultimate Money-Saving DIY Skill

From power tools and solar banks to your car, batteries are the most expensive part of your gear. Master the step-by-step DIY system to bring dead batteries back to life.

Get the Step-by-Step Guide

Changing the resolution and other settings

A webcam object has properties you can read and change. To see which resolutions your camera supports, type:

cam.AvailableResolutions

Then set one of them, for example:

cam.Resolution = ‘640×480’;

Set the resolution before you start the preview or take the snapshot. Other properties, such as brightness, contrast or focus, appear only if your camera supports them. Typing cam without a semicolon lists everything available for your device.

Capturing several images in a row

To take a series of pictures, put the snapshot inside a loop and give each file a different name:

  1. for k = 1:5
  2. img = snapshot(cam);
  3. imwrite(img, sprintf(‘image_%d.jpg’, k));
  4. pause(1);
  5. end

This saves five files, image_1.jpg to image_5.jpg, one second apart.

See also
NEGATIVE VOLTAGE TO POSITIVE VOLTAGE BOOST CONVERTER- SIMULATION IN MATLAB (-12V DC TO +48V DC)

What to do with the captured image

After capture, the image is just a matrix, so everything in MATLAB’s toolset applies. A common first step is converting RGB images to grayscale before filtering, thresholding or edge detection. For a wider look at what you can build from here, see image processing in MATLAB. If you prefer another language, you can also capture images with Python and OpenCV.

Troubleshooting

  • webcamlist is empty: Check that the camera is plugged in and works in another app. Reconnect it, restart MATLAB and confirm the support package is installed.
  • Camera is busy or cannot be opened: Another program, such as a video call app, may be using it. Close that program and run clear cam before trying again.
  • Dark or blurry first frame: Add a short pause after creating the object, or take a few snapshots and keep the last one.
  • Wrong camera: Use the name from webcamlist instead of the index number.
  • Resolution error: Pick a value from cam.AvailableResolutions exactly as it is written.

Frequently asked questions

Do I need the Image Acquisition Toolbox to use a USB webcam in MATLAB?

No. The free MATLAB Support Package for USB Webcams provides the webcam, preview and snapshot functions on its own. The Image Acquisition Toolbox is a separate product with more advanced features, such as the videoinput interface and support for more kinds of hardware.

How do I save the captured image?

Use imwrite with the image variable and a file name, for example imwrite(img, ‘capture.jpg’);. The file is saved in MATLAB’s current folder unless you give a full path.

Can I capture video instead of a single image?

Yes. Call snapshot repeatedly in a loop and display each frame with imshow or image. For simply watching live video, preview is simpler and faster.

Why does MATLAB say no webcams are available?

Usually the support package is missing, the camera is not detected by the operating system, or another application is using it. Work through the troubleshooting list above, starting with the cable and the support package.

See also
FINGER PRINT BASED STUDENTS MONITORING SYSTEM IN REAL TIME ENVIRONMENT

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works