# HW1

## Due: 4/8 12:30pm EEE Dropbox

### Using Matlab [60 points]

The goal of this problem set is to become familiar with basic Matlab commands, practice manipulating vectors and matrices, and try out basic image display and plotting functions. If you are unsure what a Matlab function does, check the reference manual (at the command line, type "help" and then the command name).

What to hand in.

1. Include a script for questions 3 and 4 (titled hw1_q3.m and hw1_q4.m) that the grader can run to generate all the given figures. Most of the following can be done in a matlab script, but feel free to call other scripts, functions (but include them in the homework zip file). When instructed to write a function, your script must call that function.

2. Make sure each question is answered in your project writeup, including either a figure, plot, and/or an example snippet of the interactive commands and output used to compute the question.

1. Read over the provided the Matlab tutorial.

Open an interactive session in Matlab and test the commands by typing them at the prompt. (Skip this step if you are already familiar with Matlab.)

2. Describe (in words where appropriate) the result of each of the following Matlab commands. Use the help command as needed, but try to determine the output without entering the commands into Matlab. For this part of the assignment, do not submit a screenshot of the result of typing these command.

1. >> x = randperm(5);

2. >> a = [1:10];
>> b = a([1:3:end]);

3. >> f = [1501:2000];
>> g = find(f > 1850);
>> h = f(g);

4. >> x = 22.*ones(1,10);
>> y = sum(x);

5. >> a = [1:1000];
>> b = a([end:-1:1]);

3. Given a 100 x 100 uint8 matrix A representing a grayscale image (hint - use "imread"), write a few lines of code to do each of the following. Try to avoid using loops. Select any image you want, or use one extracted from one of the videos on the project data page. You are free to use matlab scripts and/or functions (unless you are specifically asked to write a function).

1. Sort all the intensities in A, put the result in a single 10,000-dimensional vector x, and plot the values in x.

2. Display a figure showing a histogram of A's intensities with 32 bins.

3. Create and display a new binary image the same size as A, which is white wherever the intensity in A is greater than a threshold t, and black everywhere else.

4. Display the bottom right quadrant of A.

5. Generate a new image (matrix), which is the same as A, but with A's mean intensity value subtracted from each pixel. Set any negative values to 0.

6. Use rand to write a function that returns the roll of a six-sided die.

7. Let y be the vector: y = [1:6]. Use the reshape command to form a new matrix z whose first column is [1, 2, 3]', and whose second column is [4, 5, 6]'.

8. Use the min and find functions to set x to the single minimum value that occurs in A, and set r to the row it occurs in and c to the column it occurs in.

9. Let v be the vector: v = [1 8 8 2 1 3 9 8]. Use the unique function to compute the total number of unique values that occur in v.

### Programming: background subtraction [40 points]

1. Write a function that will compute the average image of all images in the current directory. Show this color image, and show a grayscale version of it (computed by averaging the R, G, and B channels together).

2. Pick 3 random frames with people in them. Show these three images.

3. Let av(x,y) be the RGB value of the average image at position (x,y). Let I(x,y) by the RGB value of one of the above frames at the same position. Define b(x,y) = ||av(x,y) - I(x,y)||^2 > t. Experiment with different thresholds t. Show the results for two different t.

4. Describe the behaviour of the backgound subtraction algorithm for various t, and provide explanations for where it fails and succeeds.

5. (Extra-credit) Extract frames from an additional video clip (say, from a webcam) and show the above results.

### Hints

• Be sure to do the necessary typecasting (uint8 and double) when working with or displaying the images. Some useful functions: title, subplot, imagesc, imshow, std, mean, imread, rgb2gray, dir.

• This code fragment can be used to loop through all the image files in one directory (here assuming your images are in a sub-directory named images, with .jpg extension):
```      filelist = dir('images/*.jpg');
for i=1:length(filelist)
imname = ['images/' filelist(i).name];