LESSON

# Getting an Image Into MATLAB

#### Share

#### Transcript

Here we are inside the MATLAB desktop environment. I have set the working directory to the folder that contains those holiday snaps.

So what we are going to do is load the image we were just looking at into the MATLAB workspace.

I need to put that data into a workspace variable called im. That is kind of my personal short hand for image. We use the iread function. Short for image read. And we put in the rather cryptic name of the file that contains the image. And we therefore load the picture into this workspace variable called im. Another toolbox function allows us to display a variable as an image.

And here we can see it; here we can see the crocodile displayed from inside the MATLAB environment. The image file contains not just the pixel information that we are looking at here; it also contains what we call the metadata. So let’s go back and see if we can find the metadata. We will close that image and we will modify our reading command so that it returns two output variables.

The pixel data in the variable im and the metadata into another variable called meta. There we go. And display that variable called meta. And we can see interesting information here about the image, we can see the date that it was taken, we can see that it was taken with a Panasonic camera and we can see the model of the camera. There is even more detail information in this sub-structure. And here we can see the exposure time and the F number of the camera when I took that particular picture and we can see that the flash didn’t fire and lots of other information as well.

But we are getting ahead of ourselves, we have been dealing with a colour image and colour is a topic that we are not going to visit for another couple of weeks. So let’s start with something a little bit simpler.

We are going to look at what some people call a black and white image, but what is more correctly known as a greyscale image. So what I am going to do here is to clear all the variables in the workspace, I am going to clear the console. And I am going to load a new image and this is an image that is shipped with the software, it is an image of a street in Paris and its file has got an extension of PNG which stands for portable network graphic. It’s just another type of image file format.

Load that into the workspace into the variable im. Now we can see over here the information about the variable. It says that it is 851 rows by 1280 columns. MATLAB is considering this as a matrix. There is a few interesting things about this matrix. MATLAB provides a facility to examine the elements of a matrix. We do that by right clicking on the variable, and let’s maximise the view.

And here we can see a small, sub-section of all the numbers that are within this very, very large matrix. It is a spreadsheet style display, it’s got scroll bars and we can roll down and see lots and lots of numbers. And you will see they’re all integer numbers. You will see that there are no negative ones and you will also notice that there are none bigger then 255.

Another thing to notice is that this variable has got a type and that type is uint8 and that is short hand or code for unsigned integer eight bits. So all the numbers in this variable im are eight bit unsigned integers, which means the smallest value they could have is 0 and the greatest value that they can have is 255.

The MATLAB variable im contains a lot of numbers, but they originated from an image so the numerical information within that variable represents visual information. So let’s display it as an image rather then display it as numbers and we use the function we used earlier, the idisp function parse in the image. And here we see a picture of a Parisian street.

Now what we can do is we can examine the various pixels that comprise this image. So let’s have a look at a pixel over here and if I click on that, it displays three pieces of information. The first piece of information is the coordinate of this particular pixel and the coordinate is a 105—that is the horizontal coordinate—and 87, which is the vertical coordinate. Now the image processing the convention is that the horizontal coordinate, I refer to that generally as the variable u, increases from left to right and the vertical coordinate which I generally refer to as v increases from the top to the bottom. The other piece of information that is displayed here is what is called the greyscale value. That is the numeric value of the pixel at that particular coordinate and in this case it has a value of a 194.

And we said that the pixel values can vary between 0 and 255. Convention is that a low number is dark and a high number is bright. 255 being the largest value corresponds to bright white; 0, the smallest value, corresponds to the darkest dark. This particular value—a 194—is fairly bright. If I click a value in this dark doorway we see it has got a value only of 12. Look down on the shadow of the street, we see it has got a value of 66. So the pixel values are reflecting the intensity of light at this particular point in the scene.

We can also get access to this pixel value by addressing the element within the matrix directly. The matrix is the workspace variable im and we need to provide first of all the row coordinate, which is 653, and we need to provide the column coordinate, which is 562. We access that element of the matrix and we see the grayscale value of 66. Now notice what has happened here is that the indices have been reversed. When we index into a matrix we provide the row coordinate first then the column coordinate. Typically, when we refer to the coordinate in the image itself, we write the horizontal coordinate first and then the vertical coordinate.

#### Code

If we want to process images the first thing we need to do is to read an image into MATLAB as a variable in the workspace. What kind of variable is an image? How can we see the image inside a variable? How do we refer to to individual pixels within an image.

### Skill level

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).

### Rate this lesson

## Discussion

## Leave a comment

Please Sign In to leave a comment.

Hey, great job. I love your book and toolboxes that you have made in MATLAB.

I have a problem reading images with “iread”. I receive this error:

iread(‘drag.png’);

Error: File: iread.m Line: 409 Column: 9

The input character is not valid in MATLAB statements or expressions.

Can you help me with that, please?

Thanks

You have somehow got odd characters instead of the single quote. This sometimes happen if you cut and paste text into MATLAB.

I have the same problem as above. Haven’t copy pasted the name. Please help!

You are getting exactly the same error message?

is it iread or imread?

`imread`

is a function provided by MATLAB that reads an image.`iread`

is a function in my Machine Vision Toolbox that does the same thing, but has a lot of useful extra options, it actually calls`imread`

.I get the same error :

iread(‘drag.png’);

Error: File: iread.m Line: 409 Column: 9

The input character is not valid in MATLAB statements or expressions.

I hope that you can help me!

I think the images and their folders are coded with( ” ” ) instead of (‘ ‘). I have changed that manually inside the function and it worked very well.

Probably the signs for (strings) inside the code do not belong to MATLAB and belong to Python.(Just a suggestion Mr. Paul with all respect)