Image Stacker

John Nicholson
nicholsonja@apsu.edu
Austin Peay State University, Clarksville, TN

Overview

Images can provide a relatively easy way to introduce introductory programming concepts. They often require students to read and write files, and use arrays. They can also introduce students to real-world problems with programming-based solutions.

This assignment continues that tradition by introducing students to a problem that astronomy photographers often have to deal with. Since the objects astronomers like to photograph are far, far away and require long exposures, the photographs are often noisy and grainy. In order to overcome this problem and increase the quality of the photographs, photographers often use a procedure called stacking. Basically, the photographers take multiple pictures in quick succession of the same object and then combine, or stack, the images into a final image in order to remove or reduce the noise in individual images. One method for noise removal is based on averaging the pixels: add all the pixels in the same (x, y) location in each image and then computer the average red, green, and blue values for each pixel to create the final image.

In this assignment, the noisy images are simulated. Images are downloaded from http://hubblesite.org/gallery/, the gallery for NASA's Hubble Site that showcases many images taken by the Hubble Space Telescope. The original images are processed using the provided Java code to generate a set of noisy images. These noisy images are then given to students who are then responsible for writing code to recombine the low quality images into a higher quality images resembling the original Hubble Site pictures.

This assignment uses the PPM image format since it is a text-based format. Students only need basic text processing skills in order to read the image files. The assignment also requires students to process multiple files to achieve a result, instead of the often-used pattern of one file in and one file out. Here, multiple noisy files must be combined to create one final image.

Meta Information

Summary Image Stacker -- Combine multiple noisy and grainy astronomical photographs into a single clear image. Requires students to read and parse multiple text-based image files and to use arrays to hold the intermediary results. Mathematically, it only requires an understanding of how to find the average of a list of numbers.
Topics Reading and writing text files
Managing multiple text files
Arrays
Basic programmatic directory structure navigation
Basic image processing techniques
Audience Appropriate for CS2 or a later course.
Difficulty This is an intermediate assignment, taking 1 week for a CS2 student.
Strengths Requires students to tackle a real-world problem using text-based files and arrays.

The basic concept is language independent. I have given variations of this assignment in Python and Java. It can easily be targeted to any language allowing file access.

It has the advantage of exposing students to the science from the Hubble, and the scientific eye-candy available at Hubble Site.
Weaknesses Depending on how many noisy files you generate per original file, the download for this assignment can get big. For example, I used the four files in the original directory. For each image, I created 10 noisy images for a total of 40 PPM files. Unzipped this came to 105 MB, zipped 28 MB. Smaller original images can be used to reduce the download size.

The noisy file names are generated following a pattern. For example, the original file orion.jpg would have the noisy files named orion_001.ppm, orion_002.ppm, and so on up to orion_010.ppm, assuming 10 noisy files per original image. Students are often given assignments that have specific file names for them to process. Generating filenames in a loop with format strings is usually not a topic covered in textbooks or classes.

It is sometimes seems difficult for students to understand they need to skip the PPM header information.
Dependencies
Requires an image viewer that understands the PPM format.

Traditional viewers include

Since these programs are big and complex, I wanted an easier way for the students to view the images. I have been experimenting with a simple HTML/JavaScript file that can open and read PPM files. Students are given the file ppmReader.html. The file is placed locally on the student's computer, and allows them to view PPM files. The code does not currently support all possible PPM options, but is easily modified. This file is not specific to this assignment, and can be used on any PPM-related project as an alternative to the programs above. It requires a modern browser, such as Firefox, Chrome, Safari, etc.

Variants Nothing about this assignment explicitly requires using PPM. For advanced students, you may want to explore using image libraries such as the Python Imaging Library (PIL) for Python or Java Advanced Image (JAI) API.

As written here, there is no user interface; it is only a file-based project. You could add user interface elements such as choosing the directories with only certain files or displaying the final result. This will require more knowledge of a language's GUI library.

Provide the students fewer files. The minimum requirement for this process to work is to create two noisy files per image. In general, there should be an even number of noisy files. Using fewer files will mean the student only has to think in terms of two files, and may enable them to see the algorithm faster.

Generating the Noisy Images

In order to create the sets of noisy images quickly, there is Java code for use by the instructor. It consists of two classes tools.StackCreator and tools.NoiseProcessor. The main() is in tools.StackCreator.

When run, the program asks for a source folder with the original images, and a destination folder where the noisy images will be stored. The source folder can contain JPG, BMP, GIF, or PNG images. After running the destination folder will contain a series of folders, one for each source image. Inside each of these folders, there will be a series of images - currently set at 10. Each image is a modified version of the original with noise added to the red, green, and blue components of each pixel.

Sample Student Write-up

A sample write-up to use as a starting point

Examples

Below are examples of original images from Hubble Site, a sample of the simulated noisy images created by the instructor script, and the result of stacking/averaging 10 simulated noisy images. The best images are ones that have a wide range of color and do not have a lot of black or white colors. Click each image to see the image at full size, which will allow you to compare image quality between the different image versions. Please note that the PPM files have been converted to PNG so that they can be displayed in the browser.

Name Original downloaded from Hubble Site
Processed by the instructor to create sets of noisy images
Sample noisy image
Given to students as part of assignment download
Stack result from averaging 10 noisy images
Student's code create these images
cone_nebula original
Source: http://hubblesite.org/gallery/album/nebula/pr2006001a/
sample stacked
n44f original
Source: http://hubblesite.org/newscenter/archive/releases/2004/26/image/a/
sample stacked
orion original
Source: http://hubblesite.org/newscenter/archive/releases/2006/01/image/a/
sample stacked
wfc3_uvis original
Source: http://hubblesite.org/newscenter/archive/releases/2009/25/image/i/
sample stacked