Lab (1) - Image Processing using MATLAB

Objectives:
In this lab you will learn about:
Simple Image Processing using Matrix Manipulations

Please DO NOT Print this lab as it will not appear correctly

Preparations:
Please  save your files and images under My Document folder on your PC.  Also, you need to open a MS Word file so you can keep your results in that file.  Save that file also under the same directory.  You can simply copy images from MATLAB screen to MS Word.  To copy a MATLAB screen shot into the MS Word file, first click on the screen you wish to copy, then hold down the Alt or Ctrl key (there is small difference between the two), and press PrntScrn key at the same time.  This copies the screen shot into the buffer, you now need to go to MS Word screen and paste it either by using the Edit option or by pressing Ctrl V.  Make sure the cursor is at the location you want to copy the image.  Also it is possible to save MATLAB images in different image formats.  For this use the option Save As under File to save the files.

Before getting to the first image processing part let's have a quick recap of some definitions and concepts.

## Arrays (Vectors)

A vector is what we usually refer to as a one-dimensional array (1-D array).  The array can be of different types.  Here, we assume that an array holds real or integer values.  A signal can be viewed as a 1-D array.  The 1-D array representing a signal holds the discrete values corresponding to the amplitude at different times.  For example: a signal with a sin shape is shown on Figure (1).  The signal is generated for the time period of 0 to 450 with a 22.5 step size.  Following you will see the values for t followed by the corresponding values of sin(t).

Values for t:
0   22.5   45.0   67.5   90.0  112.5  135.0  157.5  180.0  202.5  225.0  247.5  270.0  292.5  315.0  337.5  360.0  382.5  405.0  427.5  450.0

Corresponding sin(t) values:
0    0.3827    0.7071    0.9239    1.0000    0.9239    0.7071    0.3827    0.0000   -0.3827   -0.7071   -0.9239   -1.0000   -0.9239   -0.7071   -0.3827   -0.0000    0.3827  0.7071    0.9239    1.0000

Figure (1) - Sin (t) function

As you may have noticed, vector t consists of a set of numbers (1-D array).  Thus, sin(t) creates another array consisting of sin values corresponding to those t values.  In MATLAB we can create vector t by typing at the command prompt (>>) all the values and by storing them in variable t:

>> t = [0   22.5   45.0   67.5   90.0  112.5  135.0  157.5  180.0  202.5  225.0  247.5  270.0  292.5  315.0  337.5  360.0  382.5  405.0  427.5  450.0]

This will generate an output that looks like this:
t =
Columns 1 through 6
0   22.5000   45.0000   67.5000   90.0000  112.5000
Columns 7 through 12
135.0000  157.5000  180.0000  202.5000  225.0000  247.5000
Columns 13 through 18
270.0000  292.5000  315.0000  337.5000  360.0000  382.5000
Columns 19 through 21
405.0000  427.5000  450.0000

Or we could create these values using the method we described in Lab (1):
>> t = 0: 22.5 : 450

There are many built-in functions in MATLAB, for example: sin, cos, sqrt, .... We can also build our own functions.  In the following example we will compute sin(t) for all t values. To do this, at the prompt, type y = sin(t).  Note that since t is an array, then y will be an array that holds exactly the same number of elements as array t.  I.e., for each t at an arbitrary time,  ti, we have a yi = sin(ti).   We can compute the sin at all these times using:
>> y = sin(t*pi/180)

y =
Columns 1 through 6
0    0.3827    0.7071    0.9239    1.0000    0.9239
Columns 7 through 12
0.7071    0.3827    0.0000   -0.3827   -0.7071   -0.9239
Columns 13 through 18
-1.0000   -0.9239   -0.7071   -0.3827   -0.0000    0.3827
Columns 19 through 21
0.7071    0.9239    1.0000

A quick note:  All angles must be represented in Radian.  In order to convert an angle, t, to Radian, we use (t*pi/180).  For example: sin(45) = 0.7071, and in MATLAB you have to type sin(45*pi/180) to get the correct answer.

Manipulation of arrays:
Suppose, we want to compute sin(2t).  Notice that you want to do this for all t values.  In MATLAB you compute 2t simply by multiplying t by 2.

>> z = 2*t

Now z has the 2t values stored in it:
>> y2 = sin(z)

Also let's calculate y10 = sin(10t)
>> y10 = sin(10*t)

Assignment (1)
Plot the sin(t), sin(2t), and sin(10t) all on the same graph.  Note that you have stored the values of sin(t) in y, those for sin(2t) in y2, and the ones for sin(10t) in y10.  Make your observations.
How did multiplying by 2 changed the behavior?

Reading and Writing Images in MATLAB

This is the MATLAB command that reads an image (football.jpg):

The image data is stored as the matrix f.  Use the:

>> whos f

To display the image (image data), type:
>> imshow(f)

To write an image to your hard disk, you can use a command that looks like one of these two:
>> imwrite(f, ‘football’, ‘tif’)  Or
>> imwrite(f, ‘football.tif’)

Note that we are writing the images in a different format (tif) than the initial format (jpeg).

Assignment (2)
Use the matrix manipulation techniques to scale this  image by  2 (basically multiply the image data by 2), then display the original image and the new one to see if they are different.  Include all the commands and the resulting images in your file.  Note that to display both images you need to use something like this:
>> imshow(f)    %this displays the first image
>> figure, imshow(nuf)  %this displays the second image too

Include the results in your MS Word file.

Assignment (3)
When you save images using the imwrite command you can changed the image quality.  Check the help to learn how you can change the quality when you use imwrite to save your image.  Then, write the original football image with 25% and 50% quality in JPEG format.   Once done, read the original image and the two newly stored images and display them.  Include these there images in your MS Word file.

Assignment (4)
Right click on the following two brain MRI images and save them in your working directory.

Read the images in MATLAB and display them.  Include the MATLAB display screen in your MS Word file.

Check the size of these two images.  Are they 256-by-256 in size?

I want you to use MATLAB to play with these images.  One thing you can do is ot brighten the images by multiplying them by a positive constant larger than or darken them by multiplying them by a positive constant less than 1.

Thresholding

Thresholding is a simple technique used in feature extraction or pattern recognition.  The idea is to have the intensity values smaller than a threshold value set to 0  and anything above it to 1.   Let's look at the example below.

myImage = [ 23 120 34 255  4  120 200  200
120  87 62 120 23  4  0 87
23  4  0  0  120  200  200  120
23  62  250  0 120 200  34  62
23 120 34 255  4  250 200  200
120  23  62 120 23  4  0 87
23  4  0  0  120  200  255  120
23  255  250  0 120 200  34  62]

Suppose I use a threshold value of 100.  Then this matrix will be converted to:

T100 =
0     1     0     1     0     1     1     1
1     0     0     1     0     0     0     0
0     0     0     0     1     1     1     1
0     0     1     0     1     1     0     0
0     1     0     1     0     1     1     1
1     0     0     1     0     0     0     0
0     0     0     0     1     1     1     1
0     1     1     0     1     1     0     0

I obtained this matrix using a simple MATLAB command:

>> T100 = myImage > 100

Of course I can turn this to the intensity values as they had appeared in the original image by multiplying T100 by the myImage:

T100Image = T100 .* myImage

T100Image =

0   120     0   255     0   120   200   200
120     0     0   120     0     0     0     0
0     0     0     0   120   200   200   120
0     0   250     0   120   200     0     0
0   120     0   255     0   250   200   200
120     0     0   120     0     0     0     0
0     0     0     0   120   200   255   120
0   255   250     0   120   200     0     0

Assignment
Compute the threshold images for the two left and right images I have given above.  Here is what you need to do:

A) Use a threshold value of 50 and create the thresholded images of the above images.  Show your results in your MS Word file
B) Use a threshold value of 100 and create the thresholded images of the above images.  Show your results in your MS Word file
C) Use a threshold value of 180 and create the thresholded images of the above images.  Show your results in your MS Word file
D) Use a threshold value of 220 and create the thresholded images of the above images.  Show your results in your MS Word file

What did you learn from this experiment.  What kind of thing can you detect with theresholding?