topical media & game development

talk show tell print

graphic-processing-algorithm-Appendix-p319-5-p319-5.pde / pde



  int [] xd = {
    0,1,1, 1, 0,-1,-1,-1,0};
  int [] yd = {
    1,1,0,-1,-1,-1, 0, 1,1};
  PImage MyImage;
  int [][] MyCopy;
  void setup(){
    MyImage = loadImage("stockholm white.jpg");
    size(MyImage.width,MyImage.height);
    MyCopy = new int[width][height];
    image(MyImage, 0,0);
    filter(THRESHOLD);
    for(int x=0; x<width; x++)
      for(int y=0; y<height; y++)
        MyCopy[x][y] = getBinary(x,y);
    for(int g=0; g<7; g++) skeletonize();  //do more times
  }
  void skeletonize(){
    for(int x=1; x<width-2; x++)
      for(int y=2; y<height-1; y++){
        int b=0;
        int a=0;
        for(int i=0; i<8; i++){
          if(getBinary(x+xd[i],y+yd[i])==1)b++;
          if(getBinary(x+xd[i],y+yd[i])==0 &&
            getBinary(x+xd[i+1],y+yd[i+1])==1) a++;
        }
        int a2=0;
        for(int i=0; i<8; i++)
          if(getBinary(x+xd[i],y+1+yd[i])==0 &&
            getBinary(x+xd[i+1],y+1+yd[i+1])==1) a2++;
        int c2 = getBinary(x,y+1)*getBinary(x+1,y)*getBinary(x-1,y);
        int a3=0;
        for(int i=0; i<8; i++)
          if(getBinary(x+1+xd[i],y+yd[i])==0 &&
            getBinary(x+1+xd[i+1],y+yd[i+1])==1) a3++;
        int c3=getBinary(x,y+1)*getBinary(x+1,y)
          *getBinary(x,y-1);
        if((2<=b && b<=6) && a==1 &&
          (c2==0 || a2!=1) && (c3==0 || a3!=1))
          if(getBinary(x,y)==1)MyCopy[x][y]=0;
      }
    for(int x=1; x<width-1; x++)
      for(int y=1; y<height-1; y++)
        if(MyCopy[x][y]==1)
          set(x,y,color(0,0,0)); //black
        else
          set(x,y,color(255,255,255)); //white
  }
  int getBinary(int x,int y){
    return((brightness(get(x,y))>128) ? 0 : 1);
  }
  


(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.