Objectives:
Now
that we know about conditionals we can make the crop method more robust.
| Assignment: Modify the method the crop method from lab3 so that the cropped section will not extend beyond the bounds of the picture. If the rectangular region is too large, simply alter the bounds to the edge of the picture. |
We want a method which detects edges in the picture image by looking for areas of high contrast. We check for high contrast if the average of the colors values of a pixel differ by some amount from the average of the color values of a neighboring pixel.
Consider this simple edge detection code:
/* Method to do simple edge
detection by comparing the absolute value
* of the difference between the color intensities (average color values)
* between a pixel and the pixel below it.
* Edges will be drawn in black, everything else in white
* @param amount is the threshold for which a pixel marked as an edge
*/
public void edgeDetection(double amount){
Pixel topPixel = null;
Pixel bottomPixel = null;
double topAverage = 0.0;
double bottomAverage = 0.0;
/* loop through the y values from 0 to height - 1
* (since compare to below pixel)
*/
for (int y= 0; y < this.getHeight()-1; y++){
// loop through the x values from 0
to width
for (int x = 0; x<this.getWidth();
x++){
// get the
top and bottom pixels
topPixel =
this.getPixel(x,y);
bottomPixel =
this.getPixel(x,y+1);
// get the
color averages for the two pixels
topAverage =
topPixel.getAverage();
bottomAverage
= bottomPixel.getAverage();
/* check if
the absolute value of the difference
* is less
than the amount
*/
if (Math.abs(topAverage
- bottomAverage) < amount)
topPixel.setColor(Color.white);
else
topPixel.setColor(Color.black);
}
}
}
Add this method to the Picture class and test if out.
| Assignment: Since the simple edge detection is just checking a pixel with the one below, it is detecting horizontal edges but not detecting vertical edges. Change the edgeDetection method so it writes a black pixel for BOTH vertical and horizontal edges. How would you check that this is working correctly? |
| Assignment: Write a method public Picture authenticate(Picture p) which checks to see if "this" picture and the picture p are identical. It returns a new picture that it the same size as "this" picture. For each pixel position in the original picture "this", compare the color of that pixel with the color of the pixel in the same position from second picture. If the two pixels have the same color (i.e. the original is not modified in the copy), set the color of this position in the resulting picture to white. If the two pixels do not have the same color, set the color of this position in the resulting picture to the color of the pixel from the original picture. Return the resulting picture. If the two pictures are not the same size, the resulting picture should contain all black pixels. You can test your method on the pictures barbara_jpeg_wm and barbara_jpeq_manip as well as houses_jpeq_wm and houses_jpeq_manip. |