For this lab you will be writing several methods to extract
color information from Picture
objects. To be able to compare multiple pictures at the same
time, you'll need to make multiple Picture
objects
of the same image file so you can view
them simultaneously.
public class Lab4 { public static void main(String[] args) { String fileName = "T:\\Harcourt\\Spring2008\\CS140\\gallery\\bird.jpg"; Picture originalPicture = new Picture(fileName); originalPicture.explore(); // write the rest of the lab code below this comment } }
P:\cs140
directory, compile it,
and run it. You should see a picture of a colorful bird
pop up on the screen.
originalPicture
). Call the explore
method too. There's an example of what I'm talking about below.
extractRedChannel
from the previous homework. (There are multiple solutions,
it's possible that yours looks different but is still correct.)
public void extractRedChannel() { Pixel[] pixelArray = this.getPixels(); for (Pixel pixelObj : pixelArray) { int value = pixelObj.getRed(); pixelObj.setColor(new Color(value, value, value)); } }
Picture.java
class and recompile now.
extractRedChannel
does
and how it works. If you need help, ask your
instructor before continuing.
Lab4
class that you created
above to make an additional picture and
call the extractRedChannel
method above. Add the following lines below the comment line:
Picture redPicture = new Picture(fileName); redPicture.extractRedChannel(); redPicture.explore();
Recompile, and run your Lab4
class. Two pictures should open, but they may be on top of
each other. After moving them around here's what I see on my
screen (one in color and one in black and white):
extractRedChannel
method has transformed the picture as expected by choosing several
pixels in the color image and comparing them with the pixels in the
black and white image. For
example, use the explorer to examine pixel (73,64). What
is the relationship? Is it what you expected?
Picture
class
nameed extractGreenChannel
and extractBlueChannel
that extract the green and blue channel, respectively. Use the
extractRedChannel
method as a guide (that is copy-and-paste :-).
Recompile your Picture class.
greenPicture
and
bluePicture
. This code goes in your
Lab4
class. Use them to test out your new methods.
Lab4
class you should see 4 pictures pop up on the screen. One in
color, and the other three in grayscale representing each of the RGB
channels.
Take some time to look at the differences between the three images.
Question 2: Look at the red areas in the original pictures, what do these areas look like in the red channel picture?
Question 3:
Similarly, what do the the green and blue channels look like as
compared to areas of high green and blue in the original color picture?
The previous color extractions worked by setting all the RGB components to the same value as whatever color we were interested in. For example, if we encountered a pixel with colors (r=38,b=129,g=92), we would reset all the colors in the pixel to be equal to 38 thus indicating the overall level of red in the image (r=38,g=38,b=38).
extractRedChannelRed
,
extractGreenChannelGreen
,
and extractBlueChannelBlue
.
These will extract the R, G, or B, channel and display the
resulting channel in that color. So rather than seeing a
grayscale image, you'll see the actual colors they're supposed to
represent. Here are the results I got once I created all
three methods (you may have to go to this document online to see the colors)
To get checked off for this lab, arrange all 7 images on your screen so that you know which one is which and show your instructor.
Notice that the R and G values were irrelevant in the yellow
calculation. Formally, you can think of this calculation as: