Green Screen
Blurring is a frequently used in fashion photography to smooth skin. In this activity you will create a miniature photoshop that can blur images.
Details
Use the ppm module to
create the function blur_image(input_image_name,
output_image_name)
that blurs an image. The
parameters input_iamge_name
and output_image_name are strings that are the names of
ppm files. The function should load the image in the
file input_image_name, blur the image, and save the
image to a file called output_image_name. Blur a pixel
by taking the average of the pixel's neighbors. A pixel's
neighbor's are the pixels above, below, left, and right of a
pixel. Blur the image by blurring all of the pixels.
Example
Averaging with just the pixel's neighbors produces a very subtle blur. To magnify the effect, repeatedly blur the image. The following image on the right is the result of blurring the image on the left ten times.

Hint
-
Create a function called
blur_pixel(image, row, col)
that blurs a single pixel. The function should set the pixel in the two-dimensional list of pixels at indices row, col to the the average of the 4 pixels surrounding the pixel. To average a pixel, average the red, blue, and green channels separately. - Be Careful! If the pixel is on the edge of the image not all of the neighbors exist. The simple solution to this problem is to not blur edge pixels.
-
The
blur_image
function should iterate over the row and column indices of every pixel that is not on an edge of the image and call theblur_pixel
function with the row and column.
Challenge
You can produce blurred images that better mimic a camera lens blur by using a Gaussian function to compute a weighted average. Compute a Gaussian blur by summing the result of multiplying each pixel in a 5x5 grid with the following weights.
1/273 | 4/273 | 7/273 | 4/273 | 1/273 |
4/273 | 16/273 | 26/273 | 16/273 | 4/273 |
7/273 | 26/273 | 41/273 | 26/273 | 7/273 |
4/273 | 16/273 | 26/273 | 16/273 | 4/273 |
1/273 | 4/273 | 7/273 | 4/273 | 1/273 |
Green Screen
It can be pretty expensive to film movies and TV shows on location. However, with a little bit of Movie Magic, we can pretend to be anywhere in the world. Ever want to pretend you traveled to the expanses of Moscow? Now is your chance! Ever want to go to Delaware? Yeah… Delaware.
Use Google image search to find a green screen image, and a background image that is at least the same size, if not larger. You are going to write a program that will take these two images, and composite them together to create one image. You will need to convert these pictures to PPM files, as demonstrated in class.
Details
Write a program that uses the ppm module to replace the background in a green screen image. The program should read two image files, a green screen image and a background image, and write a new image that replaces all of the green pixels in the green screen image with the cooresponding pixel in the background image.
It's really hard to get a green screen to be exactly green. So, it is not sufficient to check just the the green value of a pixel. The program must make sure the green value is high and that the red and blue values are low enough to make sure the color is truly "green."
Example
Here is a very small example of what the program should do:
+
=
Hint
-
The program will need to analyze every pixel in the green screen image. Use nested for loops to iterate over all indices for the rows and columns.
-
Test if a pixel in the green screen image is green by testing if the green value is above some threshold, the green value is below another threshold , and the blue value is below yet another threshold. You You will likely have to play around with the threshold values you to get a better green screen effect.
-
Modify a pixel that is green by replacing its RGB values with the RGB values in the background image at the exact same row and column.
Challenge
It's one thing to use a green screen image you found on the Internet. It's another thing entirely to make your own green screen images! Luckily, we have this nice (hopefully pretty clean) chalkboard that we could you to mimic a green screen.
Take a picture with your cellphone of yourself standing in front of the chalkboard. Email the picture to yourself, and try to composite your image with a different image you found on the web. You will likely have to make drastic changes to your threshold values, but you should be able to get something that looks decent.