We are going to talk now a little bit more about how we access the elements of the matrix which represents an image from inside the MATLAB environment. And we are going to do this interactively from the command line tool.
We will start by loading an image, one that we have seen a few times before. It is the Mona Lisa image, has a PNG extension, and we are going to convert it from colour to grey scale because we have not really talked about colour yet. And we have loaded the image into our workspace, and I can use the toolbox command about, to tell us a bit more about that particular image.
It tells us that the image variable is of the type uint8, that it is 700 pixels wide and 677 pixels tall, and occupies 470 kilobytes of memory.
What I am going to do now is to display the image and again these commands should be pretty familiar to you. And there is the Mona Lisa image. And as we discussed before, we can move around, we can click pixels and we can look at the grey scale value. And we can click a pixel there, in the middle of her eye. And it has got quite a low value. It is in that dark part of her eye.
Now, we access that variable directly from the MATLAB environment, and I am going to choose the row coordinate which is the vertical coordinate in the image, that is 194 and the column coordinate which is a horizontal coordinate 276. So remember we talked about this before: we have to reverse the coordinates from the u,v notation, horizontal and vertical coordinate system to MATLAB’s row and column index for matrices, and there we see that same value, 26, that is a low grey scale value, a dark value which represents the intensity of that pixel within her eye.
Another thing we can do with MATLAB is to access not just a single pixel, but in fact a block of pixels, and we do that using MATLAB’s colon operator. So let’s choose all rows between 180 and 210, and all columns starting at 260, up to 290. When I hit enter, we are going to see a large number of pixel values scroll past. So I can roll back up and look at all of them there.
So it has returned, in this case a matrix. A small matrix; a sub-matrix of the original image.
Let’s actually put that into a workspace variable, and I will do that by coming back and editing that command line.
There we go. So I have created a new variable workspace called im2 and it is a 31 by 31 pixel image. And I can display that. And we see an imagem, which is actually the eye of the Mona Lisa.
This time instead of selecting a block of pixels, I am going to choose a line of pixels. I am going to do that by putting the result into a variable called lin, and this is the MATLAB variable that contains the original image. I am going to select row 190 and all pixels in columns between 260 and 290. And here they are, these are the pixel values along a short horizontal segment of the Mona Lisa image. And I can plot that.
And here we see an intensity profile. Starts off quite light, falls down the middle as we go through the dark part of the Mona Lisa’s eye, and then go to larger greyscale values as we move back onto the pixels that comprise her face.
There are a number of other interesting and useful tricks we can do with a colon operator. I am going to create another output image. This time I am going to call it im3 and what I am going to do is select again the Mona Lisa image and I am going to choose all the rows starting at 1 in steps of 4 up to end, which is a special MATLAB variable which represents the largest row number in the matrix. And select all the columns again, starting at 1 in steps of 4 up to the largest column number and create a new workspace variable called im3 and I am going to display that image. Here we go.
And what we can see now is that this image has much lower resolution, we can see the blockiness of the pixels, we can see in the horizontal and vertical directions that there are far fewer pixels in this image than there are in the original and that is because what we have done is chosen every fourth pixel from both the horizontal direction and in the vertical direction. We have reduced the resolution of the image. So that is a quick trick you can do with MATLAB, using the colon operator.
The final thing that is useful to know about the colon operator—I am going to create yet another image, and this time what I am going to do is I am going to choose the rows starting at end, starting at the biggest possible row number, going in steps of -1 working downwards, until I get to row 1, and then I am going to choose all of the columns from one up to end.
You can probably imagine what this is going to do.
And there we go—we have flipped the Mona Lisa upside down. One final quick tip is that the last command that we typed; we can actually make it a bit more concise. We need to write that last part where we say all of the columns from 1 to end, we can write that down very simply just as colon. So where ever you have colon to represent a column or row of a matrix, it means all. We can think of it as a wild card operator.
For an image stored as a variable in the MATLAB workspace let’s look at how we access the values of individual pixels in an image using their row and column coordinates. Using the MATLAB colon operator we can extract an intensity profile, extract a submatrix which is a region of the image, flip the image or reduce its resolution.
This content assumes an understanding of high school level mathematics; for example, trigonometry, algebra, calculus, physics (optics) and experience with MATLAB command line and programming, for example workspace, variables, arrays, types, functions and classes.