Reading an Image From a Local Camera


We have just seen how we can load images from files stored on our computer into the MATLAB environment where we can begin to look at them and manipulate them, but for a robot it is really important that it is sensing the world as it is now. So information that is stored in a file may not be sufficiently up to date. We need to see the world as it is now: we need to be able to bring images in from a camera.

Now it is really fortunate today that almost all computers have one or more cameras attached to them. So what we are going to look at now is how we get information from a camera on my computer into the MATLAB environment.

So here we are in the MATLAB desktop workspace again and in order to grab an image from a camera we first create an object, a MATLAB object that is attached to the camera and in this case, it is the zeroth camera—that is the first camera that my computer has.

And it creates a new object in the MATLAB workspace, an object of type VideoCamera. As soon as this object has been created, the camera attached to my computer has become live; a little red recording light has come on. This object supports a number of methods, and the most important method for our purposes is the grab method, which grabs an image from the camera, the most recent image from the camera, and displays it.

We might be interested in actually putting the image into a workspace variable and we do it in this way. We assign the output of the grab method to a workspace variable and in this case, the variable im, and the image is grabbed from the camera and placed into that workspace variable.

If I call the function again it will take another picture and display it with my head in a different pose.

The camera is running continuously. In order to turn the camera off I need to destroy the camera object. You do that with a MATLAB clear command, so if I clear the object from the workspace the object is destroyed and the camera is turned off. The red light disappears.

Most computers today have a built-in camera. Let’s look at how we can grab images directly from such a camera and put them in the MATLAB workspace.

Professor Peter Corke

Professor of Robotic Vision at QUT and Director of the Australian Centre for Robotic Vision (ACRV). Peter is also a Fellow of the IEEE, a senior Fellow of the Higher Education Academy, and on the editorial board of several robotics research journals.

Skill level

This content assumes an understanding of high school-level mathematics, e.g. trigonometry, algebra, calculus, physics (optics) and some knowledge/experience of programming (any language).

More information...

Rate this lesson



  1. Nicola says:

    Even after lots of trying, I can’t get the code from the video to work on my macbook pro. In a forum post (https://groups.google.com/forum/?hl=en#!searchin/robotics-tool-box/videocamera$20mac%7Csort:relevance/robotics-tool-box/ZGrAR0LXuI8/o3_xUCCbphcJ) you noted “things are good and there is a solution”, however I can’t find it and would love to be directed to it. Many thanks!

    1. James Sergeant says:

      Hi Nicola, are you able to provide any information about the error messages you are receiving? There may be one of two solutions. Perhaps try omitting the 0:

      >> cam = VideoCamera();

      If this doesn’t work, try finding information about which webcams you have installed with the imaqhinfo command. I’m on Linux and the below is what I get returned:

      >> imaqhinfo

      ans =

      struct with fields:

      InstalledAdaptors: {‘linuxvideo’}
      MATLABVersion: ‘9.2 (R2017a)’
      ToolboxName: ‘Image Acquisition Toolbox’
      ToolboxVersion: ‘5.2 (R2017a)’

      I can then use:

      >> cam = VideoCamera(‘linuxvideo’);

      If you don’t have any adaptors listed, you may need to install the appropriate adaptors using Add-Ons.

      Please let me know if you are having any further trouble.


    2. Peter Corke says:

      Nicola. I’m also a Mac user. You can only load images if you have the MATLAB Image Acquisition Toolbox (IAT) installed. I did have some code that was a simple replacement for that but changes by Apple over the years have broken that. Under macOS Sierra there were issues with the IAT not having adapted to a more recent set of Apple changes.

      A less convenient workaround is to snap pictures with your phone and upload them to your laptop.

  2. Vaishnava Hari S says:

    Hello, What is opinion about Scilab as a replacemnt for Matlab, since is free and open-source?
    Since MATLAB IAT has to purchased seperately for student license….have been considering affordable alternatives

  3. Vaishnava Hari S says:

    Hello, What is ur opinion about Scilab as a replacement for Matlab, since is free and open-source?
    Since MATLAB IAT has to purchased separately for student license….have been considering affordable alternatives

    1. Peter Corke says:

      Great question, please post this question to tiny.cc/rvcforum, this is not really the right place to answer it.

Leave a comment