SC101Assignment3 Eng

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Assignment 3

This assignment is based on the Assignment 3 of CS106AP at Stanford University

In the rst half of this assignment, we will become more familiar with the
rst data structure we learned in Python - "List"! And in the second half, we
will experience what a software engineer often encounters - "continuing the
code written by others" and completing the entire program! Please note that
for this assignment, you will have to use the terminal in PyCharm and run
all of the programs from the command line.

Before starting the assignment, please watch the video provided below

https://youtu.be/BXZ7XeN9Eys

Download

Should you have any questions, feel free to ask your TA! Conceptual
discussions are encouraged while working through each problem set.
However, do not show your codes to anyone else except your TA. The
plagiarism checker would warn the Professor of a potential Honor Code
Violation.
fi
fi
stanCodoshop.py

stanCode intends to launch a brand new APP - "stanCodoshop," which

allows users to remove random passers-by from beautiful landscape photos!

Suppose the user imports the three pictures shown above - "photos of

Hoover Tower of Stanford University, with pedestrians walking through" to

our magical "stanCodoshop" APP. In that case, the user will get a photo with

a clear view of Hoover Tower (as shown below).


Algorithm

Suppose the user has imported several pictures of the same size, and the
positions of random passers-by in the photos are di erent. Thus, we will
take pixels that exist at the same coordinate (x, y) in each respective picture
and compare them one by one. Afterward, we will nd a pixel not a ected by
pedestrians and put it at the same coordinate in a "blank picture of the same
size as the photo imported." When all coordinate of the blank image is lled
with the pixels we choose, our picture with a clean landscape is produced!

However, how can we determine which pixel is not a ected by passers-by


within all pixels that exist at the same coordinate (x, y) from the N photos?
For example, suppose the user has imported four pictures, (r, g, b) value of

four pixels at the same coordinate (x, y) in each image are:

pixel_pic1: (1, 1, 2) pixel_pic2: (1, 1, 1)


pixel_pic3: (1, 2, 2) pixel_pic4: (28, 27, 29)

From their (r, g, b) values, we can nd out that the value of pixel_pic4 is
di erent from the three other pixels that exist at the same location. In this
case, we can assume a high probability of having a person (or a dog) pass
by at the (x, y) position of the fourth photo(pic4)!
ff
fi
fi
ff
ff
ff
fi
Color Distance

We can easily nd the strangest pixel from the example above, yet, how can
we distinguish the odd pixel with a principle?

Here, we would like to introduce an algorithm called "color distance" to help


us objectively compare the value of pixels. For example, we can think of
each pixel's RGB value as a point in a 3-dimensional color space, with
the x-y-z coordinates we are familiar with being replaced with the red-
green-blue value. If we put four points in the space (as in the image shown
below), the linear distance between the points is called "color distance."

Figure1. Schematic diagram of (r, g, b) coordinate points of di erent pixels

exist in the same (x,y) position from various images in a 3D space.

Suppose we respectively compute the average of the RGB values from pixels
across all images at the same coordinate(x,y); we will have the value
of red_avg, green_avg, and blue_avg. By adding these average RGB values
to our 3D space, we will create another point named "avg_point." Then,
using the formula below, we can calculate the "color distance" between

a pixel point and this avg_point.


fi
ff
Milestone 1 - get_pixel_dist(pixel, red, green, blue)

Your task is to write the code for the get_pixel_dist(pixel, red, green,

blue) function of the stanCodoshop.py le. This function returns the color

distance between a pixel and the avg_point. A given red, green, and blue

values are the RGB values of the avg_point.

Test
To check whether the function you wrote works, please add the three-line

codes in the red box below to the "def solve(images):".

Click and open the terminal window in Pycham:


for macOS please enter: python3 stanCodoshop.py hoover
for Windows please enter: py stanCodoshop.py hoover

If you see the words below, Milestone 1 is complete!


fi
Milestone 2 - get_average(pixels)

Please complete the get_average(pixels) function, and return a Python List

that contains the average RGB values: [red, green, blue].

The parameter - pixels of this function is a Python List that contains a total

of N pixels at the same location(x, y) from the N pictures.

Your task is to average all the red values of the given (x, y) pixels to get the

average red value, and do the same for green and blue. And please contain

the values in a Python List and return it in the the order [red, green, blue].

Test
Please modify the code in "def solve(images)." Change the code into the

four-line code shown in the red box in the gure below to check whether the

function of this function is correct:

Click and open the terminal window in Pycham:


for macOS please enter: python3 stanCodoshop.py hoover
for Windows please enter: py stanCodoshop.py hoover
fi
If you see the words below, Milestone 2 is complete!

==================================================

Milestone 3 - get_best_pixel(pixels)

Please call the functions you have de ned in Milestone 1 and Milestone 2 to
complete the get_best_pixel(pixels) function. This function will return the
best pixel, which has the closest distance between the avg_point among all
pixels and will be the one to put on the blank image!

Test
Please change the code in "def solve(images)." into the ve-line code
shown in the red box in the gure below to check whether the function of
this function is correct:
fi
fi
fi
Click and open the terminal window in Pycham:
for macOS please enter: python3 stanCodoshop.py hoover
for Windows please enter: py stanCodoshop.py hoover

If you see the words below, Milestone 3 is complete!

==================================================
Milestone 4 - solve(images)

Now comes to our last step to build the "stanCodoshop" App. Please delete

all the code shown in the above red box that you have written in "def

solve(images).”

def solve(images) takes in a Python List - photos containing various images

taken in the same place (All images will be in the same size). Your task is to

nd the best pixel in each (x, y) position among all images and add this pixel

to the exact position of the blank image – result.

We already wrote result.show( ) at the end of def solve(images). Thus, if you

complete this assignment successfully, within 30 seconds, "an image of

attraction without passers-by" will appear on your screen.

To ensure you have completed this assignment, we provide you with four

sets of images to check (all are well-known spots at Stanford). Please type

in the command lines provided below in the terminal window in Pycharm.

macOS/ Windows/
1. python3 stanCodoshop.py clock-tower 1. py stanCodoshop.py clock-tower
2. python3 stanCodoshop.py hoover 2. py stanCodoshop.py hoover
3. python3 stanCodoshop.py math-corner 3. py stanCodoshop.py math-corner
fi
GRADING
Functionality - Does the program meet our basic requirements? The program
must be bug-free, complete the assigned task successfully, and ensure that the
program does not get stuck in any in nite loop.

Style - Good programs should have good usage instructions and be easily
understood, so that people around the world can use your code to build more,
bigger and more interesting programs. Therefore, please write concise usage
instructions, function descriptions, and single-line comments.

SUBMISSION
1. Select all the les, right click, and choose “compress les” or “zipped”
macOS:

Windows:
fi
fi
fi
2. Rename your compressed/zipped le as ”a(n)_name”. For instance:
assignment 0 -> a0_jerry_liao;
assignment 1 -> a1_jerry_liao. Ect

3. Upload (.zip) to your Google drive


1) Search for "Google Drive"
2) After logging in, click "New" in the upper left corner → "Upload les" →
select the assignment compressed le (.zip)

4. Click the share button and copy the link


1) Right-click on the le, select "Share"
2) After clicking "Change anyone with the link's permissions”, the permissions
will be changed to “Viewable".
3) Click "Copy link”

5. Paste the link to the "Assignment Submission Form" provided in the


assignment post in the Facebook group.
fi
fi
fi
fi

You might also like