Lab 6 - Image Processing using Java Methods and Arrays
Introduction
JPEG is an international file format used to save an image on a computer's disk. The fundamental component of an image is a pixel or picture element. Each pixel is assigned a color with three components red (R), green (G), and blue (B). Each of these components is assigned an integer value between 0 and 255. The combinations of red, green, and blue provide 2563 = 16,777,216 different colors.
1. Copy & Compile the files
- Copy the file caterpillar.jpg to your local directory. In addition, copy and compile each file in the following order:
Pixel.java, DigitalPicture.java, Picture.java. Finally, copy the file SimplePicture.class to your local directory.
Take a few minutes to look at the functionality of the Picture class. For example, a Picture can be initialized with the constructor
Picture myPicture = new Picture("caterpillar.jpg");
- Furthermore, a Picture object can be displayed via the toString() method as follows:
System.out.println(myPicture);
- Note: this will simply give a textual description of the Picture.
- The class hierarchy of classes is shown below

2. Test program
- Next create, compile, and run this java program to test these statements:
public class ImageProcessing{
public static void main(String[] args){
Picture myPicture = new Picture("caterpillar.jpg");
System.out.println(myPicture);
}
}
- Do you see the output:
- Picture, filename caterpillar.jpg height 150 width 329
- The numbers 150 and 329 are in pixel units (72 pixels = 1 inch).
3. Methods of Picture class
- Take a few minutes to look at the functionality of the DigitalPicture class. For example, DigitalPicture has the method
public void show(); // show the picture
which displays any DigitalPicture that show() is sent to.
myPicture.show(); //display the caterpillar
- Try it now by adding this line to the end of your program.
- Let's look at three more methods in DigitalPicture,
public String getTitle(); // get the title of the picture
public int getWidth(); // get the width of the picture in pixels
public int getHeight(); // get the height of the picture in pixels
- To use them let's define three variables as follows. Add these
lines to your program
String title = myPicture.getTitle();
int width = myPicture.getWidth();
int height = myPicture.getHeight();
- Now display each of these variables to see what these methods
return to the caller (you).
4. Pixel & Color
- In the diagram below, the XY-coordinate system is shown with
the origin located in the upper left corner of the image.

- Consider the method in the DigitalPicture class
public Pixel getPixel(int x, int y); //get the pixel information as an object
- which returns any Pixel in the image provided the (x,y) coordinates are provided. Let's get a handle on the Pixel at x=14 and y=10.
Pixel pixelOne = myPicture.getPixel(14,10); //its color is white
- Do the same for two more Pixels (40,19) and (185,80).
- For reference, a Pixel that appears white has red (R=255), blue (B=255) and green (G=255) components. On the other hand, a Pixel that appears in black has red (R=0), blue (B=0) and green (G=0) components.
- Take a look at the methods in the Pixel class. In particular, there are methods that give you the capability to get the red,
blue, and green components of any Pixel.
public int getRed() //gets the red component
public int getBlue() //gets the blue component
public int getGreen() //gets the green component
- Use these functions to compute the red, blue, and green components of these three pixels.
- You can also change the components of a Pixel using the following methods:
public void setColor(Color newColor)
- For example, to change the color of pixelOne to black,
pixelOne.setColor(new Color(0,0,0))
- Do you see the pixel that you changed (remember its coordinates are located at (14,10)?

5. Get Pixels
- The following is a very powerful method that places all pixels in a Picture into an array
Pixel[] getPixels() //stores all pixels into an array
- This statement moves all pixels in the caterpillar Picture to an array.
Pixel[] pixelArray = myPicture.getPixels();
- [Important: correspondence between a pixel's coordinates and its position in this array of pixels]
- The pixel at coordinates (x,y) in myPicture is found in the pixelArray at index y * p.getWidth() + x.
pixelArray[y * p.getWidth() + x]
6. Grayscale Project
- Grayscale is the term used to describe a monochromatic image. To convert a color image to grayscale each pixel must be processed by computing its intensity (the average of its red, green, and blue values) and changing its color so that [red=intensity, green=intensity, blue=intensity].
- Write a complete Java program to convert the caterpillar to grayscale. Hint: Place all pixels into an array. For each pixel, access its red, blue, and green components, average them and call the average (intensity), set all color components of this pixel to the intensity. After all pixels have been processed, display the image of the caterpillar.

7. Color Reduction Project
- Write a program to process the caterpillar image so that each of the color components of a pixel in the image is reduced in value by 50%. For example, if a pixel has color components [red = 201, green=76, blue=110] before processing this same pixel will have color components [red=100, green=38, blue=55]. Note that 201*0.5 = 100.5 but only integer values are allowed so (int)100.5 = 100.

|