Chapter 15 Exercises

Below is a selection of images that you can use in the programs in this section.

beach.jpgbaby.jpgvangogh.jpgswan.jpg
puppy.jpgkitten.jpggirl.jpgmotorcycle.jpg
gal1.jpgguy1.jpggal2.jpg

Note

Remember that it can take a bit of time to process all the pixels in a picture! Check for errors below the code if it is taking a long time, but if you don’t see any errors just wait.

  1. Make changes to 10 lines in the code below so that it runs. It changes areas that look red in the original to green.

  2. Fix the code below so that the red in the picture gets changed to blue.

  3. Fix the indention in the code below so that it runs correctly. It does a primitive form of edge detection by getting all of the pixels (except for the last row) and all the pixels to the right of those and determining if the difference between the average of the rgb values for the pixel and the pixel to the right are substantially different.

  4. The following program has numerous errors, including missing lines, improper indentation, and other missing syntax. Fix and change the code to change just the background color from white to gray.

  5. Fill in the missing values in the code below so that it runs correctly and posterizes the picture, which means that it reduces the number of colors to a small number, like the ones you might use if you were making a poster.

  6. Fix the indentation so that the code puts the motorcycle on the beach. The code checks if the pixel isn’t white in the first image, and if it’s not, it places that pixel in the same location on the second image.

  7. Fix 6 errors in the code below to copy the non-white pixels from gal1.jpg to guy1.jpg.

  8. The code below was intended to display a swan on the beach, but it isn’t working. Find and fix a single error to make it work.

  9. Change the code below to use if and else rather than two if statements per color to posterize the image.

  10. Fix the indentation in the code and change it so that it edges the motorcycle but the background is black and the motorcycle edging is white.

  11. Change the following code into a procedure that posterizes an image that is passed to the procedure. Be sure to call it to test it.

  12. Fix the errors in the following code so that it displays an edged motorcycle, which means the displayed image should only have 2 colors. The motorcycle should be one color, everything else should be the other color.

  13. Change the following into a procedure. It changes areas that are mostly red looking to green. Be sure to call it to test it.

  14. The code below currently makes the picture mostly gray. Change it so that it posterizes (reduce the number of colors) the image instead.

  15. Write the code to posterize a picture but use 3 values for each color instead of 2. Use 0 if the current value is less than 85, use 85 if the value is less than 170, else use 170.

  16. Fix the errors in the code and change the code to use if’s and else’s instead of just if’s.

  17. Write the code to do edge detection on a picture, but compare the curent pixel with the one below it rather than the one to the right.

  18. Write a procedure that takes an image as a parameter and edges it using the colors blue and white.

  19. Write a procedure to remove the red on very red pixels (pixels that have a red value greater than 200 and a green and blue value of less than 100).

  20. Write a procedure that takes a picture as a parameter and converts all the red to grayscale.

Next Section - Chapter 16 - Working with Collections