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- (control-) click on that.
    • Go down to Team
    • Choose Update
    • Authenticate with your WUSTL key as usual
You should see some new files: some image files in the project directory and some python 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.py. Double click on that file to open it in the editor. This is the file in which you will write the image processing methods for this lab. Add your your name to the author tag 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 ProcessorTool.py in the Package Explorer, and select Run as...Python run from the popup menu. Within a few seconds, you should see a window with three panels. It may be hidden behind some other windows.

    The leftmost panel should contain a photograph of Brookings Hall and Chancellor Wrighton should appear in the other two panels, respectively.

  4. On the right side of the window you should see a number of operations that can be performed on these images. Try the Example: ones to see how they behave.
    Note that you can use the radio buttons beneath the three panels to select which image(s) the methods manipulate (sources) and where the output is placed (target).
    The other methods won't do anything interesting... yet.
  5. 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. Writing methods that operate on primitive values: Recall that functions in python can return values based on their input parameters. For example, in studio, you wrote following method that computes the sum of its three input parameters:
    def sum(a, b, c):
       return a + b + c
    
    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 write methods whose parameter(s) and return values are such integers, each representing a color component of a pixel. When you execute these methods, the ProcessorTool will apply them to every component of every pixel of the image to produce a new image.

    In the file ImageProcessor.py, complete the provided stub methods as described below. In the method bodies, use mathematical expressions. 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.)
      Take some time to think about the mathematical function that has this property. Try this on paper before trying this in code!

    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. The result is that 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.

      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
      • 125 / 128 = 0

  2. Writing methods that use objects:
    Each pixel of an image is represented as a pyNIPColor instance. The functions you write for this part will accept and return such instances.

    There are two things you need to know to use pyNIPColor:

    • To construct a pyNIPColor object, you write
      pyNIPColor(r,g,b)
      
      where r, g, and b are values for the red, green, and blue components, respectively. For example
      pyNIPColor(0,0,0) is the color black
      pyNIPColor(255,255,255) is the color white
    • If you have a pyNIPColor instance c, then you can access its components by
      • c.getRed()
      • c.getGreen()
      • c.getBlue()

    1. Complete the method grayscale that will make a grayscale image from a color image. To do this, your function takes in one parameter (the color object for a pixel from the original image) and will retun produce a pyNIPColor in which all the components (red, green, and blue) have the same value. Hint: Consider the average the three components of the original color, a problem you solved in studio.

    2. Complete the method brighter that will return a color that is brighter than its input.
      For now try something simple, and revisit this problem once you know more python to try a more sophisticated model for brightness.

  3. 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. 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, a computation you considered in studio. 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.

  4. Extension: adding your own filter
    1. Think of a method you would like to add to this lab. Your new method might take as its input(s):
      • a pixel component, as with negative
      • a pyNIPColor instance, as with brighter
      • two pyNIPColor instances, as with combineBrighter
    2. Although you are not expected to understand yet what the code does, open ProcessorTool and add a menu item for your new function in the commandNames list.
      • Be brave: pattern what you do after the code you see to add your new menu choice.
      • Run ProcessorTool and make sure you see your new item. Click on it, but nothing will happen because we have not yet put in the code to act on the choice.
    3. In the doCommand method, you see a nested if-elif block. Based on the simple pattern you see in that code, insert code that would call a method based on choosing your new menu item.

      Try running ProcessorTool again, and try your new menu choice. What happens?

    4. Depending on the style of method you have designed
      Does it take a pixel component, one pyNIPColor instance, or two instances?
      copy and paste code that already handles a similar scenario, and arrange for that code to call your corresponding method in ImageProcessor.
    5. Finally, implement and test your new filter in ImageProcessor.
    6. Be prepared to present your idea and implementation to the class.

Submitting your work (read carefully)



Last modified 08:29:28 CDT 18 May 2012
When you done with this lab, you must be cleared by the TA to receive credit.

If you worked in a team using a group– repository:
  • Your work must be committed in that repository for this demo.
  • Each team member must complete this form and have the TA authenticate.
    For example, if there are two of you, then the form must be submitted twice, so that each person's last name and 6 digit ID is captured with the repository name.
  • The repository name must be included on each of the submissions.

group– Enter the name of your group repository here
Last name 6-digit ID
1

TA: Password: