Lab 7
Images Using a GrayScale
A black and white picture uses various shades of gray.
These shades of grey all have the same amount of red,
green and blue color. The higher the color value, the
lighter the shade of gray is. The lower the color
value, the darker the shade of gray is. For example,
look at the following squares for various shades of gray.
The amount of red, green and blue for the color is displayed
as text in each square. Note that the amount of red,
green and blue is the same in each square. Also note that
the larger the amount of color, the lighter the color of gray.
r = 0 g = 0 b = 0
| r = 63 g = 63 b = 63
| r = 127 g = 127 b = 127
| r = 191 g = 191 b = 191
| r = 255 g = 255 b = 255
|
Changing the color in a pixel
The color in a pixel can be changed by using the setRed(),
setGreen() and setBlue() functions in the
Pixel class as part of the Java bookClasses.
Determining the amount of gray
One method to determine which grayscale color to use
for a pixel when creating a black and white picture
from a color picture is to average the amount of
red, green and blue color the corresponding pixel
in the colored picture. This method works fairly
well and is fairly easy to understand and use.
This way uses the intensity of each color to determine
the grayscale value.
Another method is to take a weighted average of the
amount of red, green and blue color instead of an
evenly-weighted average. A weighted average would
give more weight to certain values and less weight to
other values. For example, a weighted average will be used when
determining the grade for this course. Exams have a higher
weight (i.e. more impact on the final grade), while
lab assignments have a lower weight (i.e. less impact
on the final grade). For determining the amount of
color for the graycolor color, the original amount of
green is often given a higher weight while the original
amount of blue is given a lesser weight. The percent
of each color that should be used according to some
research done on luminance is:
Using this weighted value is what was done for the code
written in class for Lect107i.java.
Lab Assignment 7
Due: Thursday 10/16/2008 by 11:59 pm
Write two Java programs that will do the following:
-
Program 1: Make Black And Red
- The Java program is to be named: lab7a
- Print out your name and your net-id
- Allow the user to select a picture from a file stored on
the local machine
- Change the image to a "red scale" image. This is done in a similar manner
as the creation of a grayscale image, except the values for green and blue are
kept at zero. Only the red value for each pixel will range from 0 to 255.
Where the grayscale image is black, the red scale image will also be black.
Where the grayscale image is white, the red scale image will be red.
Where the grayscale image is gray, this image will range from black to red
(the middle values will be a shade of maroon).
Note: This is NOT done
by simply setting the green and blue color values to 0 and leaving the red
value alone. You must modify all three color values in every pixel.
- Display the modified image
- Save the modfied image to a file on the local machine
-
program 2: Make Red And White
- The Java program is to be named: lab7b
- Print out your name and your net-id
- Allow the user to select a picture from a file stored on
the local machine
- Change the image to a version of a red scale image. This is done in a
similar manner as the creation of a grayscale image, but with a twist.
Where the grayscale image is black, this image will be red.
Where the grayscale image is white, this image will also be white.
Where the grayscale image is gray, this image will range from red to white
(the middle values will be rather "pinky"). Look at the table below to
determine which color value need to change and which one need to remain
constant. The red value should be set to the maimum value, while the green
and blue values are modified to reflect the grayscale amount.
Note: This is NOT done
by simply setting the red color value to 255 and leaving the green and blue
color values alone. You must modify all three color values in every pixel.
- Display the modified image
- Save the modfied image to a file on the local machine
Some of the resulting colors will appear as:
makeBlackAndWhite()
| r = 0 g = 0 b = 0
| r = 63 g = 63 b = 63
| r = 127 g = 127 b = 127
| r = 191 g = 191 b = 191
| r = 255 g = 255 b = 255
|
makeBlackAndRed()
| r = 0 g = 0 b = 0
| r = 63 g = 0 b = 0
| r = 127 g = 0 b = 0
| r = 191 g = 0 b = 0
| r = 255 g = 0 b = 0
|
makeRedAndWhite()
| r = 255 g = 0 b = 0
| r = 255 g = 63 b = 63
| r = 255 g = 127 b = 127
| r = 255 g = 191 b = 191
| r = 255 g = 255 b = 255
|
Be sure to change comment with the name of the author to
contain the following:
- your name
- day and time of
your CS 101 lab section (i.e. Wednesday at 9:00)
- A description of the project.
- You are also to submit the Java file
electronically by
using the UNIX turnin command.
To use the UNIX turnin command to electronically hand-in your
java file using the project name of lab7 and to submit the files in
lab7a.java and lab7b.java for lab7, the turnin command is entered as:
turnin -c cs101 -p lab7 lab7a.java lab7b.java
To verify what you submitted using the turnin command type:
turnin -c cs101 -p lab7 -v
There is a Finger Exercise based on
this lab.
The following are some images created based on the ideas in this
lab. The following image is the original full color picture.
The following image is the black and white version of the original image.
The following image is the black and red version of the original image.
The following image is the red and white version of the original image.