Bill Manaris : Spring 2008 / CSCI 180 Homework 3

Assigned Date: Friday, Mar. 21, 2008
Due Date: Wednesday, Apr. 2, 2008
Due Time: noon

Last modified on March 31, 2008, at 01:53 PM (see updates)

Purpose

This assignment focuses on image processing using Python.

Assignment

Write a Python program to do simple edge detection of JPEG images. Our goal is to capture abrupt changes of luminosity between consecutive pixels.

Design

First write out your algorithm in English. Make sure it does the right thing. Then translate it into Python. Remember the aphorism:

"20 hours of coding can save you two hours of design."

Documentation

All identifiers should be meaningful.

Include your design (pseudocode) as comments in your program. Use the following program as an example on style. Your program should look similar to this (updated appropriately):

# sunset.py  version 1.0    14-Mar-2008      Bill Manaris
#
# This program demonstrates how to create a sunset effect by reducing the
# green and blue components of an image.
#
# There is also commented code that allows to open an existing image.
#

from math import *
import Image

def main():
   filename = raw_input("Enter image filename: ")
   imageOld = Image.open(filename)    # open the provided image

   imageOld.show()                    # display it

   width, height = imageOld.size      # remember its dimensions

   ## create a new, empty grayscale image
   #im = Image.new("L", size = (width, height))   

   # create a new, empty color (RGB) image
   imageNew = Image.new("RGBA", size = (width, height)) 

   # access individual pixels
   pixelsOld = imageOld.load()
   pixelsNew = imageNew.load()

   # loop through all pixels (one column at a time, and within each column, top to bottom)
   for x in range(width):
        for y in range(height):

             # for each pixel in the old image, use its color (R, G, B)...
             red, green, blue = pixelsOld[x, y]

             # ...to create an appropriate color
             # for the corresponding pixel in the new image
             pixelsNew[x, y] = (red, green * 0.7, blue * 0.7)

   # display the resultant image
   imageNew.show()

   #im.save("test.jpg","JPEG")

main()
 

To calculate the distance between two color (RGB) pixels, use the following function:

def distance(pixel1, pixel2):
    """This function returns the color distance between two RGB pixels."""

    r1, g1, b1 = pixel1
    r2, g2, b2 = pixel2

    return sqrt((r1-r2)**2 + (g1-g2)**2 + (b1-b2)**2)
 

Grading

Your grade will be based on design and style as well as correctness of result. For style, see sample code below.

Submissions

Upload your Python program to WebCT.

Submit the following:

  1. the Python program
  2. the original image
  3. the (result) edge-detected image

Test

For testing, use the following image:

(Printable View of http://www.cs.cofc.edu/~manaris/?n=Spring2008.CSCI180Homework3)