Fundamentals of Computer Science I: Media Computing (CS151.01 2008S)
Primary: [Front Door] [Syllabus] - [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] [Assignment]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Readings]
References: [A-Z] [Primary] [Scheme Report (R5RS)] [Scheme Reference] [DrScheme Manual]
Related Courses: [CSC151.02 2008S (Davis)] [CSC151 2007F (Rebelsky)] [CSC151 2007S (Rebelsky)] [CSCS151 2005S (Stone)]
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.
(image-transform!
image
fun)
image in place by setting
each pixel to the result of applying fun to
that current pixel color.
(compose
f
g)
((compose f g) x)
is the same as (f (g x)).
(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))))).
(rgb-lighter
rgb-color)
(rgb-darker
rgb-color)
(rgb-redder
rgb-color)
(rgb-greener
rgb-color)
(rgb-bluer
rgb-color)
(rgb-rotate
rgb-color)
(rgb-phaseshift
rgb-color)
(rgb-complement
rgb-color)
(image-transform-pixel!
image
column
row
func)
col,row)
in image by applying
func to its old color and setting that
pixel to the resulting color.
In this laboratory, you will be creating a few images and manipulating others. We will also be working with some colors.
a. Create a new 4x3 image, call it canvas, show it, and zoom
in to 16x resolution.
b. Open an existing JPEG image of your choice, call it
picture, and show it. Please choose an image that is
not too large (say, not much more than 250x250).
c. You may have created definitions for three favorite colors,
fave1, fave2, and fave3 in
a previous lab. Check your library to see if they are there. If
not, add some definitions. For example,
(define fave1 (cname->rgb "blue violet")) (define fave2 (rgb-new 240 0 180)) (define fave3 (rgb-new 180 0 240))
a. Set a few pixels in canvas to colors of your choice. Leave
others black or white.
b. What do you expect to happen when you use image-transform!
to complement each pixel in canvas, using the following
instruction?
(image-transform! canvas rgb-complement)
c. Check your answer experimentally.
d. What do you expect to have happen if you use
image-transform! to complement each pixel in
picture? (You would use nearly the same instruction,
substituting picture for canvas.)
e. Check your answer experimentally.
f. What do you expect to have happen if you once again complement each
pixel in picture?
g. Check your answer experimentally.
a. What do you expect to have happen if you use image-transform!
to redden each pixel in canvas?
b. Check your answer experimentally.
c. You may have noticed that in the previous problem, we were able to undo the complement transformation by complementing again. Is there an easy way to undo the redden operation? (You do not have to write code; just explain how to do it.)
d. Are there transformations or sequences of transformations that would be impossible to undo? (That is, can you do something to an image such that there is nothing that you can do to the revised image that will bring back the original image?)
As you may have just observed, there are times that transforming an
image can be dangerous, because we cannot easily undo some transformations.
As an alternative, many programmers build new images that simulate the
transformation, rather than transforming the existing in place. In
DrFu, the image-variant operation does just that. It returns
the identifier of a new image.
a. Consider the following instruction. What effect do you have expect
this instruction to have on canvas?
(define new-image (image-transform! canvas rgb-darker))
b. Check your answer experimentally.
c. Consider the following instruction. What effect do you have expect
this instruction to have on canvas?
(define newer-image (image-variant canvas rgb-darker))
d. Check your answer experimentally.
e. What do you expect to have happen if we show newer-image?
f. Check your answer experimentally.
(image-show newer-image)
g. What do you expect to have happen with each of the following:
(define img1 (image-variant canvas rgb-rotate)) (define img2 (image-variant canvas rgb-lighter)) (define img3 (image-variant canvas rgb-phaseshift)) (image-show img1) (image-show img2) (image-show img3)
h. Check you answer experimentally.
Consider the following definitions.
(define much-darker (compose rgb-darker rgb-darker)) (define red (rgb-new 255 0 0))
a. What color do you expect (much-darker red) to compute?
(Answer the question in terms of red, green, and blue values.)
b. Check your answer experimentally.
c. Set the top-left pixel of canvas to red.
d. What effect do you expect the following instruction to have?
(image-transform-pixel! canvas 0 0 (compose rgb-lighter rgb-lighter))
e. Check your answer experimentally.
f. What effect do you expect the following instruction to have?
(image-transform-pixel! canvas 0 0
(compose rgb-lighter (compose rgb-lighter rgb-lighter)))
g. Check your answer experimentally.
Consider the composition (compose rgb-darker rgb-phaseshift).
a. Does this darken the image first or phase-shift the image first.
b. Does it matter? That is, do you get the same result either way?
c. Check your answer experimentally with
image-variant.
Earlier in this lab, we saw that some transformations had natural inverses and some did not. For example, if you complement a color twice, you get the original color. For many colors, if you lighten and then darken the color, you get the original color. (If any of the components is very large, then we may not be able to restore the original color.)
a. Consider the rgb-redder operation. How would
you write an inverse to that operation using the color-based
transformations along with composition?
b. Test your answer using image-transform.
For example, if you decided that the answer was to make the image
greener and bluer, you might write something like the following.
(define redder-canvas (image-variant canvas rgb-redder)) (define not-redder-canvas (image-variant redder-canvas (compose rgb-greener rgb-bluer))) (image-show redder-canvas) (image-show not-redder-canvas)
In case you were wondering, making the image greener and bluer does not invert the redder operation.
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 either 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 (lambda (c) (* 2 c))) (define t2 (lambda (c) (* 3 c))) (define t2 (lambda (c) (* -1 c))) (define t3 (lambda (c) (* 256 c))) (define t4 (lambda (c) (quotient (+ color-red c) 2))) (define t5 (lambda (c) (- c color-blue)))
Make sure you save your work regularly. Some of these procedures have the potential to crash DrFu.
Primary: [Front Door] [Syllabus] - [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] [Assignment]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Readings]
References: [A-Z] [Primary] [Scheme Report (R5RS)] [Scheme Reference] [DrScheme Manual]
Related Courses: [CSC151.02 2008S (Davis)] [CSC151 2007F (Rebelsky)] [CSC151 2007S (Rebelsky)] [CSCS151 2005S (Stone)]
Copyright (c) 2007-8 Janet Davis, Matthew Kluber, and Samuel A. Rebelsky. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)
This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
This work is licensed under a Creative Commons
Attribution-NonCommercial 2.5 License. To view a copy of this
license, visit http://creativecommons.org/licenses/by-nc/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.