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.
High school mathematics
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).
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!
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:
struct with fields:
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.
I dont have any adaptors instaled, what adaptors should I instal? I have the problem with command cam = VideoCamera(0) and VideoCamera(), nothing works… I have both toolboxes installed and this is so frustrating
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.
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
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
Great question, please post this question to tiny.cc/rvcforum, this is not really the right place to answer it.
undefined function or variable VideoCamera
I am unable to support the VideoCamera function across all platforms, sorry.
I have a problem with cam= VideoCamera(0) it shows error in that line of code for some reason. I have many many problems with this Machine Vision Toolbox and also with RoboticsToolbox, it is soo frustrating and demotivating. I type a same code as Profesor Peter does and for some reason it wont work.
For Windows users (of which I assume there are many), you need to install the “MATLAB Support Package for USB Webcams” from matlab addons.
The documentation will tell you how to use its functions but the basic ones that emulate what happens in this video are:
webcamlist – Will show you the name you can use to interact with your computers camera
cam = WebCam(‘YOUR_WEBCAM_NAME’) – Will load your webcam into the workspace like in the video
preview(cam) – Will show you a live preview of your webcam
img = snapshot(cam) – Will load the current frame from your webcam into ‘img’