Bonsoir à tous!
J'utilise actuellement le code suivant qui me permet de détecter les bords d'une image et de me donner le résultat en sortie dans une fenêtre Java.
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
402
403
404
405
406
407
408
409
410
411
package mix2;
 
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
 
public class Mix2 {
	public static void main(String[] args) throws Exception {
		try{
			JFrame capture = new JFrame();
			capture.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
			Toolkit kit = Toolkit.getDefaultToolkit();
			final Dimension d = kit.getScreenSize();
			final BufferedImage image = ImageIO.read(new File("1b.bmp"));
			image.flush();
			d.setSize(image.getWidth(), image.getHeight());
 
			capture.setSize(d);
			capture.setLocation(300, 200);
			EdgeDetector edgeDetector = new EdgeDetector();
			edgeDetector.setSourceImage(image);
			edgeDetector.setThreshold(128);
			edgeDetector.setWidGaussianKernel(5);
			edgeDetector.process();
			//final int[] raw = edgeDetector.image2pixels(image);
			//final Image bild = edgeDetector.pixels2image(raw);
			final Image srcimg = edgeDetector.getSourceImage();
			final Image newimg = edgeDetector.getEdgeImage();
			Rectangle rect = new Rectangle(d);
			try {
				Robot robot = new Robot();
				//final BufferedImage image = robot.createScreenCapture(rect);
				//final BufferedImage image = (BufferedImage)Toolkit.getDefaultToolkit().getImage("C:\\IOTest\\Bildle.jpg");
 
				JPanel panel = new JPanel() {
					public void paintComponent(Graphics g) {
						//g.drawImage(image, 0, 0, d.width, d.height, this);
						//g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), this);
						//g.drawImage(srcimg, 0, 0, image.getWidth(), image.getHeight(), this);
						//g.drawImage(bild, 0, 0, image.getWidth(), image.getHeight(), this);
						g.drawImage(newimg, 0, 0, image.getWidth(), image.getHeight(), this);
					}
				};
				panel.setOpaque(false);
				panel.prepareImage(image, panel);
				panel.repaint();
				capture.getContentPane().add(panel);
			} catch (Exception e) {
				e.printStackTrace();
			}
			capture.setVisible(true);
 
			BufferedImage bi2 = (BufferedImage)edgeDetector.getEdgeImage();
			FileOutputStream out = new FileOutputStream("C:\\Files\1b.jpg");
			ImageIO.write((RenderedImage)newimg, "jpg", out); // why does this not work?
			//JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			//encoder.encode(bi2);
			out.flush();
			out.close();
 
 
		}catch(Exception e){ e.getMessage();}
		return;
	} // main
} // class
 
 
 
class EdgeDetector extends Component {
 
final float ORIENT_SCALE = 40F;
private int height;
private int width;
private int picsize;
private int data[];
private int derivative_mag[];
private int magnitude[];
private int orientation[];
public Image sourceImage;
public Image edgeImage;
private int threshold1;
private int threshold2;
private int threshold;
private int widGaussianKernel;
 
 
 
public EdgeDetector() {
threshold1 = 50;
threshold2 = 230;
setThreshold(128);
setWidGaussianKernel(15);
}
 
public void process() throws Exception {
//if (threshold < 0 || threshold > 255)
//throw new Exception("The value of the threshold is out of its valid range.");
//if (widGaussianKernel < 3 || widGaussianKernel > 40)
 
width = sourceImage.getWidth(this);
height = sourceImage.getHeight(this);
picsize = width * height;
data = new int[picsize];
magnitude = new int[picsize];
orientation = new int[picsize];
float f = 1.0F;
canny_core(f, widGaussianKernel);
thresholding_tracker(threshold1, threshold2);
for (int i = 0; i < picsize; i++)
if (data[i] > threshold)
data[i] = 0xff000000;
else
data[i] = -1;
 
edgeImage = pixels2image(data);
data = null;
magnitude = null;
orientation = null;
} // process ///////////////////////////////////////
 
private void canny_core(float f, int i) {
boolean flag = false;
boolean flag1 = false;
derivative_mag = new int[picsize];
float af4[] = new float[i];
float af5[] = new float[i];
float af6[] = new float[i];
data = image2pixels(sourceImage);
int k4 = 0;
do {
if (k4 >= i)
break;
float f1 = gaussian(k4, f);
if (f1 <= 0.005F && k4 >= 2)
break;
float f2 = gaussian((float) k4 - 0.5F, f);
float f3 = gaussian((float) k4 + 0.5F, f);
float f4 = gaussian(k4, f * 0.5F);
af4[k4] = (f1 + f2 + f3) / 3F / (6.283185F * f * f);
af5[k4] = f3 - f2;
af6[k4] = 1.6F * f4 - f1;
k4++;
} while (true);
int j = k4;
float af[] = new float[picsize];
float af1[] = new float[picsize];
int j1 = width - (j - 1);
int l = width * (j - 1);
int i1 = width * (height - (j - 1));
for (int l4 = j - 1; l4 < j1; l4++) {
for (int l5 = l; l5 < i1; l5 += width) {
int k1 = l4 + l5;
float f8 = (float) data[k1] * af4[0];
float f10 = f8;
int l6 = 1;
int k7 = k1 - width;
for (int i8 = k1 + width; l6 < j; i8 += width) {
f8 += af4[l6] * (float) (data[k7] + data[i8]);
f10 += af4[l6] * (float) (data[k1 - l6] + data[k1 + l6]);
l6++;
k7 -= width;
}
 
af[k1] = f8;
af1[k1] = f10;
}
 
}
 
float af2[] = new float[picsize];
for (int i5 = j - 1; i5 < j1; i5++) {
for (int i6 = l; i6 < i1; i6 += width) {
float f9 = 0.0F;
int l1 = i5 + i6;
for (int i7 = 1; i7 < j; i7++)
f9 += af5[i7] * (af[l1 - i7] - af[l1 + i7]);
 
af2[l1] = f9;
}
 
}
 
af = null;
float af3[] = new float[picsize];
for (int j5 = k4; j5 < width - k4; j5++) {
for (int j6 = l; j6 < i1; j6 += width) {
float f11 = 0.0F;
int i2 = j5 + j6;
int j7 = 1;
for (int l7 = width; j7 < j; l7 += width) {
f11 += af5[j7] * (af1[i2 - l7] - af1[i2 + l7]);
j7++;
}
 
af3[i2] = f11;
}
 
}
 
af1 = null;
j1 = width - j;
l = width * j;
i1 = width * (height - j);
for (int k5 = j; k5 < j1; k5++) {
for (int k6 = l; k6 < i1; k6 += width) {
int j2 = k5 + k6;
int k2 = j2 - width;
int l2 = j2 + width;
int i3 = j2 - 1;
int j3 = j2 + 1;
int k3 = k2 - 1;
int l3 = k2 + 1;
int i4 = l2 - 1;
int j4 = l2 + 1;
float f6 = af2[j2];
float f7 = af3[j2];
float f12 = hypotenuse(f6, f7);
int k = (int) ((double) f12 * 20D);
derivative_mag[j2] = k >= 256 ? 255 : k;
float f13 = hypotenuse(af2[k2], af3[k2]);
float f14 = hypotenuse(af2[l2], af3[l2]);
float f15 = hypotenuse(af2[i3], af3[i3]);
float f16 = hypotenuse(af2[j3], af3[j3]);
float f18 = hypotenuse(af2[l3], af3[l3]);
float f20 = hypotenuse(af2[j4], af3[j4]);
float f19 = hypotenuse(af2[i4], af3[i4]);
float f17 = hypotenuse(af2[k3], af3[k3]);
float f5;
if (f6 * f7 <= (float) 0
? Math.abs(f6) >= Math.abs(f7)
? (f5 = Math.abs(f6 * f12))
>= Math.abs(f7 * f18 - (f6 + f7) * f16)
&& f5
> Math.abs(f7 * f19 - (f6 + f7) * f15) : (
f5 = Math.abs(f7 * f12))
>= Math.abs(f6 * f18 - (f7 + f6) * f13)
&& f5
> Math.abs(f6 * f19 - (f7 + f6) * f14) : Math.abs(f6)
>= Math.abs(f7)
? (f5 = Math.abs(f6 * f12))
>= Math.abs(f7 * f20 + (f6 - f7) * f16)
&& f5
> Math.abs(f7 * f17 + (f6 - f7) * f15) : (
f5 = Math.abs(f7 * f12))
>= Math.abs(f6 * f20 + (f7 - f6) * f14)
&& f5 > Math.abs(f6 * f17 + (f7 - f6) * f13)) {
magnitude[j2] = derivative_mag[j2];
orientation[j2] = (int) (Math.atan2(f7, f6) * (double) 40F);
}
}
 
}
 
derivative_mag = null;
af2 = null;
af3 = null;
} // canny_core
 
private float hypotenuse(float f, float f1) {
if (f == 0.0F && f1 == 0.0F)
return 0.0F;
else
return (float) Math.sqrt(f * f + f1 * f1);
}
 
private float gaussian(float f, float f1) {
return (float) Math.exp((-f * f) / ((float) 2 * f1 * f1));
}
 
private void thresholding_tracker(int i, int j) {
for (int k = 0; k < picsize; k++)
data[k] = 0;
 
for (int l = 0; l < width; l++) {
for (int i1 = 0; i1 < height; i1++)
if (magnitude[l + width * i1] >= i)
follow(l, i1, j);
 
}
 
}
 
private boolean follow(int i, int j, int k) {
int j1 = i + 1;
int k1 = i - 1;
int l1 = j + 1;
int i2 = j - 1;
int j2 = i + j * width;
if (l1 >= height)
l1 = height - 1;
if (i2 < 0)
i2 = 0;
if (j1 >= width)
j1 = width - 1;
if (k1 < 0)
k1 = 0;
if (data[j2] == 0) {
data[j2] = magnitude[j2];
boolean flag = false;
int l = k1;
do {
if (l > j1)
break;
int i1 = i2;
do {
if (i1 > l1)
break;
int k2 = l + i1 * width;
if ((i1 != j || l != i)
&& magnitude[k2] >= k
&& follow(l, i1, k)) {
flag = true;
break;
}
i1++;
} while (true);
if (!flag)
break;
l++;
}
while (true);
return true;
} else {
return false;
}
} // follow
/////////////////////////////////////////////
public Image pixels2image(int ai[]) {
MemoryImageSource memoryimagesource =
new MemoryImageSource(
width,
height,
ColorModel.getRGBdefault(),
ai,
0,
width);
return Toolkit.getDefaultToolkit().createImage(memoryimagesource);
}
///////////////////////////////////////////////
public int[] image2pixels(Image image) {
int ai[] = new int[picsize];
PixelGrabber pixelgrabber =
new PixelGrabber(image, 0, 0, width, height, ai, 0, width);
try {
pixelgrabber.grabPixels();
} catch (InterruptedException interruptedexception) {
interruptedexception.printStackTrace();
}
boolean flag = false;
int k1 = 0;
do {
if (k1 >= 16)
break;
int i = (ai[k1] & 0xff0000) >> 16;
int k = (ai[k1] & 0xff00) >> 8;
int i1 = ai[k1] & 0xff;
if (i != k || k != i1) {
flag = true;
break;
}
k1++;
} while (true);
if (flag) {
for (int l1 = 0; l1 < picsize; l1++) {
int j = (ai[l1] & 0xff0000) >> 16;
int l = (ai[l1] & 0xff00) >> 8;
int j1 = ai[l1] & 0xff;
ai[l1] =
(int) (0.29799999999999999D * (double) j
+ 0.58599999999999997D * (double) l
+ 0.113D * (double) j1); 
}
} else {
for (int i2 = 0; i2 < picsize; i2++)
ai[i2] = ai[i2] & 0xff;
}
return ai; // int[]
}
////////////////////////////////////////////////////////
 
public void setSourceImage(Image image) {
sourceImage = image;
}
public Image getSourceImage() {
Image srcimage = sourceImage;
return srcimage;
}
public Image getEdgeImage() {
return edgeImage;
}
 
public void setThreshold(int i) {
threshold = i;
}
 
public void setWidGaussianKernel(int i) {
widGaussianKernel = i;
}
 
} // class

Mon souci, c'est que j'ai besoin par la suite de retravailler cette image avec GIMP, j'ai besoin d'une image en sortie en jpg.... j'ai bien essayé ceci:

Code Java : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
BufferedImage bi2 = (BufferedImage)edgeDetector.getEdgeImage();
			FileOutputStream out = new FileOutputStream("C:\\Files\1b.jpg");
			ImageIO.write((RenderedImage)newimg, "jpg", out); // why does this not work?
			//JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			//encoder.encode(bi2);
			out.flush();
			out.close();
Mais j'ai dû faire une erreur quelquepart, soit dans l'emplacement, soit dans l'écriture, qu'en pensez-vous?

Amicalement