CSE 131 Module 3: Iteration

Lab


Workspace Update

  1. Update your eclipse workspace to get the lab3 package.
    Update by right-clicking (control-click on Mac) the main project in the package explorer, drag down to Team... and click Update.

    You should see a lab3 package in the src directory.


Project: Manipulating an Image Raster Using Iteration

Directions: In the provided file RasterTool.java, implement the following methods. Your methods will use iteration (either while loops or for loops) to operate on the pixels of an image raster. Unless otherwise noted, all of the methods take two parameters:

Each method should read pixel values from the source image and and write pixels to the target image using setPixel(int, int, Color). (In general, these methods will end up writing all of the pixels of the target image.) Before writing these methods, read the example flipHoriz method provided in your repository, and makes sure you understand how it works.

Note: To get the color of a pixel in an image, use image.getPixelColor(x,y)
  1. The provided method flipHoriz flips the image horizontally. (That is, after the method runs, the target image should be the left-to-right mirror image of the source image.) Study this example carefully, as the methods you write will interact with the NIP object similarly.

    Also note how the getEventNames() method provides the names displayed under NIP's Methods menu, and how the actionNameCalled(String) method calls the flipHoriz method when "Flip Horizontally" is selected from the menu, passing it the source and target images. You will need to augment actionNameCalled as you write additional methods in order for them to be triggered properly.

  2. Write a method that flips the image vertically. (That is, after your method runs, the second image should be the top-to-bottom mirror image of the first image.)

  3. Write a method that flips the left half of the image horizontally. (That is, after your method runs, the left half of the second image should be same as the first, but the right half of the second image should be the mirror of the left half.)

  4. Write a method that flips the bottom half of the image vertically. (That is, after your method runs, the bottom half of the second image should be same as the first, but the top half of the second image should be the mirror of the bottom half.)

  5. This method takes a single image as a parameter. It should create a color gradient in the image by replacing every color, where the amount of red in each pixel increases gradually from 0 at the left edge of the image, to 255 at the right edge of the image. Similarly, the amount of green in each pixel increases gradually from 0 at the top edge of the image, to 255 at the bottom edge of the image. Finally, the amount of blue in every pixel should be 128. So, each pixel will have a different color depending on its position. For example, the pixel at the top left will have red=0, green=0, and blue=128. The pixel about 1/4 of the way down on the right edge will have red=255, green=64, and blue=128. Start by writing expressions for the amount of red and green in each pixel, given the x and y position of that pixel and the width and height of the image.

  6. An edge in an image is a place where neighboring pixels are of a significantly different color. Write a method that finds edges in an image using a simple algorithm that checks the neighboring pixels. For each pixel, consider its color in comparison to the four pixels above, below, left, and right. If any of those four are significantly different from the pixel you are considering, then color the corresponding pixel in the second image black. Otherwise, color the pixel in the second image white. That way, the resulting image will have outlines for the edges. Do not process the outermost pixels of the image, since you won't have pixels to compare on all sides.

    As a starting point, consider pixels to be significantly different if they differ in each of the color components by at least 50, but feel free to fiddle with this criterion to get better edge detection.

    It is strongly recommended that you break up your implementation into two or more methods. For example, one method might be responsible for deciding if a given pixel is an edge pixel (by comparing it to its neighbors).

    Do not expect the output to be exact, but you should see the general shape of the building and the people, etc. There will be a lot of stray pixels, though. There are much more sophisticated algorithms that do a better job of detecting edges. You can try to improve on this basic algorithm if you want.


Submitting your work (read carefully)



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