-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
1191 lines (1051 loc) · 40.8 KB
/
Copy pathMain.java
File metadata and controls
1191 lines (1051 loc) · 40.8 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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************************
* Copyright (c) 2020 Marcel Austenfeld
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
********************************************************************************/
import static com.eco.bio7.image.ImageMethods.imageFeatureStackToR;
import static com.eco.bio7.rbridge.RServeUtil.evalRScript;
import static com.eco.bio7.rbridge.RServeUtil.listRObjects;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Vector;
import org.apache.commons.io.FileUtils;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import com.eco.bio7.batch.Bio7Dialog;
import com.eco.bio7.batch.FileRoot;
import com.eco.bio7.collection.CustomView;
import com.eco.bio7.collection.Work;
import com.eco.bio7.image.RImageMethodsView;
import com.eco.bio7.image.Util;
import com.eco.bio7.rbridge.RServe;
import com.eco.bio7.rbridge.RState;
import Catalano.Imaging.FastBitmap;
import Catalano.Imaging.Filters.GaborFilter;
import _util.ImageToRTransfer;
import _util.Kuwahara_Filter;
import _util.Lipschitz_;
import boofcv.alg.filter.derivative.DerivativeLaplacian;
import boofcv.alg.filter.derivative.DerivativeType;
import boofcv.alg.filter.derivative.GImageDerivativeOps;
import boofcv.struct.border.BorderType;
import boofcv.struct.image.GrayF32;
import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.WindowManager;
import ij.gui.ImageRoi;
import ij.gui.Overlay;
import ij.gui.Roi;
import ij.gui.RoiListener;
import ij.plugin.ChannelSplitter;
import ij.plugin.Duplicator;
import ij.plugin.ImageCalculator;
import ij.plugin.filter.GaussianBlur;
import ij.plugin.filter.RankFilters;
import ij.plugin.frame.RoiManager;
import ij.process.ColorProcessor;
import ij.process.ColorSpaceConverter;
import ij.process.FloatProcessor;
import ij.process.ImageConverter;
import ij.process.ImageProcessor;
/*
* A Bio7 Graphical User Interface for supervised and unsupervised classification.
*/
public class Main extends com.eco.bio7.compile.Model {
private String[] files;
private _ModelGui gui;
protected int transferType;
protected String currentFilePathMultipleDialog;
protected IProgressMonitor actionMonitor;
private boolean jobDone = true;
private Job previewJob;
protected static String classifyDir;
public Main() {
/*
* Call a method at startup to remove old ROI listeners if you recompile this
* plugin and a listener is still active!
*/
removeRoiListenerAtStartup();
/* Create the GUI interface! */
CustomView view = new CustomView();
Display display = Util.getDisplay();
display.syncExec(() -> {
Composite parent = view.getComposite("Classification");
/*
* Create the GUI and transfer a reference to this class that the GUI can
* execute methods from this class!
*/
gui = new _ModelGui(parent, Main.this, SWT.NONE);
parent.layout(true);
});
}
/* Called from the GUI class! */
public void executeSelection(int choice) {
Job job = new Job("Classification Process") {
@Override
protected IStatus run(IProgressMonitor monitor) {
actionMonitor = monitor;
monitor.beginTask("Started selected action ...", IProgressMonitor.UNKNOWN);
/* We create a feature stack. R connection not necessary! */
if (choice == 2) {
action(choice, monitor);
}
else {
if (RServe.isAliveDialog()) {
if (RState.isBusy() == false) {
/* Notify that R is busy! */
RState.setBusy(true);
action(choice, monitor);
} else {
System.out.println("RServer is busy. Can't execute the R script!");
}
}
}
monitor.done();
return Status.OK_STATUS;
}
};
job.addJobChangeListener(new JobChangeAdapter() {
public void done(IJobChangeEvent event) {
if (event.getResult().isOK()) {
RState.setBusy(false);
/* Update the R-Shell view workspace objects! */
if (RServe.isAlive()) {
listRObjects();
}
} else {
RState.setBusy(false);
}
}
});
job.schedule();
}
public void action(int choice, IProgressMonitor monitor) {
/*
* Important call to get the features and feature settings from the GUI
* (syncExec wrapped for SWT)!
*/
gui.getFeatureOptions();
/* Create feature stack! */
if (choice == 1) {
// String files = Bio7Dialog.openFile();
// if (files != null) {
ImagePlus image = WindowManager.getCurrentImage();
if (image == null) {
Bio7Dialog.message("Please open an image in ImageJ!");
return;
}
/*
* Here we add a dialog to check if really only the ROI Manager selections
* should be transferred! It happens that a selection is present in the ROI
* Manager but it was intended to transfer all ROI's. Here a little workflow
* help!
*/
RoiManager mInstance = RoiManager.getInstance();
if (mInstance == null) {
/* If ROI Manager isn't active! */
Bio7Dialog.message("Please open and add selections to the ROI Manager!");
return;
}
Roi[] r = mInstance.getSelectedRoisAsArray();
if (r.length < 1) {
Bio7Dialog.message("NO ROI's available in ROI Manager!");
return;
}
Roi[] r2 = mInstance.getRoisAsArray();
if (r.length < r2.length) {
boolean all = Bio7Dialog.decision("Selections in ROI Manager exists!\n"
+ "Only the selected ROI's in the ROI Manager will be transferred!\n\n"
+ "Press 'Yes' if you want to transfer the selected ROI's only!\n"
+ "Press 'No' to transfer all ROI's in the ROI Manager!");
if (all == false) {
mInstance.deselect();
}
}
ImagePlus imPlus = createStackFeatures(null, image, monitor);
if (gui.openStack) {
imPlus.show();
gui.layout();
}
monitor.setTaskName("Transfer Feature data to R");
int typeTransfer = gui.transferType;
if (gui.useGroups) {
/* Special method to transfer ROI Manager ROI's with a signature to R! */
ImageToRTransfer.imageFeatureStackSelectionToR(imPlus, typeTransfer, 1, imPlus.getStackSize(), true);
} else {
ImageToRTransfer.imageFeatureStackSelectionToR(imPlus, typeTransfer, 1, imPlus.getStackSize(), false);
}
// }
}
/* Create ROI Classes! */
else if (choice == 2) {
ImagePlus image = WindowManager.getCurrentImage();
if (image == null) {
Bio7Dialog.message("Please open an image in ImageJ to create feature selections!");
return;
}
Bio7Dialog.message("Add selections (ROI's = specific class) to the ROI Manager.\n\n"
+ "To set a class signature:\n\n" + "Option 1 (default):\n"
+ "Rename the ROI's to identify the classes - at the end of the string use an underscore followed by the class"
+ "number (class_1, class_2 or cell_1, cell_2..., etc.).\n\n" + "Option 2:\n"
+ "Enable the 'Use Group Signature' option in the 'Settings tab' to set signatures according "
+ "to the group membership (set ROI Groups, e.g., with the ImageJ toolbar action)!");
/* Opens the ROI Manager of ImageJ! */
IJ.run("ROI Manager...", "");
}
/* Train Classifier with external script! */
else if (choice == 3) {
/*
* We have set the busy variable to false to use the R-Shell selection in this
* job!
*/
RState.setBusy(false);
Work.openView("com.eco.bio7.RShell");
Bio7Dialog.selection("Select training features (classes) in R-Shell!\n\n"
+ "Select multiple with STRG (CMD)+MouseClick or SHIFT+MouseClick!\n\nPress 'OK' when selected to execute the training R script!");
/* Set the busy variable again to true because now we call R! */
RState.setBusy(true);
monitor.setTaskName("Apply Training Script");
String path = gui.getPathTrainingScript();
/* Execute R script! */
if (path.endsWith(".R")) {
System.out.println("");
evalRScript(path);
}
}
/* Classify selected images with external Script! */
else if (choice == 4) {
gui.interruptBatch = false;
if (gui.useDirectoryDialog) {
String dirSelection = directory("Select the base directory");
if (dirSelection == null) {
return;
}
File dir = new File(dirSelection);
/* If you need only certain image types! */
// final String[] ext = { "tif", "tiff", "dcm", "png" };
List<File> files = (List<File>) FileUtils.listFiles(dir, null, true);
// Only directories:
// FileUtils.listFilesAndDirs(new File(dir), new
// NotFileFilter(TrueFileFilter.INSTANCE), DirectoryFileFilter.DIRECTORY)
for (int i = 0; i < files.size(); i++) {
if (gui.interruptBatch) {
break;
}
File file = files.get(i);
String currentFile = null;
try {
currentFile = file.getCanonicalPath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ImagePlus imPlus = createStackFeatures(currentFile, null, monitor);
// System.out.println(choice);
classify(monitor, imPlus);
}
} else {
String[] files = openMultipleFiles();
if (files == null) {
return;
}
for (int i = 0; i < files.length; i++) {
if (gui.interruptBatch) {
break;
}
ImagePlus imPlus = createStackFeatures(files[i], null, monitor);
classify(monitor, imPlus);
}
}
}
}
private void classify(IProgressMonitor monitor, ImagePlus imPlus) {
/* Correct some image names for R! */
String name = imPlus.getTitle();
String nameCorrected = "current_feature_stack";
/* Set the data type for the R transfer from the Bio7 GUI! */
int transferType = getRDataTransferType();
/* Transfer the feature stack to R in the selected datatype! */
imageFeatureStackToR(nameCorrected, transferType, imPlus);
imPlus = null;
monitor.setTaskName("Apply Classification Script");
/* Predict in R (evalRScript is a custom method) with the randomForest model! */
String path = gui.getPathClassificationScript();
if (path.endsWith(".R")) {
evalRScript(path);
// imageFromR(3, "imageMatrix", 1);
if (gui.showClassifiedInImageJ) {
ImagePlus imageClassified = ImageToRTransfer.imageFromR("imageMatrix");
if (imageClassified != null) {
imageClassified.setTitle(name + "_Classified");
imageClassified.show();
gui.layout();
}
}
}
if (gui.applyPostImageJMacro) {
String macroPath = gui.getPathImageJMacroPostScript();
IJ.runMacroFile(macroPath);
}
}
public void classifyPreview(IProgressMonitor monitor, ImagePlus imPlus, Roi roi) {
/* Correction of name here not necessary! */
// String name = imPlus.getTitle();
String nameCorrected = "current_feature_stack";
/* Set the data type for the R transfer from the Bio7 GUI! */
int transferType = getRDataTransferType();
/* Transfer the feature stack to R in the selected datatype! */
imageFeatureStackToR(nameCorrected, transferType, imPlus);
imPlus = null;
if (monitor != null) {
monitor.setTaskName("Apply Classification Preview");
}
/* Predict in R (evalRScript is a custom method) with the randomForest model! */
String path = gui.getPathClassificationScript();
if (path.endsWith(".R")) {
evalRScript(path);
if (roi != null) {
Rectangle bounds = roi.getBounds();
int x = bounds.x;
int y = bounds.y;
ImagePlus impOverlay = ImageToRTransfer.imageFromR("imageMatrix");
if (impOverlay != null) {
/* Apply the LUT option! */
IJ.run(impOverlay, gui.lutOption, "");
ImageRoi imageRoi = new ImageRoi(x, y, impOverlay.getProcessor());
/* Parse and apply the opacity option! */
imageRoi.setOpacity(Double.parseDouble(gui.opacityOption));
// imageRoi.setZeroTransparent(true);
Overlay overlay = new Overlay(imageRoi);
ImagePlus iDisplayed = WindowManager.getCurrentImage();
iDisplayed.setOverlay(overlay);
// Font font = new Font("Arial", Font.PLAIN, 18);
// TextRoi roiText = new TextRoi(x, y, "Preview", font);
// roiText.setStrokeColor(new Color(1, 0, 0));
// overlay.add(roiText);
// impOverlay.show();
// iDisplayed.setRoi(imageRoi);
}
}
}
}
public ImagePlus createStackFeatures(String files, ImagePlus sourceImage, IProgressMonitor monitor) {
ImagePlus imPlus = null;
ImagePlus image = null;
ImageStack stack = null;
/*
* If we want to use an import macro, e.g., using the BioFormats library
* commands which can be recorded with the ImageJ macro recorder!
*/
if (gui.useImportMacro) {
/* Multiple files selected! */
if (files != null) {
/* If we used the directory dialog to open all files! */
if (gui.useDirectoryDialog) {
IJ.runMacroFile(gui.getMacroTextOption(), files);
} else {
/*
* currentFilePathMultipleDialog is the path to the directory which we need for
* the multiple files dialog which returns only the filenames!
*/
IJ.runMacroFile(gui.getMacroTextOption(), currentFilePathMultipleDialog + "/" + files);
}
}
// else {
/* Call ImageJ macro with option (file path)! */
// IJ.runMacroFile(gui.getMacroTextOption(), singleFile);
// }
} else {
/* Multiple files selected! */
if (files != null) {
/* If we used the directory dialog to open all files! */
if (gui.useDirectoryDialog) {
image = IJ.openImage(files);
} else {
/*
* currentFilePathMultipleDialog is the path to the directory which we need for
* the multiple files dialog which returns only the filenames!
*/
image = IJ.openImage(currentFilePathMultipleDialog + "/" + files);
}
} else {
// if (singleFile != null) {
// image = IJ.openImage(singleFile);
// } else {
image = sourceImage;
// }
}
}
/*
* We must avoid a null reference when using the BioFormats library with the
* macro import option!
*/
if (gui.useImportMacro && image == null) {
image = WindowManager.getCurrentImage();
}
/* If we have a RGB! */
if (image.getProcessor() instanceof ColorProcessor) {
/* Convert to HSB! */
if (gui.toHsb) {
if (monitor != null) {
monitor.setTaskName("Convert RGB To HSB Color Space");
}
/* Duplicate the image! */
Duplicator duplicator = new Duplicator();
/* Duplicate original for the HSB channels! */
ImagePlus impToHasb = duplicator.run(image);
ImageConverter con = new ImageConverter(impToHasb);
con.convertToHSB32();
String opt = gui.channelOption;
String[] channelToInclude = opt.split(",");
ImageStack hsbStack = impToHasb.getStack();
if (opt.isEmpty() == false && channelToInclude.length > 0) {
/* Create a feature stack from selected HSB channels! */
stack = new ImageStack(impToHasb.getWidth(), impToHasb.getHeight());
for (int j = 0; j < channelToInclude.length; j++) {
/* Add selected HSB float channels to the stack! */
int sel = Integer.parseInt(channelToInclude[j]);
/* Use selected slices! */
ImageProcessor floatProcessor = hsbStack.getProcessor(sel);
stack.addSlice("Channel_" + j, floatProcessor);
}
} else {
/* Use all slices. Already Float! */
stack = impToHasb.getStack();
}
}
/* Convert to LAB! */
else if (gui.toLab) {
if (monitor != null) {
monitor.setTaskName("Convert RGB To LAB Color Space");
}
/* Duplicate the image! */
Duplicator duplicator = new Duplicator();
/* Duplicate original for the LAB channels! */
ImagePlus impToLab = duplicator.run(image);
ColorSpaceConverter converter = new ColorSpaceConverter();
ImagePlus imp = converter.RGBToLab(impToLab);
// imp.show();
// image.hide();
imp.copyAttributes(impToLab);
// image.changes = false;
// image.close();
String opt = gui.channelOption;
String[] channelToInclude = opt.split(",");
ImageStack labStack = imp.getStack();
if (opt.isEmpty() == false && channelToInclude.length > 0) {
/* Create a LAB stack from selected channels! */
stack = new ImageStack(imp.getWidth(), imp.getHeight());
for (int j = 0; j < channelToInclude.length; j++) {
/* Add LAB channels to the stack. LAB stack are already float images! */
int sel = Integer.parseInt(channelToInclude[j]);
/* Use selected slices! */
ImageProcessor floatProcessor = labStack.getProcessor(sel);
stack.addSlice("Channel_" + j, floatProcessor);
}
} else {
/* Use all slices. LAB stack are already float images! */
stack = imp.getStack();
}
}
else {
/* Split original to R,G,B channels! */
ImagePlus[] channels = ChannelSplitter.split(image);
String opt = gui.channelOption;
String[] channelToInclude = opt.split(",");
/* Create a feature stack from all available channels (e.g., R,G,B) images! */
stack = new ImageStack(image.getWidth(), image.getHeight());
if (opt.isEmpty() == false && channelToInclude.length > 0) {
for (int j = 0; j < channelToInclude.length; j++) {
/* Add selected RGB channels to the new stack! */
/* Convert original to float to have a float image stack for the filters! */
int sel = Integer.parseInt(channelToInclude[j]) - 1;// Channels index starts with 0 so we
// correct here with -1!
ImageProcessor floatProcessor = channels[sel].getProcessor().convertToFloat();
stack.addSlice("Channel_" + j, floatProcessor);
}
} else {
for (int j = 0; j < channels.length; j++) {
/* Add RGB channels to the stack! */
/* Convert original to float to have a float image stack for the filters! */
ImageProcessor floatProcessor = channels[j].getProcessor().convertToFloat();
stack.addSlice("Channel_" + j, floatProcessor);
}
}
}
} else {/* Grayscale images (8-bit, 16-bit, 32-bit) */
/* If we have a grayscale stack! */
if (image.getStackSize() > 1) {
String opt = gui.channelOption;
String[] channelToInclude = opt.split(",");
/* Check if we only want to include certain slices! */
if (opt.isEmpty() == false && channelToInclude.length > 0) {
stack = new ImageStack(image.getWidth(), image.getHeight());
for (int j = 0; j < channelToInclude.length; j++) {
/* Add selected slices to a new stack! */
int sel = Integer.parseInt(channelToInclude[j]);// Stack starts with index 1 no correction
// necessary!
stack.addSlice("Grayscale_Layer_" + j, image.getStack().getProcessor(sel).convertToFloat());
}
} else {
/* Convert original to float to have a float image stack for the filters! */
stack = image.getStack().convertToFloat();
}
} else {
stack = new ImageStack(image.getWidth(), image.getHeight());
/* Convert original to float to have a float image stack for the filters! */
stack.addSlice("Grayscale", image.getProcessor().convertToFloat());
}
}
/*
* Duplicate the base stack as basis for the different filters! the filtered
* images will be added to the base stack!
*/
ImageStack tempStack = stack.duplicate();
/*
* See:
* https://imagej.nih.gov/ij/developer/api/ij/plugin/filter/GaussianBlur.html#
* blur-ij.process.ImageProcessor-double-
*/
if (gui.gaussian) {
if (monitor != null) {
monitor.setTaskName("Apply Gaussian Filter");
}
GaussianBlur gaussian = new GaussianBlur();
/* Split the gaussian option to get all sigmas! */
String[] gaussianSigma = gui.gaussianOption.split(",");
int stackSize = tempStack.getSize();
for (int i = 1; i <= stackSize; i++) {
for (int j = 0; j < gaussianSigma.length; j++) {
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
double sigma = Double.parseDouble(gaussianSigma[j]);
gaussian.blurGaussian(ip, 0.4 * sigma, 0.4 * sigma, 0.0002);
stack.addSlice("Gaussian_" + "Sigma_" + sigma + "Layer_" + i, ip);
}
}
}
if (gui.diffOfGaussian) {
if (monitor != null) {
monitor.setTaskName("Apply Difference of Gaussian Filters");
}
GaussianBlur gaussian = new GaussianBlur();
int stackSize = tempStack.getSize();
for (int i = 1; i <= stackSize; i++) {
/* See if we have several Gaussian Difference filter settings! */
String[] gaussianDiffOptionSet = gui.diffGaussianOption.split(";");
for (int j = 0; j < gaussianDiffOptionSet.length; j++) {
String opGaussianDiff = gaussianDiffOptionSet[j];
/* Split the gaussian option to get all sigmas! */
String[] gaussianSigma = opGaussianDiff.split(",");
/* Here we have two sigmas to calculate the difference! */
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
double sigma1 = Double.parseDouble(gaussianSigma[0]);
gaussian.blurGaussian(ip, sigma1, sigma1, 0.0002);
ImageProcessor ip2 = tempStack.getProcessor(i).duplicate();
double sigma2 = Double.parseDouble(gaussianSigma[1]);
gaussian.blurGaussian(ip2, sigma2, sigma2, 0.0002);
ImageCalculator ic = new ImageCalculator();
ImagePlus finalDiffGaussian = ic.run("Subtract create 32-bit", new ImagePlus("sigma1", ip),
new ImagePlus("sigma2", ip2));
stack.addSlice("DiffOfGaussian_Set_" + j + "_Layer" + i, finalDiffGaussian.getProcessor());
}
}
}
if (gui.median) {
if (monitor != null) {
monitor.setTaskName("Apply Median Filter");
}
/* Split the median option to get all radii! */
String[] medianRadius = gui.medianOption.split(",");
int stackSize = tempStack.getSize();
for (int i = 1; i <= stackSize; i++) {
for (int j = 0; j < medianRadius.length; j++) {
double radius = Double.parseDouble(medianRadius[j]);
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
RankFilters ran = new RankFilters();
extracted(radius, ran, ip, RankFilters.MEDIAN);
stack.addSlice("Median_" + "Radius_" + radius + "Layer_" + i, ip);
}
}
}
if (gui.mean) {
if (monitor != null) {
monitor.setTaskName("Apply Mean Filter");
}
/* Split the mean option to get all radii! */
String[] meanRadius = gui.meanOption.split(",");
int stackSize = tempStack.getSize();
for (int i = 1; i <= stackSize; i++) {
for (int j = 0; j < meanRadius.length; j++) {
double radius = Double.parseDouble(meanRadius[j]);
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
RankFilters ran = new RankFilters();
ran.rank(ip, radius, RankFilters.MEAN);
stack.addSlice("Mean_" + "Radius_" + radius + "Layer_" + i, ip);
}
}
}
if (gui.variance) {
if (monitor != null) {
monitor.setTaskName("Apply Variance Filter");
}
/* Split the mean option to get all radii! */
String[] varianceSigma = gui.varianceOption.split(",");
int stackSize = tempStack.getSize();
for (int i = 1; i <= stackSize; i++) {
for (int j = 0; j < varianceSigma.length; j++) {
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
double radius = Double.parseDouble(varianceSigma[j]);
RankFilters ran = new RankFilters();
extracted(radius, ran, ip, RankFilters.VARIANCE);
stack.addSlice("Variance_" + "Radius_" + radius + "Layer_" + i, ip);
}
}
}
if (gui.maximum) {
if (monitor != null) {
monitor.setTaskName("Apply Maximum Filter");
}
/* Split the mean option to get all radii! */
String[] maximumSigma = gui.maximumOption.split(",");
int stackSize = tempStack.getSize();
for (int i = 1; i <= stackSize; i++) {
for (int j = 0; j < maximumSigma.length; j++) {
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
double radius = Double.parseDouble(maximumSigma[j]);
RankFilters ran = new RankFilters();
extracted(radius, ran, ip, RankFilters.MAX);
stack.addSlice("Maximum_" + "Radius_" + radius + "Layer_" + i, ip);
}
}
}
if (gui.minimum) {
if (monitor != null) {
monitor.setTaskName("Apply Minimum Filter");
}
/* Split the mean option to get all radii! */
String[] minimumSigma = gui.minimumOption.split(",");
int stackSize = tempStack.getSize();
for (int i = 1; i <= stackSize; i++) {
for (int j = 0; j < minimumSigma.length; j++) {
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
double radius = Double.parseDouble(minimumSigma[j]);
RankFilters ran = new RankFilters();
extracted(radius, ran, ip, RankFilters.MIN);
// ran.rank(ip, Double.parseDouble(minimumSigma[j]), RankFilters.MIN);
stack.addSlice("Minimum_" + "Radius_" + radius + "Layer_" + i, ip);
}
}
}
if (gui.gradientHessian) {
if (monitor != null) {
monitor.setTaskName("Apply Gradient, Hessian Derivative");
}
/* */
GaussianBlur gaussian = new GaussianBlur();
int stackSize = tempStack.getSize();
for (int i = 1; i <= stackSize; i++) {
String[] gradientHessian = gui.gradientHessianOption.split(",");
/* Apply a Gaussian blur if we have double arguments! */
if (gradientHessian[0].isEmpty() == false) {
for (int j = 0; j < gradientHessian.length; j++) {
double sigma = Double.parseDouble(gradientHessian[j]);
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
gaussian.blurGaussian(ip, 0.4 * sigma, 0.4 * sigma, 0.0002);
gradient(stack, ip);
}
} else {
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
gradient(stack, ip);
}
}
}
if (gui.laplacian) {
if (monitor != null) {
monitor.setTaskName("Apply Laplacian Derivative");
}
int stackSize = tempStack.getSize();
GaussianBlur gaussian = new GaussianBlur();
for (int i = 1; i <= stackSize; i++) {
String[] laplacianSigma = gui.laplacianOption.split(",");
/* Apply a Gaussian blur if we have double arguments! */
if (laplacianSigma[0].isEmpty() == false) {
for (int j = 0; j < laplacianSigma.length; j++) {
double sigma = Double.parseDouble(laplacianSigma[j]);
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
gaussian.blurGaussian(ip, 0.4 * sigma, 0.4 * sigma, 0.0002);
int width = ip.getWidth();
int height = ip.getHeight();
GrayF32 boofFilterImageInput = new GrayF32(width, height);
GrayF32 boofFilterImageOutput = new GrayF32(width, height);
/* Transfer ImageProcessor data in place to boofcv image input! */
ipToBoofCVGray32(ip, boofFilterImageInput);
DerivativeLaplacian.process(boofFilterImageInput, boofFilterImageOutput, null);
FloatProcessor flProcessor = new FloatProcessor(width, height, boofFilterImageOutput.getData());
stack.addSlice("Laplacian_Derivative_From_Gaussian_" + sigma + "_Layer" + i, flProcessor);
}
} else {
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
int width = ip.getWidth();
int height = ip.getHeight();
GrayF32 boofFilterImageInput = new GrayF32(width, height);
GrayF32 boofFilterImageOutput = new GrayF32(width, height);
/* Transfer ImageProcessor data in place to boofcv image input! */
ipToBoofCVGray32(ip, boofFilterImageInput);
DerivativeLaplacian.process(boofFilterImageInput, boofFilterImageOutput, null);
FloatProcessor flProcessor = new FloatProcessor(width, height, boofFilterImageOutput.getData());
stack.addSlice("Laplacian Derivative_" + "Layer_" + i, flProcessor);
}
}
}
if (gui.edges) {
// see:
// https://imagejdocu.tudor.lu/faq/technical/what_is_the_algorithm_used_in_find_edges
if (monitor != null) {
monitor.setTaskName("Apply Edges");
}
int stackSize = tempStack.getSize();
GaussianBlur gaussian = new GaussianBlur();
for (int i = 1; i <= stackSize; i++) {
String[] edgesSigma = gui.edgesOption.split(",");
/* Apply a Gaussian blur if we have double arguments! */
if (edgesSigma[0].isEmpty() == false) {
for (int j = 0; j < edgesSigma.length; j++) {
double sigma = Double.parseDouble(edgesSigma[j]);
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
gaussian.blurGaussian(ip, 0.4 * sigma, 0.4 * sigma, 0.0002);
IJ.run(new ImagePlus("Edges_layer" + i + "_temp", ip), "Find Edges", "stack");
stack.addSlice("Edges_Layer_From_Gaussian_" + j + "_Layer_" + i, ip);
}
} else {
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
IJ.run(new ImagePlus("Edges_layer" + i + "_temp", ip), "Find Edges", "stack");
stack.addSlice("Edges_" + "Layer_" + i, ip);
}
}
}
if (gui.lipschitz) {
if (monitor != null) {
monitor.setTaskName("Apply Lipschitz Filter");
}
int stackSize = tempStack.getSize();
for (int i = 1; i <= stackSize; i++) {
/* See if we have several Lipschitz filter settings! */
String[] libschitzOptionsSet = gui.lipschitzOption.split(";");
for (int j = 0; j < libschitzOptionsSet.length; j++) {
ImageProcessor ip = tempStack.getProcessor(i).duplicate().convertToByte(true);
String opLipschitz = libschitzOptionsSet[j];
/* Split the Lipschitz set for one filter! */
String[] lipschitzOptions = opLipschitz.split(",");
Lipschitz_ filter = new Lipschitz_();
Lipschitz_.setDownHatFilter(Boolean.parseBoolean(lipschitzOptions[0]));
Lipschitz_.setTopHatFilter(Boolean.parseBoolean(lipschitzOptions[1]));
Lipschitz_.setSlopeFilter(Double.parseDouble(lipschitzOptions[2]));
filter.Lipschitz2D(ip);
stack.addSlice("Lipschitz_Set_" + j + "_Layer_" + i, ip.convertToFloat());
}
}
}
if (gui.gabor) {
if (monitor != null) {
monitor.setTaskName("Apply Gabor Filter");
}
int stackSize = tempStack.getSize();
for (int i = 1; i <= stackSize; i++) {
/* See if we have several Gabor filter settings! */
String[] gaborOptionsSet = gui.gaborOption.split(";");
for (int j = 0; j < gaborOptionsSet.length; j++) {
String opGabor = gaborOptionsSet[j];
/* Split the Gabor set for one filter! */
String[] gaborOptions = opGabor.split(",");
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
/* Will work with 8-bit only! */
BufferedImage buff = new ImagePlus("tempGabor", ip).getBufferedImage();
FastBitmap fb = new FastBitmap(buff);
GaborFilter gabor = new GaborFilter();
/* Extract the arguments for one filter for the different layers! */
gabor.setSize(Integer.parseInt(gaborOptions[0]));
gabor.setWavelength(Double.parseDouble(gaborOptions[1]));
gabor.setOrientation(Double.parseDouble(gaborOptions[2]));
gabor.setPhaseOffset(Double.parseDouble(gaborOptions[3]));
gabor.setGaussianVar(Double.parseDouble(gaborOptions[4]));
gabor.setAspectRatio(Double.parseDouble(gaborOptions[5]));
gabor.applyInPlace(fb);
float[] imArray = fb.toArrayGrayAsFloat();
int width = ip.getWidth();
int height = ip.getHeight();
stack.addSlice("Gabor_Set_" + j + "_Layer_" + i, new FloatProcessor(width, height, imArray));
// new Gabor_Filter(ip,stack);
}
}
}
if (gui.topHat) {
if (monitor != null) {
monitor.setTaskName("Apply Top Hat Filter");
}
/* Split the mean option to get all radii! */
String[] topHatRadius = gui.topHatOption.split(",");
int stackSize = tempStack.getSize();
for (int i = 1; i <= stackSize; i++) {
for (int j = 0; j < topHatRadius.length; j++) {
double radius = Double.parseDouble(topHatRadius[j]);
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
RankFilters ran = new RankFilters();
ran.rank(ip, radius, RankFilters.TOP_HAT);
stack.addSlice("Top_Hat_" + radius + "_Layer_" + i, ip);
}
}
}
if (gui.kuwahara) {
if (monitor != null) {
monitor.setTaskName("Apply Kuwahara Filter");
}
int stackSize = tempStack.getSize();
for (int i = 1; i <= stackSize; i++) {
/* Split the mean option to get all radii! */
String[] kuwaharaOptions = gui.kuwaharaOption.split(",");
for (int j = 0; j < kuwaharaOptions.length; j++) {
int radius = Integer.parseInt(kuwaharaOptions[j]);
ImageProcessor ip = tempStack.getProcessor(i).duplicate();
/* Will work with 8-bit only! */
Kuwahara_Filter kuw = new Kuwahara_Filter();
Kuwahara_Filter.size = radius;
kuw.filter(ip);
stack.addSlice("Kuwahara_" + radius + "_Layer_" + i, ip);
}
}
}
if (gui.convolve) {
if (monitor != null) {
monitor.setTaskName("Apply Convolve");
}
String[] matrices = gui.convolveOption.split(";");
for (int i = 0; i < matrices.length; i++) {
int stackSize = tempStack.getSize();
for (int u = 1; u <= stackSize; u++) {
ImageProcessor ip = tempStack.getProcessor(u).duplicate();
IJ.run(new ImagePlus("Convolved_" + i + "_layer" + u + "_temp", ip), "Convolve...", matrices[i]);
stack.addSlice("Convolved_" + i + "_Layer_" + u, ip);
}
}
}
String name = image.getShortTitle();
imPlus = new ImagePlus(name, stack);
image = null;
stack = null;
tempStack = null;
return imPlus;
}
/* Method to calculate the gradient! */
private void gradient(ImageStack stack, ImageProcessor ip) {
int width = ip.getWidth();
int height = ip.getHeight();
GrayF32 boofFilterImageInput = new GrayF32(width, height);
/* Transfer ImageProcessor data in place to boofcv image input! */
ipToBoofCVGray32(ip, boofFilterImageInput);
// First order derivative, also known as the gradient
GrayF32 derivX = new GrayF32(boofFilterImageInput.width, boofFilterImageInput.height);
GrayF32 derivY = new GrayF32(boofFilterImageInput.width, boofFilterImageInput.height);
GImageDerivativeOps.gradient(DerivativeType.SOBEL, boofFilterImageInput, derivX, derivY, BorderType.EXTENDED);
// Second order derivative, also known as the Hessian
// GrayF32 derivXX = new GrayF32(boofFilterImageInput.width,
// boofFilterImageInput.height);
// GrayF32 derivXY = new GrayF32(boofFilterImageInput.width,
// boofFilterImageInput.height);
// GrayF32 derivYY = new GrayF32(boofFilterImageInput.width,
// boofFilterImageInput.height);
// GImageDerivativeOps.hessian(DerivativeType.SOBEL, derivX, derivY, derivXX,
// derivXY, derivYY,
// BorderType.EXTENDED);
FloatProcessor flxProcessor = new FloatProcessor(width, height, derivX.getData());
FloatProcessor flyProcessor = new FloatProcessor(width, height, derivY.getData());
// FloatProcessor flxxProcessor = new FloatProcessor(width, height,
// derivXX.getData());
// FloatProcessor flxyProcessor = new FloatProcessor(width, height,
// derivXY.getData());
// FloatProcessor flyyProcessor = new FloatProcessor(width, height,
// derivYY.getData());
stack.addSlice("Gradient_Sobel X", flxProcessor);
stack.addSlice("Gradient_Sobel Y", flyProcessor);
// stack.addSlice("Hessian_Sobel XX", flxxProcessor);
// stack.addSlice("Hessian_Sobel XY", flxyProcessor);
// stack.addSlice("Hessian_Sobel YY", flyyProcessor);
}