CS 10: Winter 2016

Short Assignment 3
Due: Wednesday, January 13.

The Picture.java file from the java source folder contains a number of methods to transform images. This assignment asks you to add an additional method to that file that transforms an image by replacing each pixel color by a weighted sum of its color and the colors of its eight adjacent pixels. Image processing people call this a convolution, so your method should be called convolve. You may find it useful to write a private helper method that deals with a single pixel. You may call this method whatever you like (as long as the name is meaningful).

The method public Picture convolve(double [][] matrix, String title) takes a parameter that is a 3 x 3 matrix of weights and a parameter that is the title of the new picture that you produce. For each pixel that is not on the edge of the Picture you should produce a new pixel whose color is the weighted sum of its color and the colors of the surrounding pixels. (Pixels on the edge should not be changed.) The red, green, and blue values must be summed separately in order to get the red, green, and blue values for the new color. Finally, you should use the SimplePicture method setTitle to change the label on the picture that you produce so that you can tell which picture came from which matrix.

Note that the original Picture should not be changed. Follow the example of flip, blueScreen, and the other methods that return a Picture. Create a new Picture and fill in the correct pixel values in that.

The pixel above and to the left of the current pixel should be multiplied by matrix[0][0], the one directly above should be multiplied by matrix[0][1], the one above and to the right should be multiplied by matrix[0][2], and so on. Thus the pixel itself is multiplied by matrix[1][1] and the pixel down and to the right is multiplied by matrix[2][2]. These products should be added together to get the new value.

There are many useful methods in the Pixel class that you will want to use. In particular, the correctValue method will prove useful to make sure that the sums that you compute are in range.

Test your code on the following three test matrices:

double ninth = 1.0/9.0; double [][] blur = {{ninth, ninth, ninth}, {ninth, ninth, ninth}, {ninth, ninth, ninth}}; double [][] edges = {{-1.0, -1.0, -1.0}, {-1.0, 8.0, -1.0}, {-1.0, -1.0, -1.0}}; double [][] sharpen = {{-1.0, -1.0, -1.0}, {-1.0, 9.0, -1.0}, {-1.0, -1.0, -1.0}};

The names give some idea of what the matrices do. Thus blur should make the picture slightly blurred (soft focus), edges detects boundaries between different colors, and sharpen makes boundaries more distinct. I suggest changing the main method so that it displays the original picture and creates and displays the three modified pictures.

Setting up Eclipse to work with media files

If you want to use Eclipse instead of DrJava to solve this homework (or other homeworks that use the media files that you downloaded) you have two choices. The first is to create a new project and drag all of the files in java-source (except the "doc" folder) into it.

The other choice is to set classpaths, just as you did in DrJava. This explains how.

  1. Go into the Eclipse menu and select Preferences. A window should open with General as a choice. Click the triangle (or whatever) to expand the choices under General. One of them is Workspace. When you expand Workspace, one option is Linked Resources. Click on this. There should be a check box labeled "Enable linked resources". Check it if it is unchecked.
  2. Click on your your project in the Package Explorer window on the left side (mine is called cs10proj), go to the File menu, and select Properties. A window should open.
  3. Click on "Java Build Path" in the list of choices on the left side of the window.
  4. Click on "Libraries" in the choices along the top of the window.
  5. Click on "Add External Class Folder" and navigate to the folder java-source. Select it and click "Open". The folder java-source should now be added as a build path.

You can now add Picture.java to the project, edit it, and run it.

Turn in

Electronically turn in your modifed Picture.java file (with comments indicating where your modification is) on Canvas. Also electronically turn in screen shot(s) showing the beach.jpg image and the convolve method run on this image for each of the three matrices. (I was able to lay them out on the screen so that one screen shot showed all four.)

Extra Credit

Find other matricies that give interesting effects.