CSE 131 Module 1: Methods

Lab


SVN Update

The files you need for this lab have been imported into your svn repository, but you need to take the following steps to see them in your eclipse workspace:
  1. Bring up eclipse on the machine you wish to use (lab machine in CEC, your laptop, etc.)
  2. If you do not have the project loaded into your eclipse workspace, then follow the steps in Part I of lab 0 to do so.
  3. If you already have the project loaded into the Java view, then all you have to do is:
    • Find the project name in the Package Explorer window. It should be named by your last name, a hyphen, and your student ID number.
    • Right click on that.
    • Go down to Team
    • Choose Update
You should see some new files: some image files in the project directory and some Java files in the src/lab1 directory.


Project: Image Processing Methods

  1. In Eclipse, open the lab1 package in the Package Explorer. You should see a file called "ImageProcessor.java". Double click on that file to open it in the Java editor. This is the file in which you will write the image processing methods for this lab. Fill in your name and the date at the top of the file.
  2. Notice that there are a few sample methods in the file that have been completed for you.
  3. To run the program the first time, right click on "ImageProcessor.java" in the Package Explorer, and select Run -> Java application from the popup menu. You should see a window with three panels. The first two panels should contain photographs of Brookings Hall and Chancellor Wrighton, respectively.
  4. If you open the "methods" menu in NIP, you will see the names of eleven image operations. The method names prefaced with "Example" have been completed for you. Try them to see how they behave. The other methods won't do anything interesting... yet.
  5. Note that you can use the radio buttons underneath the three panels to select which image(s) the methods manipulate and where the output is placed.
  6. To run the program again later, just click the "run" icon near the upper left corner of the eclipse window. (It's a white triangle in a green circle.)
Directions:  In this lab, you will complete methods that are used to help perform image processing functions. For each method, you will be given a specification of what the method should do. You will implement the method so that it satisfies the specification. We have provided a skeleton application program called NIP, as well as a tool for NIP ("ProcessorTool.java") which controls how your methods are used to manipulate images. After you write each method, you will run NIP to test it on actual images.
  1. Read the introductory portions of the NIP documentation before beginning. While you do not need to completely know how NIP works at this point, a basic understanding may be useful. You may also wish to examine the ProcessorTool.java file to see how your methods will be used.

  2. Writing methods that operate on primitive values: Recall that every method in Java has a return type and may accept parameters of various types. For example, the following method takes a two double parameters and returns their sum.
    double sum(double a, double b) {
       return a + b;
    }
    
    Recall that each pixel of an image has three color components (red, green, and blue), and that each of these components is an int value in the range 0 to 255, where 0 is no intensity and 255 is full intensity. In this part of the lab, you will write methods whose parameter(s) and return values are ints, representing the three color components of a pixel. When you execute these methods in NIP, the ProcessorTool will apply them to every component of every pixel of the image to produce a new image. Within NIP, use the browse button to load in image files.

    In the file ImageProcessor.java, complete the provided stub methods as described below. In the method bodies, use mathematical expression. Do not use a conditional (if) statement.

    1. Complete the method called copy that copies the first source image to the target panel. (Hint: This is a very simple method.)

    2. Complete the method that will composite the images in the two source panels by averaging their components. You will take two parameters, which will be the color components from the corresponding pixels in the two images being composited.

    3. Complete the method called negative that will produce the negative image by inverting the intensity of each component. (For example, if the parameter value is 0, you should return 255. If the parameter value is 1, you should return 254, and so on.)

    4. Complete the method posterize that will reduce the number of possible colors in an image. For each color component, you will choose between two intensities, 0 or 255, which corresponding to that color component being turned off or on completely. So, since each color has three components (red, green, and blue), you will end up with an image that has only 8 different colors. Remember that you are not allowed to use conditionals (if) statements for this part of the lab. (Hints: Recall that color components are in the range 0-255. Also, recall that if you divide an int by another int, the result number will be truncated. For example, 130 / 128 = 1, but 125 / 128 = 0.)

  3. Writing methods that use objects:

    Note that each pixel of an image is represented as a Color object. Create and test methods (whose parameters and return values are Colors) with the following specifications.

    1. Complete the method brighter that will return, for each pixel, a Color that is brighter than the original. (The Color class makes this easy because it already provides a method brighter() that returns a brighter color.)

    2. Complete the method grayscale that will make a grayscale image from a color image. To do this, you will take in one Color parameter (the Color object for a pixel from the original image) and will produce a new Color in which all the components (red, green, and blue) have the same value. Hint: To choose which value, average the three components of the original color.

  4. Writing methods that use conditional statements: In this last group of methods, you will use conditional statements to make decisions within the method bodies.

    1. Complete the method blackAndWhite that produces a black and white image by returning, for each pixel, a Color that is either black or white, depending upon the overall intensity of the original color. For your return value, use the constant Color.BLACK or Color.WHITE. It's up to you how to decide when a color's components, taken as a whole, should be considered black or white.

    2. Complete the method combineBrighter that combines two images by choosing for each pixel the color from the image that has the brighter pixel in that location. To determine which pixel is brighter, compare the sums of the red, green, and blue components of the two pixels. Since the ProcessorTool will run your method for every pair of pixels, the resulting image will have some pixels from the first image and some from the second image.
To earn the grade of your choosing, you need to complete certain number of extensions. Using the navigation bar at the top of this page, you can take a look at the extensions you might undertake for this particular module.

Submitting your work (read carefully)



Last modified 22:14:03 CST 11 November 2009 by Ron K. Cytron