RU beehive logo ITEC dept promo banner
ITEC 109
2014spring
ibarland

homeinfolabsexamshws
D2LMediaSamples/breeze (snow day)tutor/PIs

hw05
accumulating loops; background substraction

Due May.02 (Fri) 10:00 (hardcopy, and D2L) 04 (Sun) 23:59 on D2L; bring hardcopy to the exam.

  1. (10pts) Write a function countSpaces : String -> int which returns the number of spaces (' ') in a given string.

    (2pts) You can run the following test code; add at least one test-case of your own. (A test case requires both calling the function to get an actual-result, and an expected-result to compare against.)

    # testCountSpaces : -> None
    #
    def testCountSpaces():
        print( "Actual: " + str(countSpaces("a")) )
        print( "Expect: " + str(0) )
    
        print( "Actual: " + str(countSpaces("a ")) )
        print( "Expect: " + str(1) )
    
        print( "Actual: " + str(countSpaces("Radford University")) )
        print( "Expect: " + str(1) )
    
        # Add one additional test of your own, here.
    
        print( "Actual: " + str(countSpaces("")) )
        print( "Expect: " + str(0) )
    
        print( "Actual: " + str(countSpaces("   ")) )
        print( "Expect: " + str(3) )
        
        return None
    

  2. (38pts total) In this problem, we'll write a program to make a “green-screen” special effect. Note that you can get full credit for a later function, if it correctly calls/uses previous functions (presuming those earlier ones really did work correctly).

    Reading: If you're the type who has been reading the book for an alternate view of the material, you've been reading §5.1 (Replacing Colors) and §5.4 (Blending Pictures). This homework problem is the same task as solved in §5.5 (Background Subtraction), except that we are requiring approaching with the specific functions below.

    Although our code won't produce a convincing result (it's hard to get this automated!), there is an optional idea for improving your result below.

    1. (2pts) Make sure you have a couple of images of size 640x480 on your H: drive, to use as backgrounds. (You presumably did this in lab41—creating images; mixing images; you can also look at links—sample images for some images.)

      Rename your files to be of the form bg1-description.jpg and bg2-description.jpg1 (For example, “bg1-mountain.jpg”.)

    2. (2pts) Take a picture of yourself on a uniform-as-possible background (for example: a white wall, or a green sheet draped behind you), and crop the image2 to 640x480. Save the image on your H: drive as yourRuUserId-posed.jpg (e.g.ibarland-posed.jpg”). I recommend a full-body action pose, since it will be superimposed on one of the backgrounds.
      Note: Casting even the slightest shadows on the background can degrade the effect! If using a brightly-colored sheet for the background, try to drape it w/o any wrinkles.
    3. (2pts) Using explore from JES, find and write-down a representative color from your pose's backdrop (its red,green,blue components). Then, in your program, make a variable3 that holds this color:
      COLOR_TO_IGNORE = makeColor( someRedValue, someBlueValue, someGreenValue )
      (e.g. makeColor( 200, 150, 150 )). (You may want to note several different possible color-values in a comment, to experiment with later on.)
    4. (0pts) Be aware there is already a function called JES Functions > Colors > distance, whose type is Color, Color -> float. It returns a notion of how far apart two colors are. For example: distance( black, red ) ⇒ 255.0, distance( black, makeColor(1,1,0) ) ⇒ 1.414 (√2), and distance( green, green ) ⇒ 0.0. The book explains how to calculate such a notion of “distance” between colors.

      Self-understanding question: determine the distance between the COLOR_TO_IGNORE you chose, and makeColor(128,128,128).

    5. (2pts) Make a variable that holds a number: the distance for which we'll consider two colors being “close” to each other. I suggest starting with a distance of about 40 (but later, you should experiment with different values, to get a better final-result).
    6. (10pts) Write the following function, which handles a special-effect at one specific pixel:
      # fxColorAt : int, int, Picture, Picture -> Color
      # Given a foreground image `fg`, a background image `bg`, and an (x,y) location,
      # return the color to use in a “greenscreen” special-effect, as follows:
      #
      #   If `fg`s color at that pixel is “close” to `COLOR_TO_IGNORE`,
      #   then we'll ignore `fg` and instead return the color of the background at that location.
      #   Otherwise, we'll return the color of `fg` at that location.
      #
      #
      def fxColorAt( x, y, foregroundImg, backgroundImg ):
          # your code here
      
      
      Note: don't confuse Pixels with Colors.
    7. (10pts) Write the function
      # fxRow : Picture, Picture, Picture, int -> None
      # Given a (presumably) new/blank picture `fxPict`,
      # along with a foreground `fg` and background `bg` and a row-number,
      # fill in that row of `fxPict`
      # so that each pixel has its color set as per our greenscreen effect.
      #
      #
      def fxRow( fxPict, fg, bg, rowNum ):
          # your code here
      
      
    8. (10pts) Write the function:
      # fx : Picture, Picture -> Picture
      # Given foreground `fg` and background `bg`,
      # return an entirely-new picture which has `fg` superimposed on `bg`
      # as per our greenscreen special effect.
      #
      # Pre-condition: `fg` and `bg` must be the same size (presumably 640x480).
      #
      def fx( fg, bg ):
          # your code here
      
      
      Note that this function will create a new, empty picture; fill every row of it; and then return that newly-created picture as the answer.

      (For debugging/feedback, it's okay to show your empty picture as soon as you create it, and then repaint it inside the loop of this function.)

    9. (Suggested) Try tweaking the values of CLOSENESS_THRESHOLD and COLOR_TO_IGNORE to reduce the number of errors in figuring which pixels of your pose to ignore.
    10. (optional; 5pts extra-credit) Improve your code by having two4 colors-to-ignore, and checking for pixels that are close to one or close to the other.
    11. (2pts) Submit the pictures on D2L with the filenames below. (You'll also need to submit a hardcopy of your program.)

To submit: Bring a hardcopy of your code to class, and submit six files on D2L: your code plus five pictures, named as follows:

See the documentation at JES Functions > Pictures > writePictureTo, for writing out a picture-object to a file.
Note: Be sure to use the filenames with the non-olive-color parts exactly as described — I will have a script which automatically processes the solution, and will look for names of that form!


1 It's okay if you have a different file format — e.g. bg1-mountain.png instead of .jpg. This holds for all image-files for this homework.      

2you can do this in many common photo-processing programs, or even at various on-line websites. If you find a program/site you like, please share it on the discussion boards!      

3 We use all-caps to indicate that this “variable” is actually a named-constant.      

4 Super-challenge: Make a list of colors-to-ignore, and have your code loop through that list! For example:

IGNORABLES = ( makeColor(200,150,150), makeColor(140,200,140), magenta, makeColor(20,30,40) ) # the round-parens make this a list of four colors.

for i in range( len(IGNORABLES) ):
    print( "Processing the color " + str(IGNORABLES[i]) )
           
Note that we can use square-brackets to index into any list/sequence -- not just strings.      

homeinfolabsexamshws
D2LMediaSamples/breeze (snow day)tutor/PIs


©2014, Ian Barland, Radford University
Last modified 2014.May.05 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme