Lab (1) - Image Processing
Introduction to Matrix Manipulation in MATLAB

Objectives:
In this lab you will learn about:
Arrays and their operations in MATLAB
Matrices and their operations in MATLAB
Linear Algebra in MATLAB
Simple Image Processing using Matrix Manipulations

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

Preparations:
You will complete some exercises during the lab.  Those are in-lab work.  Create a file in MS Word and cut and paste the required commands and the results in that file.  Make sure you have your name at the top of that file, then e-mail me the file as an attachment once you are done with the in-lab work.  Please send only one file for the entire in-lab work.  Use the in-lab in the subject line for this one.  Similarly, you have to complete a post-lab and e-mail the file as an attachment.  Use post-lab as the subject of that e-mail. 

The reason I am asking you to create a MS Word file is that you will create some images in this lab.  You can simply copy those images from MATLAB screen to 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 Copy option under the Edit tab, or by pressing Ctrl v.  Make sure the cursor is at the correct location. 

It is best that you create a directory lab1 on the machine that you are using. Try to use the same machine every time you come to the lab.  Make sure to copy your files on a disk, or transfer your files (FTP) on cs so you have a copy in case you need them.
  

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 8 with a 0.25 step size.  Following you will see the values for t followed by the corresponding values of sin(t).

Values for t:
0    0.2500    0.5000    0.7500    1.0000    1.2500    1.5000 1.7500    2.0000    2.2500    2.5000    2.7500    3.0000    3.2500  3.5000    3.7500    4.0000    4.2500    4.5000    4.7500    5.0000  5.2500    5.5000    5.7500    6.0000    6.2500    6.5000    6.7500 7.0000    7.2500    7.5000    7.7500    8.0000

Corresponding sin(t) values:
 0    0.2474    0.4794    0.6816    0.8415    0.9490    0.9975   0.9840    0.9093    0.7781    0.5985    0.3817    0.1411   -0.1082   -0.3508   -0.5716   -0.7568   -0.8950   -0.9775   -0.9993   -0.9589   -0.8589   -0.7055   -0.5083   -0.2794   -0.0332    0.2151    0.4500  0.6570    0.8231    0.9380    0.9946    0.9894
 

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 (>>):

>> t = [0 0.25 0.5 0.75 1.0 1.25 1.5 1.75  2.0  2.25 2.50 2.75 3.00 3.25  3.5 3.75 4.0 4.25 4.5  4.75  5.0  5.25  5.5  5.75  6.0  6.25 6.5  6.75 7.0  7.25  7.50 7.750 8.0]

This will generate an output that looks like this:
t =
  Columns 1 through 7
         0    0.2500    0.5000    0.7500    1.0000    1.2500    1.5000
  Columns 8 through 14
    1.7500    2.0000    2.2500    2.5000    2.7500    3.0000    3.2500
  Columns 15 through 21
    3.5000    3.7500    4.0000    4.2500    4.5000    4.7500    5.0000
  Columns 22 through 28
    5.2500    5.5000    5.7500    6.0000    6.2500    6.5000    6.7500
  Columns 29 through 33
    7.0000    7.2500    7.5000    7.7500    8.0000

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 ti we have a yi = sin(ti).
>> y = sin(t)

y =
  Columns 1 through 7
         0    0.2474    0.4794    0.6816    0.8415    0.9490    0.9975
  Columns 8 through 14
    0.9840    0.9093    0.7781    0.5985    0.3817    0.1411   -0.1082
  Columns 15 through 21
   -0.3508   -0.5716   -0.7568   -0.8950   -0.9775   -0.9993   -0.9589
  Columns 22 through 28
   -0.8589   -0.7055   -0.5083   -0.2794   -0.0332    0.2151    0.4500
  Columns 29 through 33
    0.6570    0.8231    0.9380    0.9946    0.9894

Note that to clear values of t from the memory, you need to type:
>> clear t.

