Image Processing 2

Table of Contents

This is a pair programming assignment. If you are working in a pair, this means that you and your partner should be doing the entirety of this assignment side-by-side, on a single computer, where one person is "driving" and the other is "navigating." Set a timer to swap every 15 minutes. You can choose your favorite from this online timer page. Make sure your sound volume is audible, but not so loud to disturb the people around you.

If you are working in a pair, only one of you needs to submit your work via Moodle. That said, you should both have a copy of your work in case you want it someday, so make sure that both of you have copies of it; you can email it or use some other mechanism to transfer it.

We will use anonymous grading on Moodle, which means that the grader won't see your name until after the grading is done. This is an easy way to help add an extra element of fairness to the grading. Therefore, make sure your name doesn't appear on your actual submission. When you submit via Moodle, it will know you are. Thanks!

1 Overview

Movies, particularly action movies that use a lot of special effects, often use a technique called chroma key. With chroma key, the actors in a scene are filmed as they perform in front of a solid color screen (perhaps green, or blue). Later on, after the scene has been filmed, the special effects people remove that color from the scene and replace it with the actual scene: a dinosaur, the New York City skyline, outer space, etc. This technique is also regularly used by TV weather forecasters to make it appear as though they are standing in front of a map.

In this assignment, you will be using the same technique.

2 Getting started

Create a directory called image2 for this assignment. To facilitate reading in, storing, and manipulating image files in "standard" formats (like .jpg, .gif, and .png), we will again use images.py. Copy images.py into your directory from the above link or from the course website. Here is the documentation for images.py.

You'll also need some images to work with. First, you'll need an image of something in front of a green screen or other solid background. The following links are for images I found online that had Creative Commons licenses that permitted me to resize them and redistribute them for the purpose at hand. I've included links to the original images and their licenses. You're welcome to use your own images for this assignment if you like, but you need to have permission to use them. That means that you've taken them yourself, or you've included a link or other text indicating a license for the image granting you to use it.

You'll also need some background images. The following images were all taken by Prof. Amy Csizmar Dalal, and she has granted permission for their use.

3 Your tasks

The background pictures depict some sort of scenery, while the first set of images portray a subject appearing against a plain background. We can combine these images into a single image by replacing the green pixels in one of the original pictures with pixels from one of the scenery pictures. To do this, we have to figure out which pixels correspond to the background (and can be changed) and which ones correspond to the subject (and should be left alone).

Create a Python module called photolab2.py. It should contain the following functions:

3.1 Replacing the background with a solid color

Your first function should be defined as:

replaceBackgroundWithColor(image, backgroundColor, replacementColor)

This function should create and return a new image that replaces all background pixels in the original image by the color specified by the supplied replacement red, green, and blue values. The background red, green, and blue values are used to indicate what is considered "green" for the background. Each image I have provided has a slightly different shade of green, so the background color needs to be specified along with the image.

Note that backgroundColor and replacementColor are expected to be Python tuples. Here is an example of how I might call the above code in a main function:

origImage = FileImage('somefile.jpg')
greenBackground = (86, 164, 104)
purpleBackground = (204, 51, 255)
newImage = replaceBackgroundWithColor(origImage, greenBackground, purpleBackground)

Finally, you should take into account the fact that the background color isn't perfectly uniform. You'll need to use some kind of range around it.

3.2 Replacing the background with an image

Create a Python function defined as:

replaceWallWithImage(originalImage, backgroundColor, replacementImage)

This function should create and return a new image that replaces all background pixels in the original image with pixels taken from the replacement image. Note that the original image and the replacement image need to have the same dimensions for this technique to produce the desired effect.

4 Test your code

Test your program by an appropriate main() function, which you should also submit as part of your photolab2.py. Feel free to use your own pictures if you'd like. We will test your program similarly.

5 Design and style

Make sure that your program follows good design and style. See the guidelines from recent assignments. If you really want to learn lots about Python style, the official style guide for Python is a great read.

6 The last point

If you complete the above functions (and other things like style, etc. are correct), you will receive nearly all of the points for this assignment. You should feel proud and good about yourself that you have gotten this far, and feel free to stop here! If you want to try to earn the remaining point, write one more function:

replaceBackgroundWithShadedColor(image, backgroundColor, replacementColor)

If you look closely at the toy machine and leopard images, you'll notice that the background is not a uniformly solid color: it contains some shading. The toy machine has some shadows, and the leopard background has some locations that are just lighter in color. This new function is a a variation on the first replaceBackgroundWithColor function that preserves the shading variations in the background. In other words, calling this new function with a set of colors defining purple and applying it to the toy machine would produce a purple brick background, with the toy casting a shaddow in purple. Modify your main as necessary to test your code.

7 Submit your work

When finished, zip up your code and submit your work through Moodle.

Good luck, and have fun! Remember that lab assistants are available in the evenings in CMC 102 and CMC 306 to help out if you need it, and you can attend prefect sessions as well.

Author: Dave Musicant

Emacs 24.5.1 (Org mode 8.2.10)

Validate