CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Lecture 35 - Images


As usual, create two directories for today's class. Create a directory called lecture35 under activities, and a directory called lab35 under labs.


And now for something (seemingly) completely different

Moving on from 2-dimensional lists, let's start talking about images. You have read in image files in the past (see Lecture 33). However, you didn't really mess with the true nature of that list. Let's see how we can work with that a little more.


The 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 values (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.


In-class Activity 1
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 cooresponding 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 analyize 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 resembles a green screened image.


Lab Assignment 35
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(image_file_name) in a file called blur.py. The function should load the specified image, blur the image, and save the image to the same file 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

Submission

When you have finished, create a tar file of your lab35 directory. To create a tar file, execute the following commands:

cd ~/cs120/labs
tar czvf lab35.tgz lab35/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 35. Only one of your pair should submit your activity. Make sure both partners are listed in the header of your files.

Do not forget to email your partner today's files!


Last modified: Thu Nov 7 21:44:28 EST 2013