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
|
package analyse;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.Histogram;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RasterFactory;
import javax.media.jai.TiledImage;
/**
* Skeleton (v2)
*
* Original algorithm : Dr. Chai Quek
* Modified algorithm : Xavier Philippeau
*
* @author Xavier Philippeau (based on work of Dr. Chai Quek)
*
*/
public class Skeleton {
// 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.
*
*/
public PlanarImage thinning(PlanarImage imag) {
BufferedImage img= imag.getAsBufferedImage();
int height= img.getHeight();
int width=img.getWidth();
// 3 columns back-buffer (original values)
byte[][]tablo=new byte[width][height];
for(int y=0;y<height;y++){
for(int x=0;x<width;x++){
byte rgb = (byte) img.getRGB(x,y);
if (rgb==-1) tablo[x][y]=1;
else tablo[x][y]=0;
}
}
byte[][] image= tablo;
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) {System.out.println("non"); return imag;}
else {
int[] imageDataSingleArray = new int[width*height];
int count=0;
for(int h=0;h<height;h++)
for(int w=0;w<width;w++){
if (image[w][h]== 1 ) imageDataSingleArray[count++] = 255;
else imageDataSingleArray[count++] = 0;
}
DataBufferInt dbuffer = new DataBufferInt(imageDataSingleArray,width*height);
// Create a byte data sample model.
SampleModel sampleModel =
RasterFactory.createBandedSampleModel(DataBuffer.TYPE_INT,width,height,1);
// Create a compatible ColorModel.
ColorModel colorModel = PlanarImage.createColorModel(sampleModel);
// Create a WritableRaster.
Raster raster = RasterFactory.createWritableRaster(sampleModel,dbuffer,new Point(0,0));
// Create a TiledImage using the SampleModel and ColorModel.
TiledImage tiledImage = new TiledImage(0,0,width,height,0,0,sampleModel,colorModel);
// Set the data of the tiled image to be the raster.
tiledImage.setData(raster);
PlanarImage result = tiledImage;
ParameterBlock pbConvert = new ParameterBlock();
pbConvert.addSource(result);
pbConvert.add(DataBuffer.TYPE_BYTE);
result = JAI.create("format", pbConvert);
System.out.println(result);
return result;
}
}
}
} |
Partager