Docsity

EECS12 Homework 8: Image Processing in Python, Assignments of Electrical and Electronics Engineering

Toggle sidebar
bg1
EECS12 Homework #8 Due: 11:59 PM, Monday, March 21, 2005
In this assignment you will write a Python program that does some elementary image processing.1
A Python class called Image will be provided for you that reads in images, lets you access the pixels
(stored as red-green-blue integer tuples), and lets you write the image back out. The main methods
are:
1. read( filename )
2. write( filename )
3. pixel( i, j )
4. setpixel( i, j, p )
See the file Image.py for a full description of what these methods do, and what the arguments mean.
The Image class encapsulates a very simple image reader/writer. It can only handle images that
are in “PPM” format, which stands for “Portable Pixel Map.”2If you wish to run your program on
other images, you will need to first convert them into PPM format.
What You Need to Do
You are to create and submit a Python module named “hw8.py” containing definitions for the
following functions (with precisely these spellings, including case):
1. PixelDiff( p, q )
2. FindEdges( filename, epsilon )
3. ShowDiffs( filename )
Be sure to submit only working Python code and comments. This should be a fully functional
program that imports Image.py. Here is what the functions should do:
1. PixelDiff: Pixels in an instance of the class Image store three integers as a tuple: (r,g,b). each
integer is in the range 0 to 255, and indicates the “brightness” of the three color components,
red, green, and blud. A zero value means off, and a value of 255 means maximum brightness.
All colors can be prepresented (approximately) by combinations of red, green, and blue. The
PixelDiff function is to take two pixels and return the absolute value of the greatest difference
between the corresponding color components. For example,
PixelDiff((5,128,90),(30,7,91))
should return the value 121, because the second (green) components differ by 121, and this
difference is greater than the difference in the other colors (25 and 1 in this case).
1You may choose to do homework 8 instead of or in addition to this homework 7. Your grade will be based on
seven (7) assignments; if you do all eight (8) assignments, your lowest score will be dropped.
2This is such a trivial format that some image viewers don’t even support it. Those that do include Photoshop
and Irfanview; the latter is freeware that you can download from the web.
1
pf2

Partial preview of the text

Download EECS12 Homework 8: Image Processing in Python and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

EECS12 Homework #8 Due: 11:59 PM, Monday, March 21, 2005

In this assignment you will write a Python program that does some elementary image processing.^1 A Python class called Image will be provided for you that reads in images, lets you access the pixels (stored as red-green-blue integer tuples), and lets you write the image back out. The main methods are:

  1. read( filename )
  2. write( filename )
  3. pixel( i, j )
  4. setpixel( i, j, p )

See the file Image.py for a full description of what these methods do, and what the arguments mean. The Image class encapsulates a very simple image reader/writer. It can only handle images that are in “PPM” format, which stands for “Portable Pixel Map.”^2 If you wish to run your program on other images, you will need to first convert them into PPM format.

What You Need to Do

You are to create and submit a Python module named “hw8.py” containing definitions for the following functions (with precisely these spellings, including case):

  1. PixelDiff( p, q )
  2. FindEdges( filename, epsilon )
  3. ShowDiffs( filename )

Be sure to submit only working Python code and comments. This should be a fully functional program that imports Image.py. Here is what the functions should do:

  1. PixelDiff: Pixels in an instance of the class Image store three integers as a tuple: (r,g,b). each integer is in the range 0 to 255, and indicates the “brightness” of the three color components, red, green, and blud. A zero value means off, and a value of 255 means maximum brightness. All colors can be prepresented (approximately) by combinations of red, green, and blue. The PixelDiff function is to take two pixels and return the absolute value of the greatest difference between the corresponding color components. For example,

PixelDiff(( 5 , 128 , 90 ), ( 30 , 7 , 91 ))

should return the value 121, because the second (green) components differ by 121, and this difference is greater than the difference in the other colors (25 and 1 in this case). (^1) You may choose to do homework 8 instead of or in addition to this homework 7. Your grade will be based on seven (7) assignments; if you do all eight (8) assignments, your lowest score will be dropped. (^2) This is such a trivial format that some image viewers don’t even support it. Those that do include Photoshop and Irfanview; the latter is freeware that you can download from the web.

  1. FindEdges is to accept the name of a file and an integer value called epsilon. It should open the PPM image of the given name and process the image as follows. For each pixel compute the “difference” (using PixelDiff) between it and its four neighbors (up, down, left, and right). If any of these differences exceeds epsilon, then we will consider the pixel to be on an “edge” in the original image. This function should create a second image, with exactly the same dimensions as the original image, and write a “white” pixel, (255, 255 , 255), at every location where there is an edge in the original image, and write a “black” pixel, (0, 0 , 0), otherwise. Be sure to give your output image a different name, so that it does not overwrite the origninal image. The image you generate will indicate roughly where the edges are in the original image (depending on the choice of epsilon). Ignore pixels that fall outside the dimensions of the original image. That is when you compute the neighbors of a pixel, and one or more of those neighbors is outside the image, simply assume that the resulting difference is zero. This will only be a problem with pixels along the out edges of the image.
  2. ShowDiffs is similar to FindEdges, but its output is not limited to black and white. It also generates a new image, but its pixels are to indicate how big the computed difference is between a pixel and its neighbors. Simply sum the four differences, clamp at 255 (i.e. make sure the sum does not exceed 255), and set the corresponding pixel in the new image to be (d, d, d), where d is the summ of the differences (clamped at 255). This is a second way to show where the edges are in the original image, but the output image in this case will contain shades of gray, not just black and white.

Demonstrate your programs on one or more images. Include the original images as well as the processed images in a folder called hw8-results. Also include a README.txt file that describes what the images are, and what values of epsilon you used. Submit this zipped (compressed) folder along with your Python program. You are encouraged to find your own images to process. All of the machines in the lab have Irfanview installed, which will allow you to view PPM images, and also convert other formats to PPM, juch as jpeg and gif.