CS112: Lab 4 :: Matrices, Images and Data files

Part i: Making synthetic images

MATLAB allows creation of synthetic images.
Let's start with a 100x100 matrix of zeros (black).

img = zeros(100,100);

Then we'll add a white (1.0) square in the upper left corner:

img(1:20,1:20) = 1.0;

Then we'll add a grey (0.50) rectangle:

img(20:50,20:80) = 0.50;

>>imshow(img)

Now, let's take a little clip of our image, as shown by the red square below:

clip = img(10:30,10:30);

>>imshow(clip)

Now, let's make a new picture, using the clip from the old picture.
Let's start with a white background:

newpic = ones(100,100);

And add in the clip in two places:

newpic(1:21,1:21) = clip;
newpic(80:100,80:100) = clip;

to produce this picture:

>>imshow(newpic)

What do you need to do to make this picture?

>>imshow(newpic)

Inverting greyscale images

Let's revisit the little clip shown below:

clip = img(10:30,10:30);

How can we create the clip's cousin, flip (shown below) in MATLAB?

hmmm, what is the relationship between clip and flip?

And how about clip's other cousin, upsidedown_clip, how can we make the picture below?

Part ii: Using imtool to get image coordinates

Here's a picture of Nate Robinson, who used to play basketball for the Boston Celtics. As you can see from the picture, he's relatively short for an NBA player (he's 5' 9"). He is known for doing fabulous shots where he leaps over tall people (in this case, Dwight Howard who is 6' 11"). Let's work on getting a close-up of his face. The picture below is called robinson.jpg in your assign3_programs folder. Read this image into MATLAB like this:

nate = double(imread('robinson.jpg'))/255;

This reads in the JPG file (where the numbers range from 0-255) and scales it into a matrix with double precision numbers ranging from 0.0-1.0. The double refers to the precision and not to the value.

nate robinson

Peek in the workspace to see your (large) variable named nate.
Now let's use imtool to figure out the coordinates to get a closeup of his face.

imtool(nate);

imtool(nate) brings up the window below (without the red dashed lines and yellow dots). As you move your cursor over the image, you can see the coordinate in the lower left corner of the Image Tool window. The coordinates of the box around Nate's face are labeled in the image below:

nate robinson in a box using imtool

Now, using your matrix variable nate, take a subset of nate using the coordinates above as a guideline. Think carefully about which coordinates to use (remember that imtool reverses x and y). Save his face in a variable called face and then show his face using imshow. Note that his face closeup will appear in grayscale (as opposed to color), we'll talk about why this occurs later in the course.

nate robinson's face

Part iii: Reading and Writing Excel files with MATLAB

MATLAB can read in data from Microsoft Excel spreadsheets and can also create spreadsheets. We'll practice this a bit today, in preparation for assignment 3.
  1. First, open Excel and create a simple spreadsheet that contains only numbers (no strings and no headers). Enter data in multiple columns in Excel.
  2. Save your spreadsheet in your assign3_programs folder as my_data.xls. (Note that it must be .xls and not .xlsx)
  3. Start MATLAB, set your current directory to your assign3_programs folder, and read in the Excel spreadsheet into a variable:
    >> datam = xlsread('my_data.xls');

  4. In MATLAB, you can manipulate your data just like any other MATLAB matrix, e.g. multiply it, scale it, change certain values within it, etc. Change the values of all your data, for example:
    >> datam = datam * pi / 85.7;

  5. Now, write your newly transformed data out to an Excel spreadsheet (note single quotes around the new file name indicating a string):
    >> xlswrite('my_data_v2',datam);

  6. MATLAB will respond with a Warning message like this (MATLAB likes to issue warnings each time you write out a data file):

    Warning: Could not start Excel server for export.
    XLSWRITE attempts to file in CSV format.

    Even though it issues a warning, MATLAB still creates a file with your data called my_data_v2.csv. The csv stands for comma separated value format.

  7. In your assign3_programs folder, look for your file (in this example, it's called my_data_v2.csv)
  8. You can double-click on it, or Control-Click and then select Open With -> Excel. Within Excel, you can save your spreadsheet as an .xls (excel format) file. Ta-dah, there's your spreadsheet written out by MATLAB!

Prelude to Assignment 3

Assignment 3 contains two Exercises to complete before you tackle the problems.
The exercises are: 1) Exploring visual perception phenomena and 2) Working with Gradesheets.
Click here for the Exercises.

Uploading your saved work

Use Fetch to upload your saved work.
Work-in-progress should go into your personal cs112/ folder.