Filtre de Canny (detection de contour) pour Image J



Code java : Sélectionner tout - Visualiser dans une fenêtre à part
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import ij.IJ;
import ij.ImagePlus;
import ij.gui.GenericDialog;
import ij.plugin.filter.PlugInFilter;
import ij.process.ByteProcessor;
import ij.process.ImageProcessor;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Canny Filter (edge detector)
 * 
 * @author Xavier Philippeau
 *
 */
public class Canny_ implements PlugInFilter {
 
	private int[][] gaussianKernel = null;
	private int gaussianKernelFactor = 0;
 
	private int lowThreshold=0;
	private int highThreshold=0;
 
	// About...
	private void showAbout() {
		IJ.showMessage("Canny...","Canny Filter by Pseudocode");
	}
 
	public int setup(String arg, ImagePlus imp) {
 
		// about...
		if (arg.equals("about")) {
			showAbout(); 
			return DONE;
		}
 
		// else...
		if (imp==null) return DONE;
 
		// Configuration dialog.
		GenericDialog gd = new GenericDialog("Parameters");
		gd.addNumericField("Gaussian kernel size",7,0);
		gd.addNumericField("Gaussian sigma2",3.0,1);
		gd.addNumericField("Hysteresis low value",30,0);
		gd.addNumericField("Hysteresis high value",100,0);
 
		int gaussianwindow = 0;
		double gaussiansigma2 = 0;
		int lowvalue = 0;
		int highvalue = 0;
 
		while(true) {
			gd.showDialog();
			if ( gd.wasCanceled() )	return DONE;
 
			gaussianwindow = (int) gd.getNextNumber();
			gaussiansigma2 = (double) gd.getNextNumber();
			lowvalue       = (int) gd.getNextNumber();
			highvalue      = (int) gd.getNextNumber();
 
			if (gaussianwindow<=0) continue;
			if (gaussiansigma2<=0) continue;
			if (lowvalue<=0) continue;
			if (highvalue<lowvalue) continue;
			break;
		}
		gd.dispose();
 
		initGaussianKernel(gaussianwindow,gaussiansigma2);
		this.lowThreshold=lowvalue;
		this.highThreshold=highvalue;
 
		return PlugInFilter.DOES_8G;
	}
 
	public void run(ImageProcessor ip) {
 
		// ImageProcessor -> ByteProcessor conversion
		ByteProcessor bp = new ByteProcessor(ip.getWidth(),ip.getHeight());
		for (int y = 0; y < ip.getHeight(); y++) {
			for (int x = 0; x < ip.getWidth(); x++) {
				bp.set(x,y,ip.getPixel(x,y));
			}
		}
 
		// canny filter
		ByteProcessor newbp = filter( bp, this.lowThreshold, this.highThreshold );
 
		// ByteProcessor -> ImageProcessor conversion
		ImageProcessor out = new ByteProcessor(ip.getWidth(),ip.getHeight());
		for (int y = 0; y < ip.getHeight(); y++) {
			for (int x = 0; x < ip.getWidth(); x++) {
				out.set(x,y,newbp.get(x,y));
			}
		}
		ImagePlus newImg = new ImagePlus("Canny Filter Result", out);
		newImg.show();
 
	}
 
	// ---------------------------------------------------------------------------------
 
 
	/**
         * Compute the gaussian kernel G(x,y) = Exp[ - (x^2+y^2)/(2*sigma^2) ]
         * 
         * @param window size of the kernel
         * @param sigma std-dev of the gaussian
         */
	private void initGaussianKernel(int window, double sigma2) {
		int aperture = window/2;
		this.gaussianKernel = new int[2*aperture+1][2*aperture+1];
 
		// factor to have only integers in the kernel 
		int intFactor = 1000;
 
		this.gaussianKernelFactor = 0;
		for(int dy=-aperture;dy<=aperture;dy++) {
			for(int dx=-aperture;dx<=aperture;dx++) {
				double e = Math.exp( - (dx*dx+dy*dy) / (2*sigma2) );
				int k = (int) Math.rint(intFactor * e); 
				this.gaussianKernel[dx+aperture][dy+aperture]=k;
				this.gaussianKernelFactor += k;
			}
		}
	}
 
	/**
         * Histogram stretching
         * 
         * @param c Image map
         */
	public static void histogramStretch(ByteProcessor bp) {
		int width = bp.getWidth();
		int height = bp.getHeight();
 
		// search min/max value in the image 
		int min=255,max=0;
		for (int y=0; y<height; y++) {
			for (int x=0; x<width; x++) {
				int v =  bp.get(x, y);
				if (v<min) min=v;
				if (v>max) max=v;
			}
		}
 
		// compute translation table
		int color[] = new int[256];
		for(int i=0;i<256;i++) {
			double t = (double)(i-min)/(double)(max-min);
			if (t<0) t=0;
			if (t>1) t=1;
			int v = (int)(255*t);
			color[i] = v;
		}
 
		// replace value in the image
		for (int y=0; y<height; y++) {
			for (int x=0; x<width; x++) {
				int v =  bp.get(x, y);
				bp.set(x, y, color[v]);
			}
		}
	}
 
	/**
         * Perform convolution (Image x Kernel) at a single point
         * 
         * @param c Image map
         * @param x x coord of the computation
         * @param y y coord of the computation
         * @param kernel the kernel matrix
         * @param factor the kernel factor
         * @return convolution result
         */
	private int convolve(ByteProcessor bp, int x, int y, int[][] kernel, int factor) {
		int width = bp.getWidth();
		int height = bp.getHeight();
 
		// assume a square kernel
		int aperture = kernel[0].length/2;  
 
		int v = 0;
		for(int dy=-aperture;dy<=aperture;dy++) {
			for(int dx=-aperture;dx<=aperture;dx++) {
				int xk = x + dx;
				int yk = y + dy;
				if (xk<0) xk=0;
				if (xk>=width) xk=width-1;
				if (yk<0) yk=0;
				if (yk>=height) yk=height-1;
				int vk = bp.getPixel(xk,yk);
				v += kernel[aperture+dy][aperture+dx] * vk;
			}
		}
		v/=factor;
 
		return v;
	}
 
 
	/**
         * Compute the local gradient
         * 
         * @param c Image map
         * @param x x coord of the computation (int)
         * @param y y coord of the computation (int)
         * @return norme and direction (-pi...pi) of the gradient
         */
	private double[] gradient(ByteProcessor bp, int x, int y) {
		int width = bp.getWidth();
		int height = bp.getHeight();
 
		int px = x - 1;  // previous x
		int nx = x + 1;  // next x
		int py = y - 1;  // previous y
		int ny = y + 1;  // next y
 
		// limit to image dimension
		if (px < 0)	px = 0;
		if (nx >= width) nx = width - 1;
		if (py < 0)	py = 0;
		if (ny >= height) ny = height - 1;
 
		// Intesity of the 8 neighbors
		int Ipp = bp.getPixel(px,py);
		int Icp = bp.getPixel( x,py);
		int Inp = bp.getPixel(nx,py);
		int Ipc = bp.getPixel(px, y);
		int Inc = bp.getPixel(nx, y);
		int Ipn = bp.getPixel(px,ny);
		int Icn = bp.getPixel( x,ny);
		int Inn = bp.getPixel(nx,ny);
 
		// Local gradient
		double r2 = 2*Math.sqrt(2);
		double gradx = (Inc-Ipc)/2.0 + (Inn-Ipp)/r2 + (Inp-Ipn)/r2; // horizontal + 2 diagonals
		double grady = (Icn-Icp)/2.0 + (Inn-Ipp)/r2 + (Ipn-Inp)/r2; // vertical + 2 diagonals
 
		// compute polar coordinates
		double norme = Math.sqrt(gradx*gradx+grady*grady);
		double angle = Math.atan2(grady, gradx);
 
		// multiply norme by 8/3, to have the same values as Sobel Kernel 3x3
		return new double[] { norme*8.0/3.0, angle };
	}
 
 
	/**
         * Compute the local gradient (subpixel with bilinear interpolation)
         * 
         * @param c Image map
         * @param x x coord of the computation (double)
         * @param y y coord of the computation (double)
         * @return norme and direction (-pi...pi) of the gradient
         */
	 private double[] gradientInterpolated(ByteProcessor bp, double x, double y) {
 
		 double wx = x - (int)x;
		 double wy = y - (int)y;
 
		 double[] Gpp = gradient(bp,(int)x,(int)y);
		 double[] Gnp = gradient(bp,(int)x+1,(int)y);
		 double[] Gpn = gradient(bp,(int)x,(int)y+1);
		 double[] Gnn = gradient(bp,(int)x+1,(int)y+1);
 
		 double norme = (1-wx)*(1-wy)*Gpp[0] + wx*(1-wy)*Gnp[0] + (1-wx)*wy*Gpn[0] + wx*wy*Gnn[0];
		 double angle = (1-wx)*(1-wy)*Gpp[1] + wx*(1-wy)*Gnp[1] + (1-wx)*wy*Gpn[1] + wx*wy*Gnn[1];
 
		 return new double[] { norme, angle };
	 }
 
 
	/**
         * compute if a position is a local maxima
         * 
         * @param c Image map
         * @param x x coord of the computation
         * @param y y coord of the computation
         * @return true if position is a local maxima
         */
	private boolean isLocalMaxima(ByteProcessor bp, int x, int y) {
 
		// gradient at current position
		double[] grad = gradient(bp,x,y);
 
		// gradient direction
		double gx = Math.cos(grad[1]);
		double gy = Math.sin(grad[1]);
 
		// gradient value at next position in the gradient direction
		double nx = x + gx;
		double ny = y + gy;
		double[] gradn = gradientInterpolated(bp,nx,ny);
 
		// gradient value at previous position in the gradient direction
		double px = x - gx;
		double py = y - gy;
		double[] gradp = gradientInterpolated(bp,px,py);
 
		// is the current gradient value a local maxima ?
 
		// synthetic image
		if (grad[0]==gradn[0] && grad[0]!=gradp[0]) return (x>nx || y>ny);
		if (grad[0]!=gradn[0] && grad[0]==gradp[0]) return (x>px || y>py);
 
		// real world image
		double EPSILON=1E-7;
		if ((grad[0]-gradn[0])<EPSILON) return false;
		if ((grad[0]-gradp[0])<EPSILON) return false;
 
		return true;
	}
 
	/**
         * Perfom Canny filtering
         * 
         * @param c Image map
         * @param lowThreshold low value of the hysteresis
         * @param highThreshold high value of the hysteresis
         * @return filtered image map
         */
	public ByteProcessor filter(ByteProcessor c, int lowThreshold, int highThreshold) {
		int width = c.getWidth();
		int height = c.getHeight();
 
		// Gaussian filter
		ByteProcessor bpg = new ByteProcessor(width,height);
		for (int y=0; y<height; y++) {
			for (int x=0; x<width; x++) {
				int v = convolve(c,x,y,this.gaussianKernel,this.gaussianKernelFactor);
				bpg.set(x,y,v);
			}
		}
 
		// histogram stretching (compensate the loss of energy caused by gaussian filtering)
		histogramStretch(bpg);
 
		ByteProcessor out = new ByteProcessor(width,height);
		List<int[]> highpixels = new ArrayList<int[]>();
 
		// gradient thresholding
		for (int y=0; y<height; y++) {
			for (int x=0; x<width; x++) {
 
				// gradient intesity
				double g = gradient(bpg,x,y)[0];
 
				// low-threshold -> not an edge
				if (g<lowThreshold) continue;
 
				// not a local maxima -> not an edge
				if (!isLocalMaxima(bpg,x,y)) continue;
 
				// high-threshold -> is an edge
				if (g>highThreshold) {
					out.set(x,y,255);
					highpixels.add(new int[]{x,y});
					continue;
				}
 
				// between thresholds -> "unknown state" (depends on neighbors)
				out.set(x,y,128);
			}
		}
 
		// edge continuation
		int[] dx8 = new int[] {-1, 0, 1, 1, 1, 0,-1,-1};
		int[] dy8 = new int[] {-1,-1,-1, 0, 1, 1, 1, 0};
		List<int[]> newhighpixels = new ArrayList<int[]>();
 
		while(!highpixels.isEmpty()) {
			newhighpixels.clear();
			for(int[] pixel : highpixels) {
				int x=pixel[0], y=pixel[1];
 
				// move low-state pixel in the 3x3 neighborhood to high-state
				for(int k=0;k<8;k++) {
					int xk=x+dx8[k], yk=y+dy8[k];
					if (xk<0 || xk>=width) continue;
					if (yk<0 || yk>=height) continue;
					if (out.get(xk, yk)==128) {
						out.set(xk, yk, 255);
						newhighpixels.add(new int[]{xk, yk});
					}
				}
			}
 
			// swap highpixels lists
			List<int[]> swap = highpixels; highpixels = newhighpixels; newhighpixels = swap;
		}
 
		// remove remaining low-state pixels
		for (int y=0; y<height; y++)
			for (int x=0; x<width; x++)
				if (out.get(x, y)!=255) out.set(x,y,0);
 
		return out;
	}
}

Edit: J'ai reposté la version complete du filtre, avec utilsation du gradient vectoriel, interpolation bi-lineaire et extension d'histogramme... Necessite de compiler avec un compilateur java 5 (ou +) => ne pas utiliser l'option "compil & run" de ImageJ

PRomu@ld : sources