The Pesky Tourist

Recently, I have been become interested in photography, and I decided that I wanted to photograph one of the sculptures on campus. 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 same sculpture. I tried repeatedly to get a picture of the sculpture 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 sculpture, without anyone in the picture, but I had had no luck.

When I went back home to look at my photos, I realized the guy had been moving a lot. For example:

photo 1 photo 2 photo 3
photo 4 photo 5 photo 6
photo 7 photo 8 photo 9

If you look closely, you can see that the most of sculpture is visible in each image. The tourist only blocked part of the sculpture, and since he was in a different position in each photo, he usually blocked a different part of the sculpture each time. If I could come up with a way to determine which pixel colors are part of the sculpture 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 the images at the same time, placing them into an array. We can then sort those values, and pick the middle value, i.e., the median value for that list of 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 create a good photo without that pesky tourist.

Your Task

You have been given 9 PPM files, the image format we discussed in lecture. The images are all the same height and width. You should open all the images at the same time for reading, and then open result.ppm for writing. Create the proper PPM header like we have done in lecture and lab. Read the header values from the first file to do that. Make sure to skip the header in the other files.

Once you at 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 up to the first value in the ninth file. Calculate the median, and write the result to the result image. 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.