/** * Edge detection * * * @author Pat Troy: troy AT uic DOT edu */ import java.util.*; import java.awt.*; public class Lect1102e { public static void main(String[] args) { // get the foreground picture String fileName = FileChooser.pickAFile(); Picture pict = new Picture (fileName); // call a method to do the background/foreground separation Picture pictNew; pictNew = detectEdge (pict); pictNew.show(); } public static Picture detectEdge (Picture p) { // set up the "background color" Color background = new Color (12, 48, 68); Picture result = new Picture (p.getWidth(), p.getHeight()); Pixel pix, pix2, pixNew; int red, green, blue, grayAmount; Color c2; // set up two loops; one nested inside the other int xIndex, yIndex; for ( xIndex = 0 ; xIndex < p.getWidth()-1 ; xIndex++ ) { for ( yIndex = 0 ; yIndex < p.getHeight()-1 ; yIndex++ ) { // Access the pixel from the orignal image pix = p.getPixel (xIndex, yIndex); pix2 = p.getPixel (xIndex + 1, yIndex); c2 = pix2.getColor(); // check to see if the pixel's color is not close to the background color if ( pix.colorDistance (c2) > 45.0 ) { //Access the pixl from the new background pixNew = result.getPixel (xIndex, yIndex); // set the 3 color values in the new pixel pixNew.setRed (0); pixNew.setGreen (0); pixNew.setBlue (0); } pix2 = p.getPixel (xIndex, yIndex+1); c2 = pix2.getColor(); // check to see if the pixel's color is not close to the background color if ( pix.colorDistance (c2) > 45.0 ) { //Access the pixl from the new background pixNew = result.getPixel (xIndex, yIndex); // set the 3 color values in the new pixel pixNew.setRed (0); pixNew.setGreen (0); pixNew.setBlue (0); } } } return ( result ); } }