-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageAugmentor.java
More file actions
486 lines (391 loc) · 14.3 KB
/
ImageAugmentor.java
File metadata and controls
486 lines (391 loc) · 14.3 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//package my.packages;
import ij.*;
import ij.process.*;
import ij.CompositeImage;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;
import ij.plugin.frame.*;
import java.util.Random;
import java.awt.Graphics2D;
import ij.plugin.filter.GaussianBlur;
import java.io.*;
import ij.io.OpenDialog;
import ij.gui.GenericDialog;
import java.awt.Font;
import fiji.util.gui.GenericDialogPlus;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.Icon;
import javax.swing.*;
import javax.swing.JComponent;
import javax.swing.JTabbedPane;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* <h3> Packages for basic image Augmentation operations using ImageJ - Jithin Mathew</h3>
*
* Author: Jithin Mathew
* Versoin: 1.0
* ImageJ (FiJi version):
*/
public class ImageAugmentor implements PlugIn {
Color bk = Color.black;
Color wi = Color.white;
Color bgc = new Color(71,75,80);
Color fgc = new Color(200,200,200);
public void run(String arg) {
Font font0 = new Font("Arial", Font.BOLD, 16);
Font font1 = new Font("Arial", Font.BOLD, 14);
Font font2 = new Font("Arial", Font.BOLD, 12);
ImageIcon icon = new ImageIcon("Path to logo .png");
ImageIcon icon2 = new ImageIcon("Path to logo .png");
GenericDialogPlus gui = new GenericDialogPlus("Image Augmenter");
gui.setBackground(bgc);
gui.setForeground(fgc);
gui.addImage(icon);
gui.addToSameRow();
gui.addMessage("Augment images in X folds for large scale deep learning ", font0, Color.white);
gui.addDirectoryField("Select Input Image directory:", "Path to source image directory /Batch_Input_Images");
gui.addDirectoryField("Select Save Output directory:", "Path to output image directory / Batch_Output_Images");
gui.addNumericField("Generate Images:",20);
gui.addMessage("Keep the probability high to augment most of the images", font2, Color.white);
gui.addMessage("By default the parameters are set for best performance", font2, Color.white);
gui.addToSameRow();
gui.addSlider(" ",0,100,80);
gui.addToSameRow();
gui.addMessage("Overall Probability (recommended:80%)", font2, fgc);
gui.addCheckbox("Random Flip (Horizontal/Vertical)", true);
gui.addToSameRow();
gui.addSlider("Probability",0,100,50);
gui.addCheckbox("Random Scaling", true);
gui.addToSameRow();
gui.addSlider("Probability",1,70,5);
gui.addToSameRow();
gui.addSlider("Max and Min Scaling",0.1,1.5,1.1);
gui.addCheckbox("Random Rotation", true);
gui.addToSameRow();
gui.addSlider("Probability",0,100,50);
gui.addToSameRow();
gui.addSlider("Max and Min of rotation",0,360,280);
gui.addCheckbox("Random Noise", true);
gui.addToSameRow();
gui.addSlider("Probability",1,100,50);
gui.addToSameRow();
gui.addSlider("Noise Factor",1,200,100);
gui.addCheckbox("Random Exponential Transform", true);
gui.addToSameRow();
gui.addSlider("Probability",1,100,50);
gui.addCheckbox("Random Gamma", true);
gui.addToSameRow();
gui.addSlider("Probability",1,100,50);
gui.addToSameRow();
gui.addSlider("Gamma value",1,10,2);
gui.addCheckbox("Contrast", true);
gui.addToSameRow();
gui.addSlider("Probability",1,100,50);
gui.addToSameRow();
gui.addSlider("Scale",1,100,50);
gui.addCheckbox("Brightness", true);
gui.addToSameRow();
gui.addSlider("Probability",1,100,50);
gui.addToSameRow();
gui.addSlider("Scale",1,100,50);
gui.addCheckbox("Smooth", true);
gui.addToSameRow();
gui.addCheckbox("Sharpen", true);
gui.addToSameRow();
gui.addSlider("Smooth and Sharpen P",1,100,50);
gui.addCheckbox("Gaussian Distortion", true);
gui.addToSameRow();
gui.addSlider("Probability",1,100,50);
gui.addToSameRow();
gui.addSlider("Scale",1,100,1);
String[] imageOutFormat = {"PNG", "JPG", "JPEG", "GIF"};
gui.addChoice("Image Out Format",imageOutFormat,"PNG");
gui.addImage(icon2);
gui.addToSameRow();
gui.addMessage("__-Jithin Mathew_", font0, Color.white);
gui.showDialog();
if (gui.wasCanceled()) return;
String FolderPath1 = gui.getNextString();
String FolderPath2 = gui.getNextString();
double A_num = gui.getNextNumber();
double A_prob = gui.getNextNumber();
Boolean flip = gui.getNextBoolean();
double flip_per = gui.getNextNumber();
Boolean scale = gui.getNextBoolean();
double scale_per = gui.getNextNumber();
double scale_minmax = gui.getNextNumber();
Boolean rot = gui.getNextBoolean();
double rotation_per = gui.getNextNumber();
double rotation_ang = gui.getNextNumber();
Boolean noise = gui.getNextBoolean();
double noise_per = gui.getNextNumber();
double noise_factor = gui.getNextNumber();
Boolean exptr = gui.getNextBoolean();
double exptr_per = gui.getNextNumber();
Boolean gamm = gui.getNextBoolean();
double gamm_per = gui.getNextNumber();
double gamm_factor = gui.getNextNumber();
Boolean contra = gui.getNextBoolean();
double contra_percent = gui.getNextNumber();
double contra_scale = gui.getNextNumber();
Boolean bri = gui .getNextBoolean();
double bri_percent = gui.getNextNumber();
double bri_scale = gui.getNextNumber();
Boolean smooth = gui.getNextBoolean();
Boolean sharpen = gui.getNextBoolean();
double S_and_S_prob = gui.getNextNumber();
Boolean Gdisto = gui.getNextBoolean();
double gdisto_percent = gui.getNextNumber();
double gdisto_scale = gui.getNextNumber();
String Oformat = gui.getNextChoice();
IJ.log("Image directory : "+FolderPath1);
IJ.log("Output directory : "+FolderPath2);
IJ.log("Flip : "+flip);
IJ.log("Flip Probability : "+flip_per);
IJ.log("Scale : "+scale);
IJ.log("Scale Probability : "+scale_per);
IJ.log("Scale by Factor : "+scale_per);
IJ.log("Rotate : "+rot);
IJ.log("Rotate Probability : "+rotation_per);
IJ.log("Rotate Maximum Angle: "+rotation_ang);
IJ.log("Noise : "+noise);
IJ.log("Noise Probability : "+noise_per);
IJ.log("Noise factor: "+noise_factor);
IJ.log("Exponential : "+exptr);
IJ.log("Exponential Probability : "+exptr_per);
IJ.log("Gamma : "+gamm);
IJ.log("Gamma Probability : "+gamm_per);
IJ.log("Gamma Factor : "+gamm_factor);
IJ.log("Contrast : "+contra);
IJ.log("Contrast Probability : "+contra_percent);
IJ.log("Contrast Scale : "+contra_scale);
IJ.log("Brightness : "+bri);
IJ.log("Brightness Probability : "+bri_percent);
IJ.log("Brightness Scale : "+bri_scale);
IJ.log("Smooth : "+smooth);
IJ.log("Sharpen : "+sharpen);
IJ.log("Smoothness and Sharpness Probability : "+S_and_S_prob);
IJ.log("Gaussian Blur : "+Gdisto);
IJ.log("Gaussian Blur Probability : "+gdisto_percent);
IJ.log("Gaussian Blur Factor : "+gdisto_scale);
IJ.log("Output Format : "+Oformat);
String[] list = new File(FolderPath1).list(); // this section of the code reads the files in the folder and save it to a list
for (int i=0; i<list.length; i++) { // Iterate through list and print the filenames
IJ.log(list[i]);
}
IJ.log(""+list.length);
java.util.Random random = new java.util.Random();
int aug = 0;
List<String> boolArray = new ArrayList<String>();
List<Double> doubleArray = new ArrayList<Double>();
if (flip == true){
boolArray.add("FLIP");
doubleArray.add(flip_per);
}
if (scale == true){
boolArray.add("SCALE");
doubleArray.add(scale_per);
}
if (rot == true){
boolArray.add("ROTATION");
doubleArray.add(rotation_per);
}
if (noise == true){
boolArray.add("NOISE");
doubleArray.add(noise_per);
}
if (exptr == true){
boolArray.add("EXPTR");
doubleArray.add(exptr_per);
}
if (gamm == true){
boolArray.add("GAMMA");
doubleArray.add(gamm_per);
}
if (contra == true){
boolArray.add("CONTRAST");
doubleArray.add(contra_percent);
}
if (bri == true){
boolArray.add("BRIGHTNESS");
doubleArray.add(bri_percent);
}
if (smooth == true){
boolArray.add("SMOOTH");
doubleArray.add(S_and_S_prob);
}
if (sharpen == true){
boolArray.add("SHARPEN");
doubleArray.add(S_and_S_prob);
}
if (Gdisto == true){
boolArray.add("GDISTORTION");
doubleArray.add(gdisto_percent);
}
IJ.log("List of methods chosen : "+boolArray);
IJ.log("Corresponding probabilities : "+doubleArray);
int count=0;
for (aug = 0; aug<= A_num; aug++){
int random_chosenFile = random.nextInt(list.length); // generate a random number based on the lenth of the list
IJ.log("Random choise "+list[random_chosenFile]); // chose a random file from the list based on the random number
if (boolArray.size()>0){
count+=1;
int random_AugMethod = random.nextInt(boolArray.size());
IJ.log(count+" Method choise "+boolArray.get(random_AugMethod));
displayImage(boolArray.get(random_AugMethod), FolderPath1 +"\\"+ list[random_chosenFile], FolderPath2, doubleArray.get(random_AugMethod), count, scale_minmax, rotation_ang, noise_factor, gamm_factor, contra_scale, bri_scale, gdisto_scale, Oformat);
}
else {
IJ.log("No Augmentation Method chosen");
}
}
}
void displayImage(String method, String image, String FolderPath2, double prob_ , int count, double scale_minmax, double rotation_ang, double noise_factor, double gamm_factor, double contra_scale, double bri_scale, double gdisto_scale, String OF){
IJ.open(image);
ImagePlus imp = IJ.getImage();
ImageProcessor ip = imp.getProcessor();
java.util.Random ovrl_random = new java.util.Random();
int main_prob = ovrl_random.nextInt(100);
if (method == "FLIP" & prob_ >= main_prob){
radomFlip(ip);
}
if (method == "EXPTR" & prob_ >= main_prob){
randomExponential(ip);
}
if (method == "SCALE" & prob_ >= main_prob){
// IJ.log("SCALE "+scale_minmax +" "+ scale_minmax);
randomScale(ip, scale_minmax, scale_minmax);
}
if (method == "ROTATION" & prob_ >= main_prob){
// IJ.log("ROTATE "+rotation_ang );
randomRotate(ip, rotation_ang);
}
if (method == "NOISE" & prob_ >= main_prob){
// IJ.log("NOISE "+noise_factor );
randomNoise(ip, noise_factor);
}
if (method == "GAMMA" & prob_ >= main_prob){
// IJ.log("GAMMA "+gamm_factor );
randomGamma(ip,gamm_factor);
}
if (method == "CONTRAST" & prob_ >= main_prob){
// IJ.log("CONTRAST "+contra_scale );
randomContrast(imp, contra_scale);
}
if (method == "BRIGHTNESS" & prob_ >= main_prob){
// IJ.log("BRIGHTNESS "+bri_scale );
randomBrightness(imp, bri_scale);
}
if (method == "SMOOTH" & prob_ >= main_prob){
randomSmooth(ip);
}
if (method == "SHARPEN" & prob_ >= main_prob){
randomSharpen(ip);
}
if (method == "GDISTORTION" & prob_ >= main_prob){
// IJ.log("GDST "+gdisto_scale );
gBlur(ip,gdisto_scale);
}
IJ.saveAs(OF, FolderPath2 +"\\"+ count);
imp.close();
}
void radomFlip(ImageProcessor ipf){
ipf.flipHorizontal();
// ipf.flipVertical();
}
void randomScale(ImageProcessor ipf, double x, double y){
ipf.scale(x, y);
}
void randomRotate(ImageProcessor ipr, double rotation_ang){
java.util.Random random = new java.util.Random();
double val1 = random.nextInt((int)rotation_ang - 0 + 1) + 0;
ipr.rotate(val1);
}
void randomNoise(ImageProcessor ipf, double y){
java.util.Random random = new java.util.Random();
double val = random.nextInt((int)y - 0 + 1) + 0;
ipf.noise(val);
}
void randomExponential(ImageProcessor ipf){
ipf.exp();
}
void randomGamma(ImageProcessor ipf, double sigma){
java.util.Random random = new java.util.Random();
double val = random.nextInt((int)sigma - 0 + 1) + 0;
ipf.gamma(val);
}
void randomContrast(ImagePlus impf, double contra_scale){
java.util.Random random = new java.util.Random();
double ID = random.nextInt(1 - 0 + 1) + 0;
if (ID == 0.0){
double val = random.nextInt((int)((126*contra_scale)/100) - 0 + 1) + 0;
impf.setDisplayRange(0+val, 255-val);
IJ.run(impf, "Apply LUT", "");
impf.changes = false;
}
if (ID == 1.0){
double val = random.nextInt((int)((800*contra_scale)/100) - 0 + 1) + 0;
impf.setDisplayRange(0-val, 255+val);
IJ.run(impf, "Apply LUT", "");
impf.changes = false;
}
}
void randomBrightness(ImagePlus impf, double bri_scale){
java.util.Random random = new java.util.Random();
double ID = random.nextInt(1 - 0 + 1) + 0;
if (ID == 0.0){
double val = random.nextInt((int)((126*bri_scale)/100) - 0 + 1) + 0;
impf.setDisplayRange(0+val, 255+val);
IJ.run(impf, "Apply LUT", "");
impf.changes = false;
}
if (ID == 1.0){
double val = random.nextInt((int)((125*bri_scale)/100) - 0 + 1) + 0;
impf.setDisplayRange(0-val, 255-val);
IJ.run(impf, "Apply LUT", "");
impf.changes = false;
}
}
void randomSmooth(ImageProcessor ipf){
ipf.smooth();
}
void randomSharpen(ImageProcessor ipf){
ipf.sharpen();
}
void gBlur(ImageProcessor ipf, double sigma){ // sigma value must be between 0 and 100
java.util.Random random = new java.util.Random();
double val = random.nextInt((int)sigma - 0 + 1) + 0;
ipf.blurGaussian(val);
}
void addSalt(ImageProcessor ipf){
/**
* Function ot add Salt and pepper noise to image using basic image operations
* The default noise colors are black and white
* Further improvements and colors will be added in the future versions
* @param int percentage: Percentage of noise to be added
*/
Random randomGenerator = new Random();
int w = ipf.getWidth();
int h = ipf.getHeight();
int infill=0;
int percent =0;
int randomInt = randomGenerator.nextInt(2);
ipf.setColor(bk);
infill = 10 + (int)(Math.random() * ((60 - 10) + 1)); // random percentage between 10 and 90
percent = (int)(infill * (w*h) / 100); // number of pixels to be filled with dots to achieve salt and pepper noise
for (int i =0; i<=percent; i++){
int u = 0 + (int)(Math.random() * ((w - 0) + 1)); // get random x, y coordinates
int v = 0 + (int)(Math.random() * ((h - 0) + 1));
ipf.setColor(wi);
ipf.setLineWidth(1);
ipf.drawDot(u,v);
}
// ImagePlus imsp = new ImagePlus("salt pepper", ipf);
// imsp.updateAndDraw();
IJ.log("Percentage of noise on right:"+infill);
IJ.log("Number of pixels to be altered based on percentage on side1: "+percent);
}
}