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
| /**
* Skeleton Filter for gray-levels images
*
* Original algorithm : Dr. Chai Quek
* Modified algorithm : Xavier Philippeau
*
* @author Xavier Philippeau (based on work of Dr. Chai Quek)
*
*/
public class SkeletonGrayFast {
// 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};
// Neighbourhood
private int neighbourhood(int[][] c,int x,int y) {
int neighbourhood=0;
int ref=c[x][y];
if (ref <= c[x-1][y-1]) neighbourhood++;
if (ref <= c[x-1][y ]) neighbourhood++;
if (ref <= c[x-1][y+1]) neighbourhood++;
if (ref <= c[x ][y+1]) neighbourhood++;
if (ref <= c[x+1][y+1]) neighbourhood++;
if (ref <= c[x+1][y ]) neighbourhood++;
if (ref <= c[x+1][y-1]) neighbourhood++;
if (ref <= c[x ][y-1]) neighbourhood++;
return neighbourhood;
}
// Transitions Count
private int transitions(int[][] c,int x,int y) {
int transitions=0;
int ref=c[x][y];
if (c[x-1][y-1]<ref && c[x-1][y ]>=ref) transitions++;
if (c[x-1][y ]<ref && c[x-1][y+1]>=ref) transitions++;
if (c[x-1][y+1]<ref && c[x ][y+1]>=ref) transitions++;
if (c[x ][y+1]<ref && c[x+1][y+1]>=ref) transitions++;
if (c[x+1][y+1]<ref && c[x+1][y ]>=ref) transitions++;
if (c[x+1][y ]<ref && c[x+1][y-1]>=ref) transitions++;
if (c[x+1][y-1]<ref && c[x ][y-1]>=ref) transitions++;
if (c[x ][y-1]<ref && c[x-1][y-1]>=ref) transitions++;
return transitions;
}
// math a single pixel
private boolean matchPixel(int ref, int pixel, byte pattern) {
if (pattern==-1) return true;
if (pattern==0 && pixel<ref) return true;
if (pattern==1 && pixel>=ref) return true;
return false;
}
// Match a pattern
private boolean matchPattern(int[][] c,int x,int y,byte[] pattern) {
int ref=c[x][y];
if (!matchPixel(ref,c[x-1][y-1],pattern[0])) return false;
if (!matchPixel(ref,c[x-1][y ],pattern[1])) return false;
if (!matchPixel(ref,c[x-1][y+1],pattern[2])) return false;
if (!matchPixel(ref,c[x ][y+1],pattern[3])) return false;
if (!matchPixel(ref,c[x+1][y+1],pattern[4])) return false;
if (!matchPixel(ref,c[x+1][y ],pattern[5])) return false;
if (!matchPixel(ref,c[x+1][y-1],pattern[6])) return false;
if (!matchPixel(ref,c[x ][y-1],pattern[7])) return false;
return true;
}
// Match one of the 8 patterns
private boolean matchOneOfPatterns(int[][] 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(int[][] image,int width,int height) {
// 3 columns back-buffer (original values)
int[][] buffer = new int[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
int[] 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;
}
}
} |