Functional Problem Solving (CSC 151 2015F) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric] [Remote Access]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Curtsinger (2015F)] [Davis (2013F)] [Rebelsky (2015S)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
This lab is newly written for Fall 2015. Expect a few bugs.
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)
image, each of whose pixels is computed
by applying fun to the color of the
corresponding pixel in image.
(o
f1
f2
...
fn-1
fn)
f, in turn, starting with
fn and
working backwards. The composition, when applied to a value,
x, produces the same result as
(f1
(f2
(...
(fn-1
(fn x))))).
(section
procedure
value-or-hole-1
...
value-or-hole-n)
procedure, leaving
the rest as parameters for the result procedure. Holes
(parameters for the result proedure) are indicated with the
<> symbol.
(image-load
filename)
(image-save
image
fname)
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)
(irgb-lighter
irgb-color)
(irgb-darker
irgb-color)
(irgb-redder
irgb-color)
(irgb-greener
irgb-color)
(irgb-bluer
irgb-color)
(irgb-rotate
irgb-color)
(irgb-phaseshift
irgb-color)
(irgb-complement
irgb-color)
(irgb-add
irgb-color-1
irgb-color-2)
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)
irgb-color-1 and
irgb-color-2.
(irgb-subtract
irgb-color-1
irgb-color-2)
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.
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.
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/rebelsky/glimmer/samples/rebelsky-pic.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.
As you may recall, the operator
composes multiple procedures , creating a new
procedure that applies each in sequence to the result of the previous
one.
o
Consider the following definition.
>(define irgb-modify2 (o irgb-redder irgb-redder irgb-bluer irgb-bluer))
a. What RGB components do you expect to get if you apply to irgb-modify2(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 to the image you obtained in exercise 1?
irgb-modify2
d. Check your answer experimentally.
Predict the effects of each of the following compound transformations, and then try them on a few sample colors and your image.
a. (o irgb-rotate irgb-rotate irgb-rotate)
b. (o irgb-lighter irgb-lighter irgb-lighter irgb-darker irgb-darker irgb-darker)
c. (o irgb-rotate irgb-rotate irgb-bluer irgb-rotate)
d. An “interesting” compound transformation that you design yourself.
As you may recall from the reading, the
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.
section
Consider the following definitions.
(define add10 (section + 10 <>)) (define add11 (section + <> 11)) (define sub5 (section - 5 <>)) (define sub6 (section - <> 6)) (define mod7a (section mod <> 7)) (define mod7b (section mod 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)___>(mod7a 12)___>(mod7b 12)___>(add10 5)___>(add11 5)___>(sub5 5)___>(sub6 5)___>(mod7a 5)___>(mod7b 5)___
c. What do your results suggest about the behavior of section?
Consider the following definition.
(define proc (o (section * 5 <>) (section - <> 2)))
a. What values do you expect to get when you apply to the inputs proc1, 2, 3, 4, and 5?
b. Check your answer experimentally.
We've just used the procedure
to build arithmetic operations. But we can certainly use it to build
color transformations from the binary color operations. Consider
the following definitions.
section
(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.
a. Suppose we did not have .
How would you define it in terms of irgb-redder,
irgb-add, and/or
irgb-subtract? (Yes, you can also
use irgb-averagecompose and o.)
b. Suppose we did not have
. How would you
define it in terms of irgb-complement,
irgb-add, and/or
irgb-subtract? (Yes, you can also
use irgb-averagecompose and o.)
c. Suppose we did not have
. How would you
define it in terms of irgb-darker,
irgb-add, and/or
irgb-subtract? (Yes, you can also
use irgb-averagecompose and o.)
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. 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?
proc8a
b. 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?
proc8b
c. Give a sample call to .
proc8a
d. Give a sample call to .
proc8b
e. What does do? Come up with
a better name for it.
proc8a
f. What does do? Come up with
a better name for it.
proc8b
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.
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.
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.
Try developing your own interesting RGB transformation procedures and
applying them to picture.