Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Homework 5

Fudan - Intro to Python

(No late submission is allowed)

Do NOT import any outside libraries. Python built-in functions that we've covered in class sumce to solve the problems.

Provided Files

None

File(s) to edit and submit

  change_background.py (Task 1)

def  change_background(image,  new_background,  replace_color)

def  color_distance(color1,  color2)

  lip.py (Task 2)

def  flip_vertical(image,  col_index,  row_index,  height,  width)

def  check_validity(image,  col_index,  row_index,  height,  width)

Part 1: Implementation

Background: Through lectures, you learned about how to represent images and how to transform  images. In this lab, you will gain some hands-on experience working with images by changing image background and lipping a certain region in the image.

Task 1: Change Image Background ( points)

To further develop your understanding of how to manipulate images in python, now it’s your turn to implement changing the image background.

First of all, you need to ind a place of a solid color wall and take a photo of yourself or together with your friends in front of the green screen. Feel free to look at the camera and do any pose

you want. Feel free to refer to the one below as an example, but please do NOT use the following image. You should take your owns, and be creative :)

 

Then you need to pick a background image that you like to replace the green screen in the     original image. The background image is supposed to be bigger than the source image. Again, this is just an example and you shouldn't simply use this one for the PA.

 

After you change the background, the new image will look something like the one below, albeit it isn't a perfect one. Feel free to play around with your functions a bit to eliminate as much      green background as possible

 

  YOUR TASK: Implement two functions, and provide your custom image

    def  change_background(image,  new_background,  replace_color)

    image  is the source image (2D list of RGB tuples) that you take in front of the green

screen

is the background image that you want to use to replace the green

  To make your life easier, you can assume both of the 2 arguments above are

2D list of tuples

replace_color

is a tuple of RGB value that will be replaced in the source image. In

our example, the replace_color is green. You need to decide the RGB value yourself. (More details below)

this function should return

image

None

and do manipulation directly on the argument

def  color_distance(color1,  color2)

color1  is the pixel color in the source image

color2  is the replace_color (some green RGB value)

You need to calculate the distance between the two color using the following formula. Feel free to import  math  and use the sqrt function

  distance =    (r1 − r2)2 + (g1 − g2)2 + (b1 − b2)2 .

  After you've generated your output image, name it creative .png  in your workspace.

We'll download everyone's workspace and automatically take out that ile from everyone. If you don't name it like so, it's very likely that your image won't be caught by the auto-    fetching script and we will not be able to witness your creativity

   More explanations: What you need to do is, if the color_distance  of the pixel color in the 

source image and the   replace_color  is within a threshold, you need to replace the source   pixel color with the corresponding pixel color in new_background . You will decide the threshold! Feel free to come up with one from your intuition and run multiple experiments to

see if the new image looks good enough.

We will grade your code and look at your output pictures manually. We will have a vote for

everyone's picture and give extra points to the top 10, so please unleash your creativity.

  You can use the methods provided in the  FudanImgLib .py  library to test your solution using

the terminal. Useful functions include but are not limited to:  load_img() , save_img() .

Note that you need to close and reopen the ile when you save something new to the same ile, otherwise the ile content does not automatically refresh.

Starter code for your change_background .py

from  FudanImgLib  import  *

import  math

def  change_background(image,  new_background,  replace_color):

#TODO

def  color_distance(color1,  color2):

#TODO

load  the  source  image  and  new  background  image

change  the  background

save  the  image  to  a  new  file

Task 2: Flip region

def  flip_vertical(image,  col_index,  row_index,  region_height,  region_width)

We want to select a speciic rectangular region of the image and lip the region top to bottom.

  input:

    image : 2D list of tuples representing RGB values

    col_index : an integer value that represents the starting column of the pixel at the upper-

left corner of the selected rectangular region

    row_index : an integer value that represents the starting row of the pixel at the upper-left

corner of the selected rectangular region

    region_width : the width of the rectangular region that we want to lip

    region_height : the height of the rectangular region that we want to lip   output:

  return  False  if parameters are not valid and no operations would be done to the image   return True  if the image is lipped successfully

def  check_validity(image,  col_index,  row_index,  region_height,  region_width)

You would implement another function to check if input parameters are valid. Input parameters are   the same as above, and you need to return True  if parameters are valid, and  False  otherwise. Rules for valid parameters are as follow:

1.   col_index,  row_index,  region_height,  region_width  should all be greater than or equal to 0

2.  The rectangular region speciied by the parameter should be entirely inside the image

3.  You can assume image  is always a valid 2D list of tuples and we don't test on this edge case

The speciedipping region is illustrated below:

[ Example 1 ]:

image  =

[[(240, 240, 0), (200, 100, 0), (240, 240, 0), (200, 200, 0)],

[(160, 240, 0), (140, 200, 0), (160, 240, 0), (120, 200, 0)],

[(240, 240, 0), (200, 200, 0), (240, 240, 0), (200, 200, 0)],

[(160, 240, 0), (120, 200, 0), (160, 240, 0), (120, 200, 0)]]

 

If we call  flip_vertical(img,0,0,4,2) ,

image  becomes

[[(160, 240, 0), (120, 200, 0), (240, 240, 0), (200, 200, 0)],

[(240, 240, 0), (200, 200, 0), (160, 240, 0), (120, 200, 0)],

[(160, 240, 0), (140, 200, 0), (240, 240, 0), (200, 200, 0)],

[(240, 240, 0), (200, 100, 0), (160, 240, 0), (120, 200, 0)]]

The bolded region is the region selected by this function call.

[ Example 2 ]

Original image (200 x 300)

 

If we do following steps:

image  =  load_img('input .png')

flip_vertical(image,  100,  60,  120,  120)

save_img(image,  'flipped .png')

We got the image below

 

from  FudanImgLib  import  *

def  check_validity(image,  col_index,  row_index,  region_height,  region_width):

#  TODO

def  flip_vertical(image,  col_index,  row_index,  region_height,  region_width):

#  Check  validity  of  input  parameters

if  not  check_validity(image,  col_index,  row_index,  region_height,  region_width): return  False

#  TODO

#  Return  True  if  successfully  flipped  the  image

return  True

Submission

change_background.py

flip.py