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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
| package millie.plugins.free;
import java.awt.image.BufferedImage;
import millie.plugins.PluginInfo;
import millie.plugins.appimage.GenericAppImagePlugin;
import millie.plugins.parameters.CheckBoxParameter;
import millie.plugins.parameters.IntSliderParameter;
/**
* @author Xavier Philippeau
*
*/
@PluginInfo(name="Skeleton (grayscale)", category="Morphologique", description="Skeleton for grayscale image")
public class SkeletonGrayscalePlugin extends GenericAppImagePlugin {
public SkeletonGrayscalePlugin() {
setReinitializable(true);
setLongProcessing(true);
addParameter(new IntSliderParameter("step", "Gray Level step", 1, 128, 16));
addParameter(new CheckBoxParameter("bright", "Bright background", false) );
}
@Override
public BufferedImage filter() throws Exception {
int step = getIntValue("step");
boolean useBrightBackground = getBooleanValue("bright");
// input image
BufferedImage input = getInputImage();
int W=input.getWidth(), H=input.getHeight();
int[][] gray = new int[W][H];
// convert input image to gray level array
for(int y=0;y<H;y++) {
for(int x=0;x<W;x++) {
int rgb = input.getRGB(x, y);
int r = (rgb>>16) & 0xFF, g = (rgb>>8) & 0xFF, b = (rgb) & 0xFF;
gray[x][y] = (int)(0.5 + 0.299*r + 0.587*g + 0.114*b);
}
}
// use negative image if required
if (useBrightBackground) {
for(int y=0;y<H;y++) for(int x=0;x<W;x++) gray[x][y] = 255 - gray[x][y];
}
// thinning using multiple threshold
int[][] result = multipleThresholdThinning(gray, W, H, step);
// generate output image
BufferedImage output = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
for(int y=0;y<H;y++) {
for(int x=0;x<W;x++) {
output.getRaster().setSample(x, y, 0, result[x][y]);
output.getRaster().setSample(x, y, 1, result[x][y]);
output.getRaster().setSample(x, y, 2, result[x][y]);
}
}
return output;
}
// ----------------------------------------------------------------------------------------------------
public int[][] multipleThresholdThinning(int[][] image, int width, int height, int step) {
// histogram of gray levels
int[] grayhisto = new int[256];
for(int y=0;y<height;y++)
for(int x=0;x<width;x++)
grayhisto[ image[x][y] ]++;
// CDF of gray levels
int[] graycdf = new int[256];
graycdf[0]=0;
for(int i=1;i<256;i++) graycdf[i]=graycdf[i-1]+grayhisto[i];
// accumulator of pixels belonging to a skeleton
int[][] accumulator = new int[width][height];
// work image
byte[][] work = new byte[width][height];
// ** multiple binary thinning for different threshold values **
int plevel=0,level=0;
int EQStep = (int)(0.5+graycdf[255]/(double)step); // step in equalized image
while(true) {
// find next threshold values
while( (level<256) && (graycdf[level]-graycdf[plevel])<EQStep ) level++;
// update accumulator
for(int y=0;y<height;y++)
for(int x=0;x<width;x++)
if (work[x][y]==1) accumulator[x][y]+=(level-plevel);
if (level>=256) break;
plevel=level;
System.out.println("Skeleton : binary thinning using threshold="+level);
// create the thresholded image
for(int y=0;y<height;y++)
for(int x=0;x<width;x++)
if (image[x][y]>=level) work[x][y]=1; else work[x][y]=0;
// perform binary thinning
new SkeletonBinary().thinning(work, width, height);
}
/*
for(int level=1;level<255;level++) {
// if we reach a new valid level
if( (graycdf[level]-graycdf[plevel]) >= DELTA ) {
plevel=level;
System.out.println("Skeleton : binary thinning using threshold="+level+" cdf="+graycdf[level]);
// create the thresholded image
for(int y=0;y<height;y++)
for(int x=0;x<width;x++)
if (image[x][y]>level) work[x][y]=1; else work[x][y]=0;
// perform binary thinning
new SkeletonBinary().thinning(work, width, height);
}
// update accumulator
for(int y=0;y<height;y++)
for(int x=0;x<width;x++)
if (work[x][y]==1) accumulator[x][y]++;
}
*/
// rescale accumulator in 0...255
int max=0;
for(int y=0;y<height;y++)
for(int x=0;x<width;x++)
max = Math.max(max,accumulator[x][y]);
if (max>0) {
for(int y=0;y<height;y++)
for(int x=0;x<width;x++)
accumulator[x][y] = (int)(0.5+accumulator[x][y]*255.0/max);
}
// return the accumulator
return accumulator;
}
// ----------------------------------------------------------------------------------------------------
/** @author Xavier Philippeau (based on work of Dr. Chai Quek) */
class SkeletonBinary {
// 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(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
while(true) {
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;
}
}
}
} |