Objectives:
| Assignment: Write a method colorReplace(int startX, int startY, int endX, int endY, Color startColor, Color newColor, int threshold) which replaces all values near to the startColor within the specified rectangle to the newColor. The threshold defines how "near" we have to be to the startColor to change it. Try this out by writing a program that starts with a picture of a person and changes their skin to green and hair to orange. (or whatever colors you would prefer). |
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 if there is high contrast with either the pixel below or with the pixel to the right. |