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 Lect1006g.java.
Lab Assignment 7
Due: Wednesday 2/29/2012 by 11:59 pm
Write two Java programs that will do the following:
-
Program 1: Make Black And Green
- The Java program is to be named: NetidLab7a
- 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 "black-to-green scale" image. This is done in a similar manner
as the creation of a grayscale image, except the values for red and blue are
kept at zero. Only the green value for each pixel will range from 0 to 255.
Where the grayscale image is black, the green scale image will also be black.
Where the grayscale image is white, the green scale image will be green.
Where the grayscale image is gray, this image will range from black to green
(the middle values will be a shade of dark green).
Note: This is NOT done
by simply setting the red and blue color values to 0 and leaving the green
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 Green And White
- The Java program is to be named: NetidLab7b
- 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 green 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 green.
Where the grayscale image is white, this image will also be white.
Where the grayscale image is gray, this image will range from green to white
(the middle values will be light green). Look at the table below to
determine which color value need to change and which one need to remain
constant. The green value should be set to the maximum value, while the red
and blue values are modified to reflect the grayscale amount.
Note: This is NOT done
by simply setting the green color value to 255 and leaving the red 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
|
makeBlackAndGreen()
| r = 0 g = 0 b = 0
| r = 0 g = 63 b = 0
| r = 0 g = 127 b = 0
| r = 0 g = 191 b = 0
| r = 0 g = 255 b = 0
|
makeGreenAndWhite()
| r = 0 g = 255 b = 0
| r = 63 g = 255 b = 63
| r = 127 g = 255 b = 127
| r = 191 g = 255 b = 191
| r = 255 g = 255 b = 255
|
-
You must write your programs using good programming style which
includes:
- Good variable names
- in-line commenting
- header block commenting for the program and each method written
Be sure to include the following with the header block comment for the
program.
- your name
- day and time of
your CS 101 lab section (i.e. Friday at 9:00)
- A description of the project.
- proper indentation of program statements
- use of blank lines to separate blocks of code.
-
You are to submit both Java files
electronically via the
assignment link in Blackboard for Lab 7.
Please only submit source code
files (the .java file, not the .class
or the .java~).
Also, if you have any comments about your program, please write it
down in the header block comment of the .java file;
please do NOT write in the "Comments" field on
the submission page (this will go into a different file and will be
easily missed).
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 green version of the original image.
The following image is the green and white version of the original image.