Skip to main content

Laboratory: Transforming Images

Summary: In this laboratory, you will extend the operations you’ve used to transform colors into operations that transform images.

Reference:

(image-variant image fun) MediaScheme GIMP Procedure.
Create a new image of the same width and height as image, each of whose pixels is computed by applying fun to the color of the corresponding pixel in image.
(compose f g) Traditional Higher-Order Procedure.
Build a one-parameter procedure that applies g to its parameter, and then f to that result. ((compose f g) x) is the same as (f (g x)).
(section procedure value-or-hole-1 ... value-or-hole-n) Mediascheme Higher-order Procedure/Macro.
Creates a new procedure by specifying some of the parameters to procedure, leaving the rest as parameters for the result procedure. Holes (parameters for the result proedure) are indicated with the <> symbol.
(image-load filename) MediaScheme GIMP Procedure.
Load an image from a file. The name of the file is a string (and, unless a named value, typically surrounded by quotation marks).
(image-save image fname) MediaScheme GIMP Procedure.
Save image in the specified file (which should provide the full path to the file). The type of the image (JPEG, GIF, PNG, etc.) is determined by the suffix of the file name.
(image-show image) MediaScheme GIMP Procedure.
Opens a new window with the image.
(irgb-lighter irgb-color) MediaScheme Color Procedure.
Build a lighter version of the given color.
(irgb-darker irgb-color) MediaScheme Color Procedure.
Build a darker version of the given color.
(irgb-redder irgb-color) MediaScheme Color Procedure
Build a redder version of the given color.
(irgb-greener irgb-color) MediaScheme Color Procedure.
Build a greener version of the given color.
(irgb-bluer irgb-color) MediaScheme Color Procedure.
Build a bluer version of the given color.
(irgb-rotate irgb-color) MediaScheme Color Procedure.
Rotate the three components of the given color, setting the red component to the value of the green component, the green component to the value of the blue component, and blue compnent to the value of the red component.
(irgb-phaseshift irgb-color) MediaScheme Color Procedure.
“Phase shift” the color by adding 128 to components less than 128 and subtracting 128 from components greater than 128.
(irgb-complement irgb-color) MediaScheme Color Procedure.
Compute the psuedo-complement of the given color.
(irgb-add irgb-color-1 irgb-color-2) MediaScheme Color Procedure.
Add the corresponding RGB components of irgb-color-1 and irgb-color-2. If any component sum is greater than 255, uses 255 for the resulting component.
(irgb-average irgb-color-1 irgb-color-2) MediaScheme Color Procedure.
Average the corresponding RGB components of irgb-color-1 and irgb-color-2.
(irgb-subtract irgb-color-1 irgb-color-2) MediaScheme Color Procedure.
Subtract the RGB components of irgb-color-2 from the corresponding components of irgb-color-1. If any component difference is less than 0, uses 0 for the resulting component.

Preparation

a. Start and connect GIMP and DrRacket using the steps from the previous labs.

b. If you did not already do the preparation from the self-check in the reading, please do it now.

Exercises

Exercise 1: Loading Images

a. Find an image you like, such as your campus directory picture (or President Kington’s directory picture), and save it to your desktop.

b. Using image-load, image-show, and anything else that you think will be useful, load and show that image. Note that the path to the file is likely to be something like "/home/username/Desktop/image.jpg", although you will need to replace the user name and image.

c. Name the loaded image pic.

d. Using image-load, image-show, and anything else you think will be useful, load and show "/home/curtsinger/public_html/images/profile.jpg". Name the loaded image prof.

e. Using image-variant, make prof a bit bluer.

f. Using image-save, save the modified image to your desktop.

g. Try opening the modified image by double-clicking on it.

h. You probably don’t need the modified image any more. Delete it.

Exercise 2: Composed Transformations

As you may recall, the compose procedure composes multiple procedures , creating a new procedure that applies each in sequence to the result of the previous one.

Consider the following definition.

> (define irgb-modify2 (compose irgb-redder irgb-redder irgb-bluer irgb-bluer))

a. What RGB components do you expect to get if you apply irgb-modify2 to (irgb 127 127 127)?

b. Check your answer experimentally by entering the following expression.

> (irgb->string (irgb-modify2 (irgb 127 127 127)))

c. What effect do you expect to get if you apply irgb-modify2 to the image you obtained in exercise 1?

d. Check your answer experimentally.

Exercise 3: A Few Compound Transformations

Predict the effects of each of the following compound transformations, and then try them on a few sample colors and your image.

a. (compose irgb-rotate irgb-rotate irgb-rotate)

