< Back

Lab 31: Images

As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.

    $ cd ~/cs120/labs
    $ mkdir lab31 
    $ cd lab31 


Practice 1

Write a function called create_nxn_matrix(size), which takes a single integer as a parameter. Your function should return a \(size \times size\) matrix of \(0\)'s.



PPM Module

Image files can be thought of as just 2-dimensional lists of pixel color values. We can even extend that thought to 3-dimensional lists, since every pixel value can be represented by a red, green, and blue value (typically abbreviated RGB). The one complexity with this representation is how to get an image file into a list of this structure. Luckily, we are giving you a module that will do just that.

The ppm.py module holds 4 functions, but you are only concerned with two of them. the read_image(file_name) function takes a string file name of a PPM image, and returns a 3-dimensional list of pixel color values. For any given pixel color list, the red value is store in the 0th index, the green value is stored in the 1st index, and the blue value is stored in the 2nd index.

The write_image(ppm_file_name, image) takes a string file name of where you want to write a PPM image, and a 3-dimensional list of the above form. This function will actually write the image out to the file system, using the file name specified in the parameters.


Blur

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

Create a function called blur_image(input_image_name, output_image_name) in a file called blur.py. 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. You can 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. You can blur the image by blurring all of the pixels.

Example

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). The function should take an image, a two-dimensional array of pixels, and the row and column of a pixel to blur. The function should set the pixel in the image list at indices row, col to the the average of the 4 pixels surrounding the pixel. To average a pixel, you 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 may 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 the blur_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/2734/2737/2734/2731/273
4/27316/27326/27316/2734/273
7/27326/27341/27326/2737/273
4/27316/27326/27316/2734/273
1/2734/2737/2734/2731/273

Green Screen

It is pretty expensive to film movies and TV shows on location now-a-days. 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 function overlay_greenscreen(greenscreen_list, background_list) in a file called greenscreen.py. This function takes two parameters: an m × n 2-dimensional list of RGB values (really just a 3-dimensional list of integers), and a j × k 2-dimensional list of RGB values (but really just a 3-dimensional list of integers), where j ≥ m and k ≥ n. This function should modify greenscreen_list by replacing pixels that are mostly green, with the corresponding pixel from background_list.

As it turns out, it's really hard to get a green screen to be exactly green. Thus, it is not sufficient to check just the value of the green portion of a given pixel. You need to make sure the green value is high enough, and that the red and blue values are low enough to make sure the color is truly "green."

Example

Here is a very simplistic example of what you are trying to do:

+ =

Hint

  • The overlay_greenscreen function will need to analyze every pixel in the greenscreen_list. Use nested for loops to iterate over all indices for the rows and columns.

  • Define three global constants called RED_THRESHOLD, GREEN_THRESHOLD, and BLUE_THRESHOLD. These should be integer values in the range [0, 255]. With these variables, a given pixel is replaced with a pixel from from the background only if the red and blue colors are below this defined threshold, and the green value is greater than the threshold.

  • You will likely have to play around with the threshold values you define for the RGB values to get a better green screen effect. The thresholds are going to be very different from what you expect. Can you figure out why this is the case?

 

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 (or a friends) 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 by the end.