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.