Homework 1

< CS101

These first 3 exercises use simple <, not the average-test technique. The resulting color areas will not be exact; just get it kind of close.

Exercise 1

Consider fence-ivy.jpg

Write code to detect the green leaves, and set their green value to zero. The effect is to make the leaves a sort of violet.


Exercise 2

Write code for fence-ivy.jpg detect the red fence boards, and make them lighter by doubling each of their red, green, and blue values. The result make the fence look sort of light and weathered.


Exercise 3

Suppose you got a parking ticket here

Write code to detect the red curb and change it to gray.



Now using the if-avg technique:

Exercise 4

Night mode: for the stop sign, detect the blue sky and turn it dark. The tricky part is detecting the blue sky vs. the rest of the image. First try setting the sky pixels to pure black. That looks a little harsh. Try setting the red green and blue to 30 for a sort of dusky look.


Exercise 5

Rather than detect the red parts of the sign, detect the white parts. Set them to be pure blue, so we have blue letters on a red sign. How to detect the white areas?

Hint: a white area is one where all three colors are pretty high (say over 200). What will the average be for a white pixel?


Exercise 6

Write code to set the left half of stop.jog to pure black. Use image.getWidth().


Exercise 7

Write code to set the bottom 33% of stop.jpg to pure black. Use image.getHeight().


Exercise 8 - Monkey Moon

Now you do one just like Hollywood. Here we have monkey.jpg and moon.jpg. Select the blue background pixels behind the monkey, and for each such pixel, copy over the corresponding pixel from the moon.jpg; the result is the monkey hanging out on the moon. The blue background behind the monkey is a little dark, however the monkey (brown and yellow) has very little blue in it, so you can figure out a * factor that separates the monkey and its blue background.

Experiments: We also have paris.jpg and yosemite.jpg -- try using those as the background instead of moon.jpg. Note that your if-logic does not look at the background at all ... the detection logic is all on the monkey image, so there's no limit about the colors or whatever in the background. That is, there's lots of blue in paris.jpg but that does not mess things up.

The paris.jpg image is much larger than the monkey image. We end up just using the upper-left quarter of paris.jpg. The image code supports a setSameSize() function which can be used to scale an image up or down in size to match the size of another image. Call it like this to scale the paris.jpg back image to be the same size as the monkey image: back.setSameSize(image); -- the monkey will appear with the full size paris or yosemite or whatever background image.

Previously we relied on the fact that the background images are at least as large as the foreground images. If they backgrounds were smaller without resizing -- say the monkey image was 400 pixels wide, but the moon image was only 300 pixels wide -- we would get an out-of-bounds error when trying to copy non-existent pixels from the moon image. If you have that problem, use setSameSize() as show above.

Exercise 9 - Totoro

Here we have the totoro.jpg image of the character from the movie My Neighbor Totoro, (BTW a great kid movie from the creator of Spirited Away). Write code to show Totoro on the moon or paris or yosemite.

Exercise 10 - Fence

Write code to change fence-ivy.jpg so the leaves in the image are bluescreen replaced with pixels from leaves.jpg -- in effect the leaves in the fence image are replaced with smaller leaf pattern.

Due 11:59 pm Mon Jan 30 (oops, said 31 here briefly)

Submission instructions: 1. On this page, click the button below to collect the code answers to all the questions as one block of text. Copy that text.
2. Now navigate to Coursework ---> Assignments --> Homework 1. There should be one question in Homework 1, where you will paste in the text of all your answers. If doing this with a partner, be sure to note your partner's name at the very top of the text.
3. Submit the assignment in Coursework and you're done!

Retrieve Code

Retrieves locally stored exercise code for this page from the 'Run' button:

(code appears here)