As you perhaps noticed, in the above example t started from 0 and went up 0.25 by 0.25 until we got to 8.  We can use a different method to generate t values and avoid typing all those numbers.

Another method for creating the same array:

>> t = 0:0.25:8

This will result in the same numbers for t as you had before.  Then, you can generate the sin(t) values in the same way as before.
>> y = sin(t).

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, but if you type at the MATLAB prompt:
>> sin(45) you will get 0.8509.  In order to get the correct answer, you have to type:
>> y = sin(45*pi/180), which will produce the correct answer; 0.7071.

Manipulation of arrays:
Suppose, we want to compute sin(2t).  Notice that you want to do this for all t values.  If you wanted to do this in C++, you would use a loop to go through all t values and you would multiply the values by 2, then you would compute sin(2t) .  But in MATLAB, this is done much easier and faster.
>> clear t
>> clear y
>> t = 0:0.25:8
>> z = t + t
>> y = sin(z)

Exercise (1A) : sin(2t) = 2 sin(t)cos(t).  Use MATLAB to find the values for sin(2t) for an arbitrary angle t and determine whether that is the same as 2sin(t)cos(t). 

Exercise (1B):    sin2(t) + cos2(t) = 1, use MATLAB to confirm this for an arbitrary angle t. sin2(t) + cos2(t).

Matrices

A matrix is what we refer to as an arrays with more than one dimension.  There are many commands designed in MATLAB to do matrix manipulations.  In MATLAB, we can set up matrices the same way as it was done for vectors, except that each row of elements in the matrix is separated by a semicolon (;) or by a Carriage return.  Let's look at an example.

Suppose we have a 3x3 matrix, A, define as:

A =
20 22 18
12 16 12
8  10  10

and another one of the same size, B, defined as:
B =
2 4 8
1 -1 6
-2 2 4

How do we enter these matrices in MATLAB.
First method:
A = [20 22 18; 12 16 12; 8  10  10]

The output looks like this:
A =
    20    22    18
    12    16    12
     8    10    10

Second method:
B =
[2 4 8
1 -1 6
-2 2 4]

The output looks like this:
B =
     2     4     8
     1    -1     6
     -2     2     4

Let's try a few operations in linear algebra here.  Suppose, you want to find the transpose of matrix A, rows are interchanged with columns, and call the new matrix C.
>> C = A'
This generates the following output;
C =
    20    12     8
    22    16    10
    18    12    10

Suppose, we want to compute D = A * B.
>> D = A*B
D =
    26    94   364
    16    56   240
    6    42   164

Exercise (2): How do you multiply the corresponding elements of matrix A and B ?  Please note that I didn't ask you to multiply A * B.  I want you to tell me how to multiply A(1,1) by B(1,1), and A(1,2) by B(1,2), ....?  Find matrix C the result of such a multiplication between A and B given above.

You can also find the inverse of a matrix. Remember that not all metrics have inverse. You may need to determine that before you attempt to find the inverse. If a matrix doesn't have inverse (i.e., is singular) MATLAB will produce a warning.

>> X = inv(A)
X =
    0.3125   -0.3125   -0.1875
   -0.1875    0.4375   -0.1875
   -0.0625   -0.1875    0.4375

Do you know how much work you needed to do if you wanted to do this using traditional methods?  Please make sure you know how the inverse of a matrix is computed.

We can find the eigenvalues of a matrix too.

>> eig(A)
ans =
   42.4939
    2.0000
    1.5061

There is even a function to find the coefficients of the characteristic polynomial of a matrix. The "poly" function creates a vector that includes the coefficients of the characteristic polynomial.

>> p = poly(A)

Answer is:
p =
    1.0000  -46.0000  152.0000 -128.0000

We can find the roots of the characteristic polynomial, which is the same as eigenvalues:
>> roots(p)
ans =
   42.4939
    2.0000
    1.5061

ns = 5.3723 -0.3723

