>>  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
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. cartman=imread('cartmancop.jpg') .

To display the image we use function image(cartman) , where cartman is the name of the variable that the loaded data has been ascribed to.

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:

figure('Units','pixels','Position', [100 100 300 193]); image(cartman) set(gca,'Position',[0 0 1 1]); ... 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 size(cartman) . Notice that size() returns 3 values: height, width, and layers (discussed below).

Using the general matrix indexing conventions, we can extract a portion of the image and display it: image(cartman([170:193],:,:)); where [170:193] is a range of rows to extract, and the 2 following colons (:) denote "all columns", and "all layers", respectively.

Figure 2.6 shows another example of sub-matrix extraction: image(cartman([1:130],[45:200],:)); . Here we extract a specific range of rows AND columns, while still extracting all layers.

Multiplying the image matrix by a scalar greater than 1 increases the combination of brightness and contrast of the image: image(uint8(double(cartman(:,:,:)) * 2));
We can expand the one-line expression to make it more understandable:

cartmanTemp = double(cartman(:,:,:)); cartmanDarker = cartmanTemp * 2; cartmanTemp = uint8(cartmanDarker); image(cartmanTemp); 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 the image.

Multiplying the image data by a number less than 1 decreases brightness/contrast of the image.

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:

cartman([170:193],:,:) = uint8(double(cartman([170:193],:,:))*2); 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.

As shown in Figure 2.4, size(cartman) 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.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: cartman=imread('cartmancop.jpg') To view the red intensity image and a corresponding intensity color bar, we issue: image(cartman(:,:,1)), colorbar; 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.

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:

image(cartman(:,:,1)), colormap([[0:1/255:1]', zeros(256,1), zeros(256,1)]), colorbar; The resulting image shows the intensity of red color in the original image data.

Figure 2.15 shows the image's green intensity values.

Figure 2.16 shows the image's blue intensity values.