Image Exercises

Setup

Use Language|Add Teachpack... and choose image.ss in the htdp subdirectory. Search in Help Desk for image.ss to find a list of the available functions.

Exercise 1.1, A Face

Use overlay/xy to combine two circles for eyes and a rectangle for a mouth into a face.

Exercise 1.2, Eye colors

Define a function face that accepts an eye color and returns a face with that eye color.

For example, (face 'blue) could be and (face 'green) could be

Exercise 1.3, Bullseye

Define a function that accepts two colors and makes a five ring bulls-eye, alternating between those two colors. Use overlay to combine circles.

Exercise 1.4, Dumbell

Make a dumbell, consisting of two circles with a rectangle between them.

Exercise 1.5, Beside

Write a helper function beside that takes two images and returns an image containing the two input images, one beside the other. Hint: think about the pinholes in the images.

Use beside to make the dumbell from 1.4

Exercise 2.1, Conditionals

Develop the function area-less-than-100 : image -> boolean. It returns true if the total area of the input image is less than 100.

Exercise 2.2, Small overlay

Develop the function small-overlay : image image -> image. If the first image is larger than the second, it behaves just like overlay. If the second image is larger than the first, it reverses the arguments to overlay. Either way (unless the images are the same size), both images should be visible in the final result. Do not forget about helper functions.

Exercise 2.3, Stack

Develop the function stack : image image image -> image. It stacks its input images with the largest on the bottom, the smallest on top, and the middle one in the middle.

Exercise 3.1, Framing

Finish the program frame-person we discussed in lecture. It takes two images as arguments and returns an image. The first input is some picture and the second input is an image inside the first. It returns a new image with the second persons image framed inside. Making test examples can be a bit tricky, so here is an example for you:

(frame-person ) 'shouldbe

Exercise 3.2, Potential Framing

Develop the program maybe-frame-person. It accepts two pictures as in 2.1 and if the second image is inside the first image, it frames the person as before. If the second image is not inside, it returns the original image, unmodified. In addition to the test case above, use this one:

(frame-person ) 'shouldbe

Exercise 4.1, Inverting images

The color struct is defined in the image.ss teachpack, as shown below. Do not duplicate it in your homework, just use the one from the teachpack.

Read up on image->color-list and color-list->image in Help Desk.

The notation number[0-255] means a number between zero and 255.

; a list-of-colors is either
; - empty, or
; - (cons color list-of-colors)

; a color is
; - (make-color number[0-255]
;               number[0-255]
;               number[0-255])
(define-struct color (red green blue)

Write the following function:
; invert-image : image -> image
; to invert (ie, subtract from 255)
; each color component in i
(define (invert-image? i) ...)

Exercise 4.2, Greyscale images

Write the following function:
; grey-image : image -> image
; to produce a grey-scale image from i
(define (grey-image i) ...)

Hint: replace each color with one where the red, green, and blue components are identical and equal to the average of the original.

Exercise 4.3, Mostly red

Write the following function:
; mostly-red? : image -> boolean
; to determine if the sum of the red components
; is larger than the blue and larger than the green
(define (mostly-red? i) ...)

; examples as tests
(mostly-red? ) true
(mostly-red? ) false
(mostly-red? ) true
(mostly-red? ) false
Robby Findler