Image Exercises


The teachpack and .plt files have changed for assignment 3. Re-download and re-install them.

You must install utils-151.plt in your DrScheme v205. After downloading the file, choose File|Install .plt file to install it. Also, download this teachpack: for use with these assignments. Use Language|Add Teachpack... once you've downloaded the teachpack.

Teachpack Documentation

Note: you do not need all of these operations for each assignment.

image=?:image image -> bool
Takes two images and returns true if the images are the same, false otherwise.
image-width:image -> num
Takes an image and returns its width in pixels.
image-height:image -> num
Takes an image and returns its height in pixels.
image+:image image -> image
Takes two images image and produces an image where the second is placed on the first. The top-left of the result image corresponds to the top-left of the given images. The white pixels are treated as invisible in the second image
offset-image+:image num num image -> image
Like image+, but with two number arguments inserted between the image arguments. The first number is the number of pixels right to shift the second image, and the secnd number is the number of pixels down to shift the second image.
solid-box:num num symbol -> image
Takes two numbers and a symbol and produces an image. The first number determines the width of the result image and the second number determines the height. The symbol determines the color that fills the entire result image. See also the list of available colors.
outline-box:num num symbol -> image
Like solid-box , except that only the border of the image uses the specified color, and the rest of the image is white.
solid-dot:num num symbol -> image
Like solid-box , except that only a circle bounded by the image is filled with the specified color, and the corners of the image are white.
outline-dot:num num symbol -> image
Like solid-dot , except that only the circle edge bounded by the image is draw with the specified color, and the rest of the image is white.
image-inside?:image image -> bool
Returns true if the second image appears somewhere in the first, false otherwise.
find-image:image image -> posn
Returns a posn representing the location of the second image within the first. (If the second image does not appear in the first, an error is reported.) The result posn indicates the location of the second image's top-left corner relative to the first image's top left corner. The X part of the posn indicates the number of pixels to the right that the second image's corner is shifted, and the Y part indicates the number of pixels down that the second image's corner is shifted.
(define-struct color (red green blue))
Colors on a computer screen are represented by a combination of red, green, and blue light intensities. The intensity of each color varies between 0 and 255. For example, pure blue is (make-color 0 0 255), while (make-color 0 0 128) is a dim blue. Using the same intensity for red, blue, and green always produces a shade of gray; in particular, (make-color 0 0 0) is black, (make-color 255 255 255) is white, and (make-color 128 128 128) is a medium gray. Other combinations produce other colors. For example, (make-color 255 255 0) is bright yellow.
image->color-list:image -> list-of-color
Returns a list of color values representing the pixels of a given image. The first color corresponds to the top-left pixel, the second color corresponds to the pixel one step to the right, and so on --- left-to-right then top-to-bottom. The number of colors is in the list is the same as the image's width times its height.
color-list->image:list-of-color num num -> image
The opposite of image->color-list. This function needs the width and height of the image to construct, in addition to the pixel content.

Exercise 1.1, You

Define the constant me to be a 128x128-pixel image of yourself.

If you cannot obtain a picture of yourself in electronic form, find or create any 128x128-pixel image. We ask for a picture of you to help us learn your name and match it with a face.

For best results with the following exercises, choose your image so that your eyes are centered in the image, and with the distance from the centers of your eyes at about 64 pixels.

Exercise 1.2, Disguises

Define the function add-glasses that takes two images: a person image, and an image (of any size) reprsenting glasses. The result should be an image with the given person wearing the glasses, which is constructed by centering the glasses image on top of the person image, and using the glasses image as its own mask.

The function should not assume anything about the size of the images.

Here are some glasses images that you can use for testing:

Here is an example:

Exercise 1.3, More Disguises

Define the function add-beard that is like add-glasses, but the second image is a beard image. The beard should be centered horizontally, but bottom-aligned with the person image.

The function should not assume anything about the size of the images.

Here is a beard image that you can use for testing:

Exercise 1.4, Recognition

Define the function wearing-glasses? that takes two person images and a glasses image. It returns true or false . The result is true if the second image matches the first image plus glasses (so the result is true only when the second image already includes glasses).

Exercise 1.5, Disguises

Define the function same-person-maybe-disguised? that takes four images: a source person image, a target person image, a glasses image, and a beard image. It returns true or false. The result is true if the second image is that same as the first, possibily with glasses and/or beard added.

Exercise 2.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 2.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 3.1, list-of-color

Write a data definition for list of color (using the define-struct for color given above).

Exercise 3.2, Black & White

Develop the program black-and-white. It accepts an image and determines if it is a black and white image, that is, the function only has black, white, and grey pixels.

Exercise 3.3, Kind of Blue

Develop the program kind-of-blue?, which takes an image and returns true if the total blue intensity of pixels in the image is greater than the total red intensity, and also greater than the total green intensity.
Hint: write three helper functions to compute three different total intensities and another helper function to compare the intensities.
For example, is not kind-of-blue, but is kind-of blue.

Exercise 3.4, Inversion

Develop the program invert, which takes and produces an image. The resulting image should be just like the input, except each pixel's color should be inverted. To invert a pixel, subtract each of its red, green, and blue coordinates from 255. For example, the pixel (make-color 55 100 155) would become (make-color 200 155 100).

Robby Findler