topical media & game development

talk show tell print

graphic-processing-algorithm-Ch05-p113b-p113b.pde / pde



  PImage myImage; //define an image object
  myImage = loadImage("memorial.jpg"); //load it
  size(myImage.width,myImage.height); //size it to fit the window
  image(myImage, 0,0); //display the image
  // convert the image to grayscale
  filter(GRAY);
  color white = color(255,255,255); //define white
  color black = color(0,0,0); //define black
  //extract edges
  for(int y=0; y<myImage.height-1; y++) //for all pixels in y
    for(int x=0; x<myImage.width-1; x++){ //for all pixels in x
      color thisPixel = get(x,y); //get a pixel’s color
      color nextPixel = get(x+1,y); //get the next pixel’s color
      int ra = int(red(thisPixel)); //extract the red value
      int rb = int(red(nextPixel)); //extract the red of the next
      if(abs(ra-rb)>6) //if they are different
        set(x,y,black); //set the pixel’s color black
      else
        set(x,y,white); //else to white
    }
  save("memorial_edge.jpg"); // save the image as a file


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.