CSE 131 Module 3: Iteration
- Update your eclipse workspace to get the
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:
- A source image
- A target image
Each method should read pixel values from the source image
and and write pixels to the target image
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
- 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.
- 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.)
- 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.)
- 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.)
- 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.
- 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)
- Complete the cover-page.txt file
in the repository for this lab. Just open it up in your eclipse editor
and type in your responses.
- In the cover-page.txt file, be sure to indicate anybody with whom
you have collaborated on the work you are submitting.
- You must commit all of your work to your repository. It's best to do this
from the top-most level of your repository, which bears your name and student ID.
- You must demo the commited work to a TA. Make sure the TA knows that
your demo is for credit at this point.
- The TA must have your name and information
on a lab demo sheet for this lab to be graded and
Be sure your name and ID are written
clearly on the TA's demo sheet.
Last modified 22:14:02 CST 11 November 2009
by Ron K. Cytron