Sep 20: Finish Classes and Objects. Image Processing


Finished up the notes from last time, then did:

How is an image represented?

A digital image is represented as a 2-dimensional grid of pixels, or picture elements. (Exactly how it is represented is hidden inside the class BufferedImage.) Draw a rectangle, label upper left corner (0,0), talk about pixel at (x, y), where x is position in row, y is position in column. Note that y gets bigger as it goes down the image.

So what is a pixel? It is basically a representation of the color that should be drawn on the screen at that point. This color is represented in RGB, short for Red-Green-Blue. If you ask a BufferedImage for the color at a given pixel you get back an int. This value is not really treated as an int, though. Its 32 bits are broken into 4 8-bit chunks. Each chunk represents a number between 0 and 255 in binary.

The leftmost 8 bits are the alpha value. This represents the amount of transparency, with 0 as totally transparent and 255 as totally opaque. The next 8 bits represent the amount of red (0 no red to 255 maximum red), the next 8 bits represent the amount of green, and the rightmost 8 bits represent the amount of blue. We won't be dealing with the alpha value much - all of the images we will be playing with have alpha = 255. (You can play around with it, though.)

Java has built-in graphics and image-handling classes and methods. We will see some soon. However, Barb Ericson at Georgia Tech has created a class Picture that is designed to make image processing relatively easy. Most of the work is done in a class SimplePicture, and Picture extends SimplePicture. We will look at extending classes a lot more next class. For now, it means that any method defined in SimplePicture can be called on a Picture object and it will work correctly. She did this so that you can modify the Picture class without having to worry about how all the details are handled in the SimplePicture class.

The Picture class

Let's look at the JavaDoc for the Picture class and play with some of the functions. We will first figure out what they are doing, and then try to decide how they must work. Eventually we will see how the Java code does this.

We have a choice of 5 different constructors. Overloaded! Look at what each does. Experiment with the no-parameter constructor and the constructor with 2 int parameters to see what they produce.

pickAndShow gives and easy way to get a picture from an image file. Demo it. showNamed does the same, but from the file name. copy is fairly obvious, although the reason for it may not be. What advantage is there to copying rather than p2 = p? (Changing the copy won't change the original.)

So what does increaseRed do? Demo. Looks like it ups the red value. (Actually doubles it.) Maxes out at 255, though. Do enough times every pixel is saturated with red. Why not all entirely red? Have green and blue components. If all are 255, get white! Play with decreaseRed also. Looks like we need to go into the pixel repesentation and change the red value.

It would be a pain to go in and modify the int representing each color. Would have to pull out the correct 8 bits, treat them as a number, double it, and then put the new value for 8 bits back into the color representation. Not hard, but messy. Fortuately the Pixel class will simplify this for us. Go through the JavaDoc for this class. Note getRed, setRed, other useful functions.

What does negate do? Makes it look like a photographic negative (for those of you who have seen photographic film). Click on pixels using explore(), note that the amount of color in a pixel in the original and the amount of the same color in the negative seem to add to 255. So subtract from 255 to get new color.

flip is fairly straightforward. How would you compute it? Reverse the order of the pixels in each row. Note the flip returns a Picture, so we can say p.flip().explore();. Work left to right. p is a Picture. Call flip on it, get another Picture. Call explore on that picture.

Look at more complex things. compose replaces part of a picture by another picture. Looks like it copies pixels from the first, saves them in the second.

A similar idea appears in blueScreen. This is the method used by TV meterologists to make it appear that they are standing in front of a big weather display when in fact they are standing in front of a blue background. Demo using blue-katie-standing.jpg. How does it work? Like compose, but only copy non-blue pixels (those with less blue than the sum of red and green).

Look at the others yourself. The one that changes a picture to black and white (called gray scale) finds a weighted average of the three colors and creates a pixel with all three having that average. (Weighted average because gray scale only shows brightness, and the eye perceives the brightness of the colors differently.) Oil paint method replaces each pixel by the most common color in a rectangle around it, which makes for blotches of color.