CSI Lab 04

Tuesday, February 3rd

Objectives:


Introduction

The material in this week's lab is based heavily on the material in sections 4.2 of your textbook.  At times, we will ask you to work very closely with your book.  At other points, we will ask you to use what you have just done to write some code of your own.  If you get stuck, feel free to refer to the appropriate pages in your textbook.  However, please pay attention to the specific directions in this lab, as some of the activities will ask you to solve a problem in a radically different manner than that used in your book.

As in the past, the instructions are contained on this website, but the answers or signatures that you are required to obtain should be completed on the paper Activity Report which will be provided to you in lab.


Motivation

Last week in Lab 3 I asked you to suppose that you have been hired by the UNI athletic department to help them with some publicity. You were to copy two images to your mediasources file on the p: drive 

To open these, recall that last week I showed you how to use :

> setMediaPath()
> mascot = makePicture("mascot.jpg")
> field = makePicture("footballfield.jpg")

This week we use these images to do some specific manipulations


Activity A : Using a Nested Loop to create a Vertically Mirrored Image

The first thing that the athletic department would like to do is create a picture of TC standing next to himself.  One way to do this is to mirror the left half of the image onto the right half of the image.  We talked about an algorithm for doing this in lecture on Monday without actually developing the code to do this.  Below is code which roughly produces what we were discussing.

Assume that you invoke mirrorVertical by passing it a copy of mascot.jpg.  Look at the code carefully and answer the following questions

[Q1]  Specifically, what value will be assigned to mirrorPoint?

[Q2]  When the inner loop runs for the first time during it's first iteration, what are the coordinates of the pixel assigned to leftPixel?

[Q3]  What are the coordinates of the pixel  assigned to rightPixel?

[Q4]  When the inner loop runs for the last time during it's first iteration (before the outer loop increments the first time) what are the coordinates of the pixel assigned to leftPixel?

[Q5]  What are the coordinates of the pixel assigned to rightPixel?

[Q6]  When the inner loop runs for the last time during it's last iteration  what are the coordinates of the pixel assigned to leftPixel?

[Q7]  What are the coordinates of the pixel assigned to rightPixel?

[Q8]  What is the purpose of the "+1" when calculating the x coordinate of the rightPixel?

 

Create a new file called Lab04.py and add mirrorVertical() as written above.  Run this method using the mascot image.

[SIG1]  When you have the mirrorVertical() method working, demonstrate this to a TA by executing the following commands in the Interactions pane.

    > mascot = makePicture("mascot.jpg")
    > show(mascot)
    > mirrorVertical(mascot);
    > repaint(mascot);

 

As we demonstrated last week, this method works on any picture.   Try it out on the footballfield image. 

    > field = makePicture("footballfield.jpg")
    > show(field)
    > mirrorVertical(field);
    > repaint(field);

Activity B : Using a Nested Loop to create a Horizontally Mirrored Image

While the mirror in Activity A made sense for the picture of TC, we can just as easily imagine a horizontal mirror for some other image.  Add the mirrorHorizontal() method given below to your lab04.py file

[SIG2]  When you have the mirrorHorizontal() method working, demonstrate this to a TA by executing the following commands in the Interactions pane.

    > mascot = makePicture("mascot.jpg")
    > show(mascot)
    > mirrorHorizontal(mascot);
    > repaint(mascot);
[Q9]  Consider the code from Activity A and Activity B.  In what ways are these two methods similar?

[Q10]  In what ways are these two methods different?

[Q11]  In what ways could you modify the mirrorHorizontal() method and still get the same result?


Activity C : Using a Nested Loop to create a Doubly Mirrored Image

The athletic department likes your mirror images of TC so much, they want you to combine the two processes.  In other words, they want you to write a single method whose result is a picture that looks something like:

Write a method called mirrorQuad() which creates an image like the one shown above.  There are SEVERAL ways to do this.  Find one that works for you.

[SIG3]  When you have a version of mirrorQuad(), demonstrate this to a TA.


Activity D : Copying rather than "Mirroring"

Many of you probably didn't like the image we created in Activity C.  It flips TC upside down and gives him a backward jersey.  Suppose we wanted to make a SIMILAR image, but we wanted the four shots of TC to all be facing the "normal" way.  (Kind of a simple, UNI, version of the Brady Bunch, where TC is the only actor!)  What would we need to do?

[SIG4]  Write the method called copyQuad() that produces the image described above.  When you are done, demonstrate this method for a TA.


Activity E : Using a Nested Loop to remove the words

The athletic department is pleased with the mirror images that you have created.  However, they wished that the original image didn't have the words "University of Northern Iowa" on it.  They ask you if there is any way to remove this feature from the image. 

You think about it, and decide:

[SIG5]  Write the method called removeUNIText() that is described above.  When you are done, demonstrate this method for a TA.

 


That is the end of the main part of the lab.

If you finish this lab early and want to challenge yourself, consider completing one or both of the following activities.


Bonus Activity : Solve a more challenging "removeText()".

For this activity, you should work with this modified picture of TC.

Use the picture tool to figure out the boundaries around the "UNI"  Using these values, repeat what you did in Activity D to create a removeUNIText2() .  Notice that when you do this the result doesn't look very nice.

Why?

Figure out how to change how/when you record the color of the pixel that will be the replacement pixel so that the image looks "cleaner."  (HINT : rather than using one pixel for the entire region, you will want to figure out how you use different pixels for different locations withing the replacement region.

[SIG BONUS] When you get removeUNIText2() working, pat yourself on the back, and demonstrate this for a TA.