You are on page 1of 8

Matlab Lecture 2 http://www.aquaphoenix.com/lecture/matlab2/page2.

html

>> Lectures >> Matlab 2

Navigator

2.1 Introduction - Practial Foundation - Matrices


For a practical example of using matrices, we will now
look at and manipulate multidimensional data in the
form of images. Monitor screens and images are
inherently matrices with rows and colums, where each
element (pixel) can be addressed by a particular row
and column index.

Figure 2.1

Figure 2.2

1 of 8 3/16/2011 2:46 PM
Matlab Lecture 2 http://www.aquaphoenix.com/lecture/matlab2/page2.html

In Matlab, we can load a large number of image data


types, including JPG, BMP, GIF. We make use of the
function imread(), which takes as an argument the file
name of the image. The image file should reside in the
Current Directory, and its name must be enclosed in
single quotes, e.g.
.

To display the image we use function


, where cartman is the name of the
variable that the loaded data has been ascribed to.

Figure 2.3
Click image to enlarge, or click here to open

The displayed image is not displayed in its original size,


but either enlarged or shrunken. In order to display the
image in its true size, we use issue the following
commands:

... where 300 is


the width of the image and 193 its height.

Note: You are NOT required to use this for


assignments!To find out the size of the loaded image
data, we use function . Notice that
size() returns 3 values: height, width, and layers
(discussed below).

Figure 2.4
Click image to enlarge, or click here to open

Using the general matrix indexing conventions, we can


extract a portion of the image and display it:
where
[170:193] is a range of rows to extract, and the 2
following colons (:) denote "all columns", and "all
layers", respectively.

Figure 2.5
Click image to enlarge, or click here to open

2 of 8 3/16/2011 2:46 PM
Matlab Lecture 2 http://www.aquaphoenix.com/lecture/matlab2/page2.html

Figure 2.6 shows another example of sub-matrix


extraction:
. Here we extract a specific range
of rows AND columns, while still extracting all layers.

Figure 2.6
Click image to enlarge, or click here to open

Multiplying the image matrix by a scalar greater than 1


increases the combination of brightness and contrast of
the image:

Figure 2.7
Click image to enlarge, or click here to open

We can expand
Multiplying the one-line
the image data expression
by a number to make
less itthan
more
1
understandable:
decreases brightness/contrast of the image.

While the point of this series of statements is to merely


multiply the image data by a scalar, the first and third
lines are necessary to format the data in a particular
way. While we will not go into the details of what these
data types are, we note that raw image data cannot be
manipulated by conventional operators, such as
multiplication or addition. We must convert the data
type from uint8 to double, then manipulate the data,
and finally convert it back from double to uint8.

To summarize: Line 1 converts the image data to type


double; Line 2 increases brightness/contrast; Line 3
converts the data back to uint8; and Line 4 displays Figure
Figure 2.8
2.9
Click image
Click image to enlarge, or
to enlarge, or click
click here
here to
to open
open
the image.

3 of 8 3/16/2011 2:46 PM
Matlab Lecture 2 http://www.aquaphoenix.com/lecture/matlab2/page2.html

Figure 2.10 shows an example in which a portion of the


image matrix is replaced. In this example, the
replacement sub-matrix comes from the same image
matrix, only that its brightness/contrast is increased:

We have already seen that the left side of this


expression denotes the lower portion of the image. On
the right side we index into the same portion of the
image matrix and multiply the data by a scalar, which
increases the brightness.

Figure 2.10
Click image to enlarge, or click here to open

As shown in Figure 2.4, returned 3


values, one of which denotes layers. These 3 layers are
in fact 3 images of their own, representing red, green,
and blue values for each pixel in the image. The
combination of the 3 generates a full color image. How
colors are mixed is shown in Figure 2.12.

Note: The following discussion regarding colormap and


colorbar is meant to explain some general concept
behind images and colors. You are NOT required to
reproduce this in you homework assignments!

Figure 2.11
Click image to enlarge, or click here to open

4 of 8 3/16/2011 2:46 PM
Matlab Lecture 2 http://www.aquaphoenix.com/lecture/matlab2/page2.html

Figure 2.12

We can extract each of these layers by using their


respective indices ( Red = 1, Green = 2, Blue = 3, also
called RGB). The image cartman needs to be reloaded
first because it has been altered in a previous step:
To view the
red intensity image and a corresponding intensity color
bar, we issue:
The first colon denotes all rows, the
second colon all columns, and the third index denotes
the color Red.

The default colorbar has a scale of 1 to 64, where 1 is a


low intensity and 64 is a high intensity.

Figure 2.13
Click image to enlarge, or click here to open

5 of 8 3/16/2011 2:46 PM
Matlab Lecture 2 http://www.aquaphoenix.com/lecture/matlab2/page2.html

Not only is it difficult to use this rainbow-colored scale,


but it is also incomplete, because color intensity for
full-color images ranges not from 0 to 63, but 0 to 255.
We thus modify the color scale and re-display the
image:

The resulting image


shows the intensity of red color in the original image
data.

Figure 2.14
Click image to enlarge, or click here to open

Figure 2.15 shows the image's green intensity values.

Figure 2.15
Click image to enlarge, or click here to open

Figure 2.16 shows the image's blue intensity values.

Figure 2.16
Click image to enlarge, or click here to open

6 of 8 3/16/2011 2:46 PM
Matlab Lecture 2 http://www.aquaphoenix.com/lecture/matlab2/page2.html

We can also transpose the individual layers of the


image, such as the red layer, as shown in Figure 2.17.
While it may appear that the transpose operation
rotated the image 90 degrees, it also flipped the image
at the same time. You may wish to think about how this
happened.

Figure 2.17
Click image to enlarge, or click here to open

Finally, we can also display the intensity values in a 3D


graph. While we will discuss meshing in a later lecture,
it may be interesting to look at a 3D intensity plot of an
image even now:

In this example,
we are examining layer 3 = blue. Before meshing the
image, we convert it to data type double.

Figure 2.18
Click image to enlarge, or click here to open

The 3D mesh can be rotated interactively using the


rotate button.

7 of 8 3/16/2011 2:46 PM
Matlab Lecture 2 http://www.aquaphoenix.com/lecture/matlab2/page2.html

Figure 2.19
Click image to enlarge, or click here to open

8 of 8 3/16/2011 2:46 PM

You might also like