20120103 CS Project 6 Magic Wand

4
Bard High School Early College Queens Introduction to Computer Science SK1U Fall 2011 Instructor: Matthew Carlberg Project #6: The Magic Wand Tool Due Date: Tuesday, January 17 at 11:59PM Description: In this project, you will be implementing a basic version of the Magic Wand tool that can be found in image processing programs such as Adobe Photoshop or Gimp. The Magic Wand tool allows a user to select and modify regions of an image based on color similarity. We will implement a rudimentary form of the “Magic Wand” tool in Python. Specifically, when a user clicks on a particular pixel in an image, the program will identify the similarly-colored region around that pixel and change that whole region to red. Starter Code: Starter code is located in Python Resources > Unit 5 Code & Images > MagicWand. Programming Requirements & Grading: 1. The Basics (10 points): Create a new folder called “MagicWand” in your Dropbox shared folder. Upon submission, the folder should include the following files: a. magicWandXY.py: This is your Python code. X is your first initial and Y is your last initial. b. floodFillDescription.doc: A written description of how the flood fill algorithm selects and modifies a similarly-colored region in an image (see Problem #6) c. cImage.py d. Tulips.gif e. ProcessedTulips.gif, which shows an example of the result of your code after you have used the Magic Wand tool Turning in the project late or turning it in with missing or misnamed files will result in point deductions. Please do not change the names of any functions or classes in the starter code. 2. Problem #1 (15 points): Create a class called Node. This class stores an (x,y) location of a pixel in an image. x is the column number of the pixel and y is the row number of the pixel. The Node class should have four functions: __init__, getX, getY, __str__, __eq__. Below is test code to help you figure out if your class is working correctly: >>> x1 = Node(1, 3) >>> x2 = Node(8, 9)

Transcript of 20120103 CS Project 6 Magic Wand

Page 1: 20120103 CS Project 6 Magic Wand

Bard High School Early College Queens

Introduction to Computer Science SK1U

Fall 2011

Instructor: Matthew Carlberg

Project #6: The Magic Wand Tool

Due Date: Tuesday, January 17 at 11:59PM

Description: In this project, you will be implementing a basic version of the Magic Wand tool

that can be found in image processing programs such as Adobe Photoshop or Gimp. The Magic

Wand tool allows a user to select and modify regions of an image based on color similarity. We

will implement a rudimentary form of the “Magic Wand” tool in Python. Specifically, when a

user clicks on a particular pixel in an image, the program will identify the similarly-colored

region around that pixel and change that whole region to red.

Starter Code: Starter code is located in Python Resources > Unit 5 Code & Images >

MagicWand.

Programming Requirements & Grading:

1. The Basics (10 points): Create a new folder called “MagicWand” in your Dropbox

shared folder. Upon submission, the folder should include the following files:

a. magicWandXY.py: This is your Python code. X is your first initial and Y is

your last initial.

b. floodFillDescription.doc: A written description of how the flood fill

