Let’s load a color image into our workspace.

We’ll put it into the variable called im, and the image that I’m going to use is called flowers8 and it’s a png format file. So this is one of the files that’s distributed with the toolbox. We have loaded it into the workspace.

Now once again this is an unsigned 8-bit integer image. We can see that the image belongs to the class uint8. The dimensions of this image are interesting compared to what we’ve seen before. We see that this one has got 426 rows, 640 columns and an extra dimension with a value of 3. We refer to the second dimension as the plane, and that will become clear shortly.

First thing we’re going to do is to display the image and we see a picture containing a number of colored flowers. Now if I click on some pixel values—I’m going to click here on this red flower—we see that it has got a pixel value which has three components. The pixel value is actually a vector, and those numbers indicate the amount of red, the amount of green, the amount of blue. So I’ve just clicked on a red flower and we see that the red amount is 176, and this is quite significant. Remember, 8-bit unsigned integers go from a minimum value of 0 to a maximum value of 255, so this pixel has got quite a lot of red in it. It’s got almost no green, and it’s got almost no blue. Let’s move over to a pink flower. This is a bit closer to white, and we see that this pixel has got a significant amount of red, and a fair amount of green, and a fair amount of blue.

Let’s go and have a look on a green leaf here, and we see that the green value is the biggest one, but there is also an amount of red and blue there, but dominated by green.

There are some tiny little blue flowers so I’m going to zoom in on this part of the image, and click on one of those blue flowers, and as we would expect, the blue value dominates here.
Let’s have a look at one of these image planes. I am going to display the image. I’m going to display all rows and all columns, but only those pixels that belong to plane number 1. And there we have it. This is now a grey scale image. The color has all gone and this is the red plane, the first plane. And if I look at the values here we see that they are quite bright where there was red in the image, and they’re very dark where there was no red in the image. So it will be dark in the green leaves, it will be dark on the blue flowers. We can look at the green plane, that’s plane number 2. We can see that this looks quite different. The red flowers are quite dark—that’s because they don’t have any green in them—whereas the leaves are looking a little bit brighter than they were before, and the pinky white flowers are also looking reasonably bright because the white flowers have got a mixture of red and green and blue.

Finally, we can look at the blue plane, that’s the third plane. And here we see that the little blue flowers around the bottom are now looking quite bright. The white flowers as I mentioned, pinky white flowers as I mentioned before, are also looking reasonably bright because they contain a fair amount of blue. The green leaves are also looking quite dark because there’s not much blue being reflected from those leaves.

We can consider a color image then as comprising three planes. We refer to those as the red, green and blue planes. Each plane we can consider to be a grey scale image, where the pixel value corresponds to the amount of that primary color at that particular pixel location. So if we look at these white flowers here, we see that they are bright in the red, green and blue images. That means that those white flowers contain a lot of red, and a lot of green and a lot of blue.

By contrast, the red flowers are quite bright in the red image, but they’re very, very dark in the green and blue images. So the red flowers comprise a lot of red color, as you would expect, not much green and not much blue. We can see that the yellow flowers are bright in the red and green images; that is, they reflect red light and green light, but they do not reflect blue light.

If we consider our color image as a stack of planes, red green and blue, imagine them stacked up one above the other. So when it comes to describe a pixel in a color image now, we describe the pixel at the coordinate u,v and this is what we’ve done previously; u is the horizontal coordinate, v is the vertical coordinate. And now what we’re going to do is drill down through this stack of images. So this is the red value, then we find the green value, and then we find the blue value. So at the coordinate u,v the pixel value is actually comprised of three numbers. It is a three-element vector: contains a red, green and blue component.

We’ve previously looked at the way we index into a matrix in MATLAB, and in MATLAB we represent a matrix with two coordinates. The first coordinate is the row number, and the second coordinate is the column number. In a color image it’s a three-dimensional matrix as far as MATLAB is concerned, and again we use this analogy of a stack of planes. So again, the first index is the row number, the second index is the column number and the third index is what we call the plane number. It’s which sheet in the stack that we are referring to. And there’s a caution as I’ve mentioned in an earlier lecture: when we are talking about indexing into an image in MATLAB, we use the row followed by the column. When we refer to an image we generally refer to the horizontal coordinate, then the vertical coordinate, and they are transposed; they are swapped. So be careful with that.

We can do this programmatically; that is, from the command line. So we have our image. Let’s look at the value of the pixel at coordinate 100,100, and we want to look at all of the planes. We get this rather strange result from MATLAB. It has given use three numbers, as we would expect: the amount of red, green and blue, but in a rather inconvenient form. We actually have to use the MATLAB operator squeeze when we do this, to remove two of what MATLAB refers to as singleton dimensions. So if we do that now we have a vector value for this particular pixel.


There is no code in this lesson.

Let’s learn how to import a color image into MATLAB and see how the data is organized as a matrix with three dimensions.

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; for example, trigonometry, algebra, calculus, physics (optics) and experience with MATLAB command line and programming, for example workspace, variables, arrays, types, functions and classes.

More information...

Rate this lesson


Check your understanding

Leave a comment

Previous lesson Next lesson