SA-2, due Sep 25

The Picture.java file from the software accompanying the PSDS text 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(float [][] matrix) takes a single parameter that is a 3 x 3 matrix of weights. 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.

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.

You are allowed to change the Pixel method correctValue from private to public if you find that useful. (In the latest version of the file java-source.zip it is Public, so you need not change it.)

Test your code on the following three test matrices:

float ninth = 1.0f/9.0f;
    float [][] blur = {{ninth, ninth, ninth}, 
                       {ninth, ninth, ninth}, 
                       {ninth, ninth, ninth}};
    p.convolve(blur).explore();
    
    
    float [][] edges = {{-1.0f, -1.0f, -1.0f},
                          {-1.0f, 8.0f, -1.0f},
                          {-1.0f, -1.0f, -1.0f}};
    p.convolve(edges).explore();
    
    float [][] sharpen = {{-1.0f, -1.0f, -1.0f},
                          {-1.0f, 9.0f, -1.0f},
                          {-1.0f, -1.0f, -1.0f}};
    p.convolve(sharpen).explore();

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.

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 need 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. Whey 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. Click on "Add External Class Folder" again and add the inst folder to the class paths.
  6. Click on "Add External Jars" and select the jar file jMusic1.6.3.jar and the three jar files within java-source: javazoom.jar, jmf.jar, and sound.jar.

You can now add Picture.java to the project, edit it, and run it. (Running it will require modifying the main method to test your new method.)

Turn in

Electronically turn in your modifed Picture.java file (with comments indicating where your modification is) on Blackboard. 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.