b. (compose irgb-lighter irgb-lighter irgb-lighter irgb-darker irgb-darker irgb-darker)

c. (compose irgb-rotate irgb-rotate irgb-bluer irgb-rotate)

d. An “interesting” compound transformation that you design yourself.

Exercise 4: Arithmetic Sectioning

As you may recall from the reading, the section operation creates a new procedure from an existing procedure by filling in one or more parameters. In the reading, we considered section from the perspective of colors. For this lab, we’ll first explore it with numeric operations.

Consider the following definitions.

(define add10 (section + 10 <>))
(define add11 (section + <> 11))
(define sub5 (section - 5 <>))
(define sub6 (section - <> 6))
(define rem7a (section remainder <> 7))
(define rem7b (section remainder 7 <>))

a. What value do you expect to get if you apply each of these procedures to the value 12? What about the value 5?

b. Check your answer experimentally.

> (add10 12)
___
> (add11 12)
___
> (sub5 12)
___
> (sub6 12)
___
> (rem7a 12)
___
> (rem7b 12)
___
> (add10 5)
___
> (add11 5)
___
> (sub5 5)
___
> (sub6 5)
___
> (rem7a 5)
___
> (rem7b 5)
___

c. What do your results suggest about the behavior of section?

Exercise 5: Composing Sections

Consider the following definition.

(define proc (compose (section * 5 <>) (section - <> 2)))

a. What values do you expect to get when you apply proc to the inputs 1, 2, 3, 4, and 5?

b. Check your answer experimentally.

Exercise 6: Building New Transformations

We’ve just used the section procedure to build arithmetic operations. But we can certainly use it to build color transformations from the binary color operations. Consider the following definitions.

(define irgb-modify-6a (section irgb-add (irgb 64 0 64) <>))
(define irgb-modify-6b (section irgb-average (irgb 64 0 64) <>))
(define irgb-modify-6c (section irgb-subtract (irgb 64 0 64) <>))
(define irgb-modify-6d (section irgb-subtract <> (irgb 64 0 64)))

a. What effect do you expect each of these procedures to have on the color (irgb 127 127 127)?

b. Check your answer experimentally.

c. What effect do you expect each of these procedures to have on your sample image?

d. Check your answer experimentally.

Exercise 7: Rebuilding Old Transformations

a. Suppose we did not have irgb-redder. How would you define it in terms of irgb-add, irgb-subtract, and/or irgb-average? (Yes, you can also use compose.)

b. Suppose we did not have irgb-complement. How would you define it in terms of irgb-add, irgb-subtract, and/or irgb-average? (Yes, you can also use compose.)

c. Suppose we did not have irgb-darker. How would you define it in terms of irgb-add, irgb-subtract, and/or irgb-average? (Yes, you can also use compose and o.)

Exercise 8: More Composition

Consider the following definitions.

(define k (image-load "/home/rebelsky/Desktop/kitten.jpg"))
(define proc8a (section image-variant <> irgb-complement))
(define proc8b (section image-variant k <>))

a. proc8a is a unary procedure. What type of parameter does proc8a expect? That is, does it expect a color, a string, an image, a color transformation, a number, or something else?

b. proc8b is a unary procedure. What type of parameter does proc8b expect? That is, does it expect a color, a string, an image, a color transformation, a number, or something else?

c. Give a sample call to proc8a.

d. Give a sample call to proc8b.

e. What does proc8a do? Come up with a better name for it.

f. What does proc8b do? Come up with a better name for it.

Explorations

Explorations are intended for students interested in further exploring the design aspects of these techniques. They also provide students who finish early with extra activities that may challenge them in different ways. You may do them in any order.

Exploration 1: Combining Transformations

While we only have a few basic transformations, there are, in some sense, an infinite number of ways to combine them. Try to find an interesting composition of basic transformations that someone might want to use as a filter.

Exploration 2: Arithmetic Transformations, Revised

As you have undoubtedly noticed, RGB colors are represented as integers. That means that we can transform colors with arithmetic operations as well as with component based operations. What do you think the following operations will do to your image? Try some of them to find out. Then, try a few of your own devising.

> (define t1 (section * 2 <>))
> (define t2 (section * 3 <>))
> (define t3 (section * 1.5 <>))
> (define t4 (section * 256 <>))
> (define t5 (section - <> (irgb 0 0 255)))

Make sure you save your work regularly. Some of these procedures have the potential to crash the GIMP or MediaScript.

Extra 3: Your Own Transformations

Try developing your own interesting RGB transformation procedures and applying them to picture.