CS 112

Assignment 9

Due: Wednesday, May 9

This is the last assignment of the semester! It is best to hand in this assignment during the last class, but there will be no penalty for handing it in late. Your hardcopy submission should include a cover sheet and printouts of two code files: findStars.m and mickey.m.

Reading

The following material is especially useful to review for this assignment: Lectures #19, #21 and #22, and Lab #12.

Getting Started: Download assign9_programs from cs112d

Use Fetch or WinSCP to connect to the CS server using the cs112d account and download a copy of the assign9_programs folder from the cs112d directory onto your Desktop. Rename the folder to be yours, e.g. ellen_assign9_programs. In MATLAB, set the Current Directory to your assign9_programs folder. This folder contains only one file that is an image of the center of the Milky Way: milkyWay.jpg.

Uploading your saved work

Use Fetch or WinSCP to upload your saved work, and connect to the CS file server using your personal user account name and password. After logging in to your account:

When you are done with this assignment, you should have 3 files stored in your assign9_programs folder: milkyWay.jpg, findStars.m and mickey.m.

Exercise 1: Reaching for the Stars

Your goal for this exercise is to count the stars in NASA's Astronomy Picture of the Day from October 23, 2005. The milkyWay.jpg file in the assign9_programs folder contains this NASA image, which can be loaded into the MATLAB Workspace using imread. The image is loaded into a 516x624x3 matrix of type uint8. The third dimension stores red, green and blue (RGB) color components as integers ranging from 0 to 255. This color image can be displayed with imshow.

The stars in the MilkyWay image are bright white areas. Recall that in the RGB color representation, white is composed of maximum values of all three RGB colors. You can find the stars in the image by first finding locations with large red, green and blue values (i.e. locations that are close to white), and then counting the clusters of white locations. You will write a function that uses this strategy to count the stars, and also displays intermediate results along the way.

More specifically, write a function findStars that has two inputs, an image and a threshold, and performs the steps listed below. All of the display steps can be performed with the imshow function, and will be displaying images in three separate figure windows. Recall that the figure command opens a new figure window. After running the findStars function, it is a good idea to execute the close all command to close the existing windows before running findStars again. MATLAB behaves in a buggy manner when too many figure windows are open at one time.

Write your findStars function to perform the following steps:

  1. In one figure window, use subplot to create a 2x2 configuration of four images: the original image, and three gray-level images that depict the red, green and blue components of the original image (similar to our Mona Lisa example in lecture).
  2. Find the stars:
    1. create a 3-D matrix of the same size as the input image, to store logical values 0 and 1 (note that the size function returns a vector with all three dimensions in the case of a 3-D matrix, and an initial matrix of the logical value 0 can be created with the false function, e.g. matrix = false(10,10,3)).
    2. for each location in this new matrix, store a 1 if the corresponding location in the input image is larger than the input threshold, and 0 otherwise. For example, if the red value in the original image at location (x,y,1) is greater than the input threshold, then store a 1 at location (x,y,1) in the 3-D logical matrix. (When calling findStars, keep in mind that the RGB values range from 0 to 255, so choose a threshold in this range.)
    3. create a 2-D matrix of logical values with the same number of rows and columns as the input image. In this matrix, store a 1 at locations where the red, green and blue values are all above the input threshold (i.e., the value 1 is found in all three indices in the third dimension of the 3-D logical matrix created in step 2b).
    4. in a new figure window, use subplot to create a new 2x2 configuration of four images: the red, green and blue slices of the 3-D logical matrix created in step 2b, and the 2-D matrix created in step 2c.
  3. Count the stars and display them with custom-made colors:
    1. use bwlabel to find the connected components of the binary image (logical matrix) that you created in step 2c. Connected components are groups of 1's that are connected in the image (the second input to bwlabel can be 4 or 8, depending on whether you want to consider diagonal elements as connected). The groups of image locations that are connected are all labeled with the same number, and the label number increases with each new connected component that is found.
    2. determine the number of stars found (the number of connected components, which will be the largest number stored in the matrix of component labels).
    3. create your own colormap to display the connected components corresponding to stars - the number of colors should equal the number of stars, so that each label is displayed with a different color. For example, you could just create multiple shades of your favorite color. The index 1 in the colormap should store the color black, and colors for labels should be sufficiently large RGB values to be visible.
    4. in a third figure window, display the star components using your colormap - imshow can display an indexed image where each value is an index into a colormap: imshow(components, colormap).
    5. finally, print a message in the Command Window that indicates how many stars were found.

Exercise 2: I'm goin' to Disneyland!

In this exercise, you will write a recursive function in a MATLAB file called mickey.m that produces the pictures below. The function mickey takes 6 parameters: levels, xcenter, ycenter, radius, color1 and color2.

mickey(1,0,0,50,'r','b'); mickey(2,0,0,50,'r','b');
mickey(3,0,0,50,'r','b'); mickey(4,0,0,50,'r','b');
mickey(5,0,0,50,'r','b');

Let's take a close look at the level 2 figure to understand how mickey is drawn:

mickey(level, xcenter, ycenter, radius, color1, color2)
mickey(2, 0, 0, 50, 'r', 'b')

In this particular drawing, xcenter and ycenter are both 0, so the circle is centered at the origin (0,0). The radius of the big circle is 50; the radii of the two smaller circles are 50/2, or 25. The first circle is drawn with color1 (in this case, red) and the smaller circles are color2 (in this case, blue). The yellow box in the diagram illustrates how the big circle and the little circle are positioned relative to one another.

Hints:

 

This is the end of assignment 9*.
* Even better, this is the end of the last cs112 assignment this semester. :-)