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:
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.
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.
cs10proj
), go to the File menu, and select Properties.
A window should open.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.
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.)
Find other matricies that give interesting effects.