Counting Stars Exercises

Based on the Multimedia Programming lesson at Software Carpentry.


Instructions: Create a new notebook called CountingStarsExercises in your CountingStars directory and solve the following problems inside it. Be sure to include the problem statements in a markdown cell above your solution. You don't need to put the "helper" code in the markdown cell, just implement the helper code in your code cell with your solution.

Preliminaries: At the top of your notebook, include a "Heading 1" cell with the title "Counting Stars Exercises". Then include the SciPy and NumPy inline functions by adding a code cell that invokes the %pylab inline magic and imports the needed packages.



In [ ]:
%pylab inline
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import requests
from StringIO import StringIO

Question 1

Modify the fill function so that it never looks at the same pixel twice. There are at least two ways to do this. Name your new function fast_fill. The function prototype might look something like this:

def fast_fill(picture, xsize, ysize, xstart, ystart):
    """Faster fill algorithm that doesn't look at any pixel more than once."""

Be sure to include comments and add detail to the docstring to describe what it is doing.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

Question 2

Modify your count function to take another argument - which fill function to use - and compare the time it takes to count the stars using the original fill, your improved fast_fill, and the recursive rec_fill.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

Question 3

Use the fastest algorithm from question 2 to count the stars above a threshold in all of the images listed at the top of the page. How does the count for each image change if you lower the threshold from 600 to 500? Comment on any changes to the count. Why do they happen?


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

Question 4

Write a variation on your count function to keep track of the pixels in a given blob and use the brightest pixel in the blob as the "location" of the star. Use the data I/O skills we learned to write out a text (not binary) file containing

  • a header which includes
    • the link to the image
    • the threshold value used in the conversion to monochrome
    • the algorithm used to count its stars
    • how fast it processed the image
    • how many stars were found
    • a description of the data in the subsequent columns
  • a list of the star data organized in three columns and formatted for easy reading as follows:
    • location (x,y in pixel units)
    • maximum brightness value for the star

Apply your function to the six images at the top of the page.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

All content is under the modified MIT License, and can be freely used and adapted. See the full license text here.