Reading and Writing Images in MATLAB

As we discussed in class, here is a command that reads an image (football.jpg):

>> f = imread(‘C:\MATLAB7\toolbox\images\imdemos\football.jpg’)

Note that here we have used the full path of the directory in which we have the football.jpg image stored. If our Current Directory is set to the directory where the image is stored, then we do not need to have the full path.
Once this command is executed, the image data is stored as the matrix f.  Use the:

>> whos f
To find out some information about this matrix.

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).

Postlab - Due Wednesday Sept 13

P1 (80 points) Suppose you are given a matrix A (8x8) all integers as:

127 120   0     255  250  170  150 100
227 220   200 255  250  170   150 100
17   20    100  255  250  100   120 100
120 10     0      55   150  70   250   200
120 10     0      55   150  70   250   200
17   20    100   255  250  100  120 100
227 220   200   255  250 170  150 100
127 120   0      255  250  170  150 100

Create a .m (p1.m) file for this problem, copy all your commands in that file.  I will run that .m file on my machine to generates the A and performs the computations explained below.

Compute Ai,j+1 = (Ai,j+1 - Ai,j) and Ai,j = Ai,j + (Ai,j+1)/2 where i is the index used for rows and j is the index used for columns. Note that in MATLAB the first number (top-left) has index (1,1).   Thus, in this matrix, columns with j = 1, 3, 5,  and 7 correspond to j's and j = 2, 4, 6, and 8 correspond to j+1.  Similar thing is true for i's.  Since an image is a 2-D array, you will go through two phases to complete the process.  In the first phase you will process the columns and in the second phase you will compute the rows. 

In the first one, you will compute the results in two rounds:
    Round 1) Compute all the Ai,j+1 values, and
    Round 2) Compute all the Ai,j values and use the new values as computed above in this calculation.

Example of one calculation only:
One of the calculations in Round one:  120 will be replaced with 120-127 = -7 , this came from: A1,2 = (A1,2 - A1,1)
One of the calculations in Round two:  127 will be replaced with with 127+(-7/2) = 127 -3 = 124, this came from: A1,1 = A1,1 + (A1,2)/2

You need to repeat this for all pairs on each row until all rows are processed.

This is the result after the first round is completed for all rows: A =
a =
  127  -7  0  255  250  -80  150  -50
  227  -7  200  55  250  -80  150  -50
  17  3  100  155  250  -150  120  -20
  120  -110  0  55  150  -80  250  -50
  120  -110  0  55  150  -80  250  -50
  17  3  100  155  250  -150  120  -20
  227  -7  200  55  250  -80  150  -50
  127  -7  0  255  250  -80  150  -50

This is the result after the second round is completed for all rows:
A =
  123.5000   -7.0000  127.5000  255.0000  210.0000  -80.0000  125.0000  -50.0000
  223.5000   -7.0000  227.5000   55.0000  210.0000  -80.0000  125.0000  -50.0000
   18.5000    3.0000  177.5000  155.0000  175.0000 -150.0000  110.0000  -20.0000
   65.0000 -110.0000   27.5000   55.0000  110.0000  -80.0000  225.0000  -50.0000
   65.0000 -110.0000   27.5000   55.0000  110.0000  -80.0000  225.0000  -50.0000
   18.5000    3.0000  177.5000  155.0000  175.0000 -150.0000  110.0000  -20.0000
  223.5000   -7.0000  227.5000   55.0000  210.0000  -80.0000  125.0000  -50.0000
  123.5000   -7.0000  127.5000  255.0000  210.0000  -80.0000  125.0000  -50.0000

Once this is done, you need to permute the matrix, by bringing all the elements in the even columns together to on left-hand-side and the ones at the odd columns on the right-hand-side of the matrix. Here is how the matrix will look after the permutation.
a =

  123.5000  127.5000  210.0000  125.0000  -7.0000  255.0000  -80.0000   -50.0000
  223.5000  227.5000  210.0000  125.0000  -7.0000  55.0000  -80.0000  -50.0000
  18.5000  177.5000  175.0000  110.0000  3.0000  155.0000  -150.0000  -20.0000
  65.0000  27.5000  110.0000  225.0000  -110.0000  55.0000  -80.0000  -50.0000
  65.0000  27.5000  110.0000  225.0000  -110.0000  55.0000  -80.0000 -50.0000
  18.5000  177.5000  175.0000  110.0000  3.0000  155.0000  -150.0000 -20.0000
  223.5000  227.5000  210.0000  125.0000  -7.0000  55.0000  -80.0000 -50.0000
  123.5000  127.5000  210.0000  125.0000  -7.0000  255.0000  -80.0000 -50.0000

Now repeat this process for columns. Save all your commands in one file such that when I execute the file, I get the the all the intermediate results shown here and the final matrix.

The finale result after processing all rows and columns should looks like this
a =

123.5000  127.5000 210.0000 125.0000 -7.0000 255.0000 -80.0000 -50.0000
100.0000  100.0000 0 0 0 -200.0000 0 0
18.5000   177.5000 175.0000 110.0000 3.0000 155.0000 -150.0000 -20.0000
46.5000   -150.0000 -65.0000 115.0000 -113.0000 -100.0000 70.0000 -30.0000
65.0000   27.5000 110.0000 225.0000 -110.0000 55.0000 -80.0000 -50.0000
-46.5000  150.0000 65.0000 -115.0000 113.0000 100.0000 -70.0000 30.0000
223.5000  227.5000 210.0000 125.0000 -7.0000 55.0000 -80.0000 -50.0000
-100.0000  -100.0000 0 0 0 200.0000 0 0

a =

173.5000 177.5000 210.0000 125.0000 -7.0000 155.0000 -80.0000 -50.0000
100.0000 100.0000 0 0 0 -200.0000 0 0
41.7500 102.5000 142.5000 167.5000 -53.5000 105.0000 -115.0000 -35.0000
46.5000 -150.0000 -65.0000 115.0000 -113.0000 -100.0000 70.0000 -30.0000
41.7500 102.5000 142.5000 167.5000 -53.5000 105.0000 -115.0000 -35.0000
-46.5000 150.0000 65.0000 -115.0000 113.0000 100.0000 -70.0000 30.0000
173.5000 177.5000 210.0000 125.0000 -7.0000 155.0000 -80.0000 -50.0000
-100.0000 -100.0000 0 0 0 200.0000 0 0

a =

173.5000 177.5000 210.0000 125.0000 -7.0000 155.0000 -80.0000 -50.0000
41.7500 102.5000 142.5000 167.5000 -53.5000 105.0000 -115.0000 -35.0000
41.7500 102.5000 142.5000 167.5000 -53.5000 105.0000 -115.0000 -35.0000
173.5000 177.5000 210.0000 125.0000 -7.0000 155.0000 -80.0000 -50.0000
100.0000 100.0000 0 0 0 -200.0000 0 0
46.5000 -150.0000 -65.0000 115.0000 -113.0000 -100.0000 70.0000 -30.0000
-46.5000 150.0000 65.0000 -115.0000 113.0000 100.0000 -70.0000 30.0000
-100.0000 -100.0000 0 0 0 200.0000 0 0


To help you understand this better, I will give a complete example for a small matrix, a, (4-by-4). Note we start counting from 0 and we go up, thus, 0 is considered as an even number. Thus, the first ODD number is 1.

a =
2 6 8 16
20 2 18 4
10 16 18 26
0 6 8 0


Work on ODD columns (all rows): Odd = Odd-Even
Even unchanged
2 6-2 8 16-8
20 2-20 8 4-18
10 16-10 8 26-18
0 6-0 8 0-8
Which produces:
2 4 8 8
20 -18 18 -14
10 6 18 8
0 6 8 -8

Then we process the Even ones:
Odd unchanged
Work on EVEN columns (all rows) - Even = Even + (1/2)Odd
Integer division.
2+(4/2) 4 8+(8/2) 8
20+(-18/2) -18 18+(-14/2) -14
10+(6/2) 6 18+(8/2) 8
0+(6/2) 6 8+(-8/2) -8

Which produces:
4 4 12 8
11 -18 11 -14
13 6 22 8
3 6 4 -8

Now permute, by putting even columns on the left and odd columns on the right (keeping the order):
4 12 4 8
11 11 -18 -14
13 22 6 8
3 4 6 -8
Done with the rows. We do the same things for columns this time.
Work on ODD rows (all columns): Odd = Odd-Even
Even rows unchanged
4 12 4 8
11-4 11-12 -18-4 -14-8
13 22 6 8
3-13 4-22 6-6 -8-8
This results in:
4 12 4 8
7 -1 -22 -22
13 22 6 8
-10 -18 0 -16

ODD rows unchanged
Work on EVEN rows (all columns) - Even = Even +(1/2)ODD
Integer division.

4+(7/2) 12+(-1/2) 4+(-22/2) 8+(-22/2)
7 -1 -22 -22
13+(-10/2) 22+(-18/2) 6+(0/2) 8+(-16/2)
-10 -18 0 -16
which produces:
7 12 -7 -3
7 -1 -22 -22
8 13 6 0
-10 -18 0 -16
One last permutation: Even rows to top, odd rows to bottom: This produces the final answer:

7 12 -7 -3
8 13 6 0
7 -1 -22 -22
-10 -18 0 -16

I hope this helps you with the understanding of the process.

P2(20 points)
Use MATLAB command(s) to flip the following image upside down.  Note that I do not want you to use rotation, I want you to use matrix manipulation to do this.

f = imread(‘C:\MATLAB7\toolbox\images\imdemos\football.jpg’)

List your commands that you have used in the post-lab file you will submit.

P2 - (40 points) Use matrix manipulation to combine the right 1/2 half of the image on the right hand side with the left 1/2 half of the image on the left hand side. Both images are 256X256.  Copy all your commands and the resulting image in your post-lab file.

im1  im2

         Image 1 - MRI scan of brain                        Image 2 - MRI scan of brain
Results:
comb
P3 (40 Points)  Use Image 2 as the original image for this assignment. 
    A) Sampling -
            Part 1 - Remove every other pixel from Image 2 both on rows and columns and display the final image.  You can remove from the rows first, then from       
            columns.  What is the size of the resulting image?
            Part 2 - Randomly remove 1/2 of the pixels on each row and 1/2 of pixels on each column, then display the image.  Note that after  you remove  1/2 of the    
            pixels  on rows and  columns the total number of pixels remaining will be 1/4 of the original number, so you need to pack them (on the top left) in order to be
            able to display. Basically you will end up with some bubbles that you need to remove by copying the remaining pixels on top of those bubbles.
            as an image.  Here is an example.
Suppose
A =
[2  4  6  8
 1  1  3  6
 0  2  5  7
 2  3  9  1]
Remove randomly from each row:
[2  -   -  8
  -  1   -  6
  -  -   3  6
  -  3  9  -]
 I have to pack this to make it work, so the new A looks like this:

[2  8
 1  6
 3  6
 3 9]
Now you can work on removing 1/2 from columns.

    B) Quantization -
            Part 3 - Image 2 is an 8-bit image.  Drop the intensity depth by a factor of 4, then display the image.  What is the maximum number of bits you need to represent the    
            new image?
            Part 4 - Drop the intensity depth of the original image by 16, then display the image.  What is the maximum number of bits you need to represent the new                 image?

P4 (20 points). Graduate Students Only- Apply the procedure described in P1 on Image 1 above.  Display the resulting (final) image in your post-lab file.