algorithm selects and modifies a similarly-colored region in an image (see

Problem #6)

c. cImage.py

d. Tulips.gif

e. ProcessedTulips.gif, which shows an example of the result of your code after

you have used the Magic Wand tool

Turning in the project late or turning it in with missing or misnamed files will result in

point deductions. Please do not change the names of any functions or classes in the

starter code.

2. Problem #1 (15 points): Create a class called Node. This class stores an (x,y) location

of a pixel in an image. x is the column number of the pixel and y is the row number of

the pixel.

The Node class should have four functions: __init__, getX, getY, __str__, __eq__.

Below is test code to help you figure out if your class is working correctly:

>>> x1 = Node(1, 3)

>>> x2 = Node(8, 9)

Page 2: 20120103 CS Project 6 Magic Wand

>>> x3 = Node(1, 3)

>>> print(x1)

(1, 3)

>>>print(x1.getX())

1

>>>print(x1.getY())

3

>>>print(x1 == x2)

False

>>>print(x1 == x3)

True

3. Problem #2 (15 points): Create a class called Stack. A stack is a specialized list that

processes data in a “last in first out” fashion (to be discussed more in class). The only

data stored by the class is a list, which starts out empty. The Stack class has four

methods:

a. __init__: Initializes a stack by creating an empty list

b. put: Adds an item to the end of the list.

c. get: Returns the last item of the list AND removes it from the list. This function

should return None if the list is empty.

d. isEmpty: Returns True if the list is empty and False if the list is not empty.

Below is test code to help you figure out if your class is working correctly:

>>> s = Stack()

>>> print(s.isEmpty())

True

>>> s.put(4)

>>> s.put(5)

>>> print(s.isEmpty())

False

>>> print(s.get())

5

>>> print(s.get())

4

>>> print(s.isEmpty())

True

4. Problem #3 (10 points): Write a function called colorDifference. This function takes

two pixels, both with RGB values, as input. It returns a decimal number that represents

how different the colors of the two pixels are. If the colors of the two pixels are the exact

same, this function will return 0, and if the colors of the two pixels are very different, this

function will return a large positive number.

Page 3: 20120103 CS Project 6 Magic Wand

Specifically, if one pixel has RGB values (r1, g1, b1) and the other pixel has RGB values

(r2, g2, b2), this function returns: √

Below is test code to help you figure out if your function is working correctly:

>>> print(colorDifference(Pixel(124, 255, 90), Pixel(124, 255, 90)))

0.0

>>> print(colorDifference(Pixel(124, 255, 90), Pixel(128, 244, 10)))

80.8517

5. Problem #4 (10 points): Write a function called inImage. As input, the function takes a

Node (i.e. an (x,y) pixel location) and an image. The function returns a Boolean (True or

False). It returns True if the (x,y) pixel location is within the bounds of the image, i.e. the

x coordinate is between 0 and 1 less than the width of the image and the y coordinate is

between 0 and 1 less than the height of the image.

Below is test code to help you figure out if your function is working correctly:

>>> img = EmptyImage(30, 20)

>>> print(inImage(Node(0, 0), img))

True

>>> print(inImage(Node(29, 19), img))

True

>>> print(inImage(Node(-1, 0), img))

False

>>> print(inImage(Node(10, -1), img))

False

>>> print(inImage(Node(35, 10), img))

False

>>> print(inImage(Node(10, 20), img))

False

6. Problem #5 (20 points): Write a function called floodFill. Pseudocode for the flood fill

algorithm will be distributed as a separate handout at a later date.

To test this code, all you should have to do is to call the function main() and see if your

code implements the Magic Wand tool appropriately.

7. Written Problem #6 (20 points): Describe in paragraph form how the flood fill

algorithm selects and modifies a similarly-colored region in an image. A full description

of the algorithm will likely include one or more diagrams showing the order in which

pixels are processed and under what conditions a pixel is changed from its own color to

the replacement color (red). You may research the flood fill algorithm online if you

desire, so long as you cite your sources. Your writing should be understandable to

someone who has programming experience but HAS NOT seen your code. A general

guideline for appropriate length is 1 to 2 paragraphs. Note: You do not have to

successfully implement the code in order to get full credit for this problem.

Page 4: 20120103 CS Project 6 Magic Wand

Details of Flood Fill Algorithm (Problem #5)

Function Definition:

def floodFill (baseNode, image, targetColor, replacementColor):

Parameters

1. baseNode (Node object). (x,y) location around which you wish to find the similarly

colored region. This corresponds to the (x,y) location at which the user clicked on the

image.

2. image (FileImage object). Image that is being processed. The image is a matrix of RGB

pixels, which are accessed by (x,y) locations. A pixel is retrieved from the image using

the getPixel function and is modified using the setPixel function.

3. targetColor (Pixel object). RGB value of baseNode. The algorithm finds pixels that are

similar to this RGB color.

4. replacementColor (Pixel object). RGB value that will be used to replace all of the pixels

in the similarly-colored region around baseNode.

Pseudocode:

1. Create an empty stack.

2. Put baseNode onto the stack

3. Mark baseNode as seen

4. While the stack is not empty:

5. Get node n from stack.

6. If the color of n is similar to targetColor:

7. Set the color of n to replacementColor.

8. Define the west node.

9. If the west node is within the image bounds and hasn’t been seen

Put west node onto the stack.

Mark west node as seen

10. Repeat step #9 with the east node, south node, and north node.

Notes:

North node = pixel location in image directly above node n.

South node = pixel location in image directly below node n.

West node = pixel location in image directly to left of node n.

East node = pixel location in image directly to right of node n.

The way I suggest you keep track of whether or not a node has been “seen” is to use a matrix of

zeros and ones whose width and height are the same as the image’s width and height. A zero at a

pixel location (x,y) means the pixel has not been seen yet. A one at a pixel location (x,y) means

the pixel has been seen already. At the start of the flood fill code, you want the matrix to be

filled with zeros because none of the pixels have been seen. In the starter code, a matrix of all

zeros called pixelSeen has already been initialized for you.