topical media & game development
graphic-processing-algorithm-Ch05-p121-p121.pde / pde
int [] xd = {
0,1,1,1,0,-1,-1,-1,0}; //neighbors’ x index cw (top)
int [] yd = {
1,1,0,-1,-1,-1,0,1,1}; //neighbors’ y index cw (top)
PImage MyImage = loadImage("stockholm.jpg"); //load an image
size(MyImage.width,MyImage.height); //size to match the image
image(MyImage, 0,0); //display the image
int [][] MyCopy = new int[width][height]; // array equal to image
for(int x=1; x<width-1; x++) //for all pixels (except border)
for(int y=1; y<height-1; y++){
int b=0;
int a=0;
for(int i=0; i<8; i++){
if(brightness(get(x+xd[i],y+yd[i]))<128) //case 1
b++;
if(brightness(get(x+xd[i],y+yd[i]))<128 &&
brightness(get(x+xd[i+1],y+yd[i+1]))>128) //case 2
a++;
}
if((b>=2 && b<=6) || a==1 )
MyCopy[x][y]=1; //mark these ones as edges
else
MyCopy[x][y]=0;
}
for(int x=1; x<width-1; x++) //go through all pixels
for(int y=1; y<height-1; y++){
if(MyCopy[x][y]==1) //if they are marked
set(x,y,color(0,0,0)); //paint them black
else
set(x,y,color(255,255,255)); //else white
}
save("MyImage.jpg"); //save just incase
(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.