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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| /**
*
*/
package millie.operator;
import millie.image.Image;
import millie.operator.commons.ImageOperator;
/**
*
* @author Xavier Philippeau
*
*/
public class SkeletonOperator implements ImageOperator {
// Gray Level Threshold
private int threshold = 0;
// Smoothing pattern
private byte[] pattern1={-1,1,0,1,0,0,0,0};
private byte[] pattern2={0,1,0,1,-1,0,0,0};
private byte[] pattern3={0,0,-1,1,0,1,0,0};
private byte[] pattern4={0,0,0,1,0,1,-1,0};
private byte[] pattern5={0,0,0,0,-1,1,0,1};
private byte[] pattern6={-1,0,0,0,0,1,0,1};
private byte[] pattern7={0,1,0,0,0,0,-1,1};
private byte[] pattern8={0,1,-1,0,0,0,0,1};
public SkeletonOperator(int threshold) {
this.threshold=threshold;
}
// Neighbourhood
private int neighbourhood(byte[][] c,int x,int y) {
int neighbourhood=0;
if (c[x-1][y-1]==1) neighbourhood++;
if (c[x-1][y ]==1) neighbourhood++;
if (c[x-1][y+1]==1) neighbourhood++;
if (c[x ][y+1]==1) neighbourhood++;
if (c[x+1][y+1]==1) neighbourhood++;
if (c[x+1][y ]==1) neighbourhood++;
if (c[x+1][y-1]==1) neighbourhood++;
if (c[x ][y-1]==1) neighbourhood++;
return neighbourhood;
}
// Transitions Count
private int transitions(byte[][] c,int x,int y) {
int transitions=0;
if (c[x-1][y-1]==0 && c[x-1][y ]==1) transitions++;
if (c[x-1][y ]==0 && c[x-1][y+1]==1) transitions++;
if (c[x-1][y+1]==0 && c[x ][y+1]==1) transitions++;
if (c[x ][y+1]==0 && c[x+1][y+1]==1) transitions++;
if (c[x+1][y+1]==0 && c[x+1][y ]==1) transitions++;
if (c[x+1][y ]==0 && c[x+1][y-1]==1) transitions++;
if (c[x+1][y-1]==0 && c[x ][y-1]==1) transitions++;
if (c[x ][y-1]==0 && c[x-1][y-1]==1) transitions++;
return transitions;
}
// Match a pattern
private boolean matchPattern(byte[][] c,int x,int y,byte[] pattern) {
if (pattern[0]!=-1 && pattern[0]!=c[x-1][y-1]) return false;
if (pattern[1]!=-1 && pattern[1]!=c[x-1][y ]) return false;
if (pattern[2]!=-1 && pattern[2]!=c[x-1][y+1]) return false;
if (pattern[3]!=-1 && pattern[3]!=c[x ][y+1]) return false;
if (pattern[4]!=-1 && pattern[4]!=c[x+1][y+1]) return false;
if (pattern[5]!=-1 && pattern[5]!=c[x+1][y ]) return false;
if (pattern[6]!=-1 && pattern[6]!=c[x+1][y-1]) return false;
if (pattern[7]!=-1 && pattern[7]!=c[x ][y-1]) return false;
return true;
}
// Match one of the 8 patterns
private boolean matchOneOfPatterns(byte[][] c,int x,int y) {
if (matchPattern(c,x,y,pattern1)) return true;
if (matchPattern(c,x,y,pattern2)) return true;
if (matchPattern(c,x,y,pattern3)) return true;
if (matchPattern(c,x,y,pattern4)) return true;
if (matchPattern(c,x,y,pattern5)) return true;
if (matchPattern(c,x,y,pattern6)) return true;
if (matchPattern(c,x,y,pattern7)) return true;
if (matchPattern(c,x,y,pattern8)) return true;
return false;
}
/**
* Skeletonize the image using succesive thinning.
*
* @param image the image in an array[x][y] of values "0" or "1"
* @param width of the image = 1st dimension of the array
* @param height of the image = 2nd dimension of the array
*/
public void thinning(byte[][] image,int width,int height) {
// 3 columns back-buffer (original values)
byte[][] buffer = new byte[3][height];
// initialize the back-buffer
for(int y=0;y<height;y++) {
buffer[0][y]=0;
buffer[1][y]=image[0][y];
buffer[2][y]=image[1][y];
}
// loop until idempotence
for(int loop=0;;loop++) {
boolean changed=false;
// for each columns
for(int x=1;x<(width-1);x++) {
// shift the back-buffer + set the last column
byte[] swp0 = buffer[0]; buffer[0]=buffer[1]; buffer[1]=buffer[2]; buffer[2]=swp0;
for(int y=0;y<height;y++) buffer[2][y]=image[x+1][y];
// for each pixel
for(int y=1;y<(height-1);y++) {
// pixel value
int v = image[x][y];
// pixel not set -> next
if (v==0) continue;
// is a boundary/extremity ?
int currentNeighbourhood = neighbourhood(buffer,1,y);
if (currentNeighbourhood<=1) continue;
if (currentNeighbourhood>=6) continue;
// is a connection ?
int transitionsCount = transitions(image,x,y);
if (transitionsCount==1 && currentNeighbourhood<=3) continue;
// no -> remove this pixel
if (transitionsCount==1) {
changed=true;
image[x][y]=0;
continue;
}
// can we delete this pixel ?
boolean matchOne = matchOneOfPatterns(image,x,y);
// yes -> remove this pixel
if (matchOne) {
changed=true;
image[x][y]=0;
continue;
}
}
}
// no change -> return result
if (!changed) return;
}
}
@Override
public void compute(Image out, Image in) throws Exception {
int width = in.getWidth();
int height = in.getHeight();
out.resize(width, height);
// create binary array from input image
byte[][] array = new byte[width][height];
for(int y=1;y<height-1;y++) {
for(int x=1;x<width-1;x++) {
int value = 0;
for(int i=0;i<in.getNumComponents();i++)
value+=(int)in.getPixel(x, y, i);
value/=in.getNumComponents();
if (value>=threshold) array[x][y]=1;
}
}
// perform the thinning
thinning(array, width, height);
// create output image
for(int y=1;y<height-1;y++) {
for(int x=1;x<width-1;x++) {
for(int i=0;i<in.getNumComponents();i++)
if (array[x][y]==0)
out.setPixel(x, y, i, 0);
else
out.setPixel(x, y, i, in.getPixel(x, y, i));
}
}
}
} |
Partager