1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
// separable 2-d median filter
PImage img;
PImage pass1; // horizontal pass
PImage pass2; // vertical pass
int dim =5; // dimension of filter - needs to be an odd number
void setup() {
size(1024, 768); // this is all hard coded because the idea is the filter not interface
img = loadImage("c:/Lighthouse.jpg"); // windows 7 stock image moved to c:\ - use the unix form c:/
pass1= createImage(img.width, img.height, RGB);
pass2= createImage(img.width, img.height, RGB);
}
void draw() {
float []red = new float[dim];
float []green = new float[dim];
float []blue = new float[dim];
float temp,mred,mgreen,mblue;
loadPixels();
// Since we are going to access the image's pixels too
img.loadPixels();
for (int y = dim/2; y < height-dim/2; y++) { //walk through all the y rows
for (int x = dim/2; x < width-dim/2; x++) { //walk through the x columns
int loc = x + y*width;
int ip = 0; //index of pixel array where values are stored
for(int p = -(dim/2);p < dim/2;p++){ // this is the offset where pixels before and afer the x,y pixel are gathered
int locp = p+ x + y*width;
red[ip] = red(img.pixels[locp]); //put the red, green and blue compnents into their proper array
green[ip] = green(img.pixels[locp]);
blue[ip] = blue(img.pixels[locp]);
ip++;
}
for(int i = 1; i <dim; i++){ // these for loops sort the red , blue and green
// array in order by value
temp = red[i];
int j = i-1;
while(j >=0 && red[j]>temp){
red[j+1]=red[j];
j = j-1;
}
red[j+1]= temp;
}
mred = red[dim/2]; // once sorted, the middle index should hold the median value
for(int i = 1; i < dim; i++){ // do this for green and blue
temp = green[i];
int j = i-1;
while(j >=0 && green[j]>temp){
green[j+1]=green[j];
j = j-1;
}
green[j+1]= temp;
}
mgreen = green[dim/2];
for(int i = 1; i <dim; i++){
temp = blue[i];
int j = i-1;
while(j >=0 && blue[j]>temp){
blue[j+1]=blue[j];
j = j-1;
}
blue[j+1]= temp;
}
mblue = blue[dim/2];
pass1.pixels[loc] = color(mred,mgreen,mblue); //finally, paste the pixel value into our image that will hold
// the first pass
}
}
for (int y = dim/2; y < height- dim/2; y++) { // now do the same thing for the verticle direction
for (int x = dim/2; x < width-dim/2; x++) {
int loc = x + y*width;
int ip = 0;
for(int p = -2;p < dim/2;p++){
int locp = x + (y+p)*width; // this is the only different line, rest is cut and paste.
red[ip] = red(pass1.pixels[locp]);
green[ip] = green(pass1.pixels[locp]);
blue[ip] = blue(pass1.pixels[locp]);
ip++;
}
for(int i = 1; i <dim; i++){
temp = red[i];
int j = i-1;
while(j >=0 && red[j]>temp){
red[j+1]=red[j];
j = j-1;
}
red[j+1]= temp;
}
mred = red[dim/2];
for(int i = 1; i <dim; i++){
temp = green[i];
int j = i-1;
while(j >=0 && green[j]>temp){
green[j+1]=green[j];
j = j-1;
}
green[j+1]= temp;
}
mgreen = green[dim/2];
for(int i = 1; i <dim; i++){
temp = blue[i];
int j = i-1;
while(j >=0 && blue[j]>temp){
blue[j+1]=blue[j];
j = j-1;
}
blue[j+1]= temp;
}
mblue = blue[dim/2];
pass2.pixels[loc] = color(mred,mgreen,mblue); // I lied, this is different also
}
}
pass2.updatePixels();
image(pass2,0,0); // display the separable 2d median filter
// need more work as it leaves a border = to the filter size around image.
}
|