# Homework 1

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.

### hw2-ex1

 image = new SimpleImage("fence-ivy.jpg"); for (pixel: image) { // your code here } print(image);

## 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.

### hw2-ex2

 image = new SimpleImage("fence-ivy.jpg"); for (pixel: image) { // your code here } print(image);

## Exercise 3

Suppose you got a parking ticket here

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

### hw2-ex3

 image = new SimpleImage("curb.jpg"); for (pixel: image) { // your code here } print(image);
curb.jpg

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.

### hw2-ex4

 image = new SimpleImage("stop.jpg"); for (pixel: image) { // Your code here } print(image);

### 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?

### hw2-ex5

 image = new SimpleImage("stop.jpg"); for (pixel: image) { // Your code here } print(image);

### Exercise 6

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

### hw2-ex6

 image = new SimpleImage("stop.jpg"); for (pixel: image) { // Your code here } print(image);

### Exercise 7

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

### hw2-ex7

 image = new SimpleImage("stop.jpg"); for (pixel: image) { // Your code here } print(image);

## 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.

 image = new SimpleImage("monkey.jpg"); back = new SimpleImage("moon.jpg"); for (pixel: image) { avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue()) / 3; // your code here } print(image);

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.

 image = new SimpleImage("totoro.jpg"); back = new SimpleImage("moon.jpg"); for (pixel: image) { avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue()) / 3; // your code here } print(image);

## 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.

 image = new SimpleImage("fence-ivy.jpg"); back = new SimpleImage("leaves.jpg"); for (pixel: image) { avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue()) / 3; // your code here } print(image);

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)