CS112: Lab 4 :: Matrices and Images

## 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), 20x20, square in the upper left corner: img(1:20,1:20) = 1.0; Then we'll add a grey (0.50), 30x60, rectangle, as show in the picture to the right: >>imshow(img) Now, let's take a little clip of our image, as shown by the red square below: >>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: to produce this picture: >>imshow(newpic)

### Inverting greyscale images

Let's revisit the little clip shown below:

clip = img(11:30,11: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_exercises` 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. In other words, double does not mean that any numbers are being multipled by 2, but rather that the numbers are highly precise.

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:

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, but if you are curious, here is a brief explanation.

## Prelude to Assignment 3

Assignment 3 contains two Exercises to complete before you tackle the problems.
The exercises are:
1. Exploring "The Thatcher Illusion", an interesting visual perception phenomenon, 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.