Recently, I have been become interested in photography, and I decided that I wanted to photograph the cross that is between Hayes Hall and the Rogalski Center at St. Ambrose University. I took my trusty camera and tripod out to take a quick picture. Unfortunately, there was an out-of-town tourist there at the same time. He was apparently very interested in the cross. I tried repeatedly to get a picture of the cross without him in the image, but he managed to be in every single frame. I eventually ran out of time and had to leave without the picture I wanted. All I had wanted was one single photo of the cross, without anyone in the picture, but I had had no luck.
When I went back to my office to look at my photos, I realized the guy had been moving a lot. For example:
If you look closely, you can see that the most of cross is visible in each image. The tourist only blocked part of the cross, and since he was in a different position in each photo, he usually blocked a different part of the cross each time. If I could come up with a way to determine which pixel colors are part of the cross and the background (what I want), and which pixel colors are part of the tourist (what I don't want), I could create a photo without the tourist.
It turns out there is a way to do this. We can use an image processing technique called the median filter. This is a method for removing noise - or a tourist in this case - from a set of similar images.
In the median filter, we compare individual pixels from each image. We start out by making the assumption that most of the pixels in each image at a specific coordinate (x, y) are same color or very close in value. These are the colors we want. Sometimes, at a particular pixel in a particular image, that pixel will be part of the tourist and that pixel will have a very different color from the other pixels at the same coordinate in the other images. These different colored values are the noise we are looking for and trying to remove.
In order to pick the correct color for the pixel at each (x, y) coordinate, we can read the color value for that particular coordinate in all nine images at the same time. We can then pick the middle value, i.e., the median value for those nine numbers. Once we know that median value, we can write it out to our filtered image. Of course we have to remember that each pixel really contains three values: one for red, one for green, and one for blue. That means for each pixel, we have to find three median values.
If we do the above process for each pixel, and if we do it for the red, green, and blue value in each pixel, then the final result will be a good photo without that pesky tourist.
Your Task
You have been given 9 PPM files. The images are all the same height and width. You should open all the images at the same time for reading, and then open a file named IMG_Final.ppm for writing and create the proper PPM header. Read the header values from the first file to do that. Make sure to skip the header in the other files.
Once you are ready to read the pixel color values, read all the values in the files, one integer at a time. The first value in the first file, the first value in the second file, the first value in the third file, and so on, for all 9 image files. Calculate the median, and write the result to the IMG_Final.ppm file. Continue on to the second value and follow the same process, and then the remaining values the same way. Your result image should be tourist free.
More About PPM Files
The pictures are stored in the PPM format. A PPM file is a plain text file. The structure of the text file is as follows:
· The first line contains the string P3.
· The second line contains two integers which give the width and height of the image in pixels. We'll call these w and h.
· The third line contains a single integer that tells the maximum value used for a color value for red, blue, and green components of each pixel, this will be 255 for our images.
· The remainder of the file consists of w × h lines. Each of these lines contains three integers representing the red, blue, and green component of a single pixel.
There are several options for viewing a PPM image:
Here are two tiny PPM images that you can open in a text editor. Both have a greenish background with a red line from the top left to the bottom right corner. The tiny1.ppm file is 5 × 5 pixels and the tiny2.ppm file is 20 × 20 pixels