-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiofiter_morph.java
More file actions
1813 lines (1727 loc) · 58.4 KB
/
Copy pathBiofiter_morph.java
File metadata and controls
1813 lines (1727 loc) · 58.4 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
/*
* Biofiter_morph software v1.0 for morphometry analysis
* Copyright (C) 2019 Carlos Alquézar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Dimension;
import java.awt.event.*;
import java.awt.Font;
import java.awt.geom.Line2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.lang.Math;
import javax.swing.ButtonGroup;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Arrays;
import java.io.*;
import ij.*;
import ij.gui.*;
import ij.io.*;
import ij.process.*;
import ij.process.AutoThresholder;
//import ij.process.AutoThresholder.Method;
import ij.process.ImageConverter;
import ij.process.ImageStatistics;
import ij.plugin.*;
import ij.plugin.filter.Analyzer;
import ij.plugin.filter.GaussianBlur;
import ij.plugin.filter.ParticleAnalyzer;
//import ij.plugin.filter.PlugInFilter;
import ij.measure.*;
import java.lang.Math;
import java.lang.Integer;
/**
* Computer Assisted Sperm Analysis Plugin, based on mTrack2r, CASA and wrMTrck
* @author calquezar (Carlos Alquézar Baeta) at University of Zaragoza - <a href="mailto:carlos.alquezar.baeta@gmail.com">carlos.alquezar.baeta@gmail.com</a>
*/
public class Biofiter_morph implements PlugIn,Measurements,ChangeListener,MouseListener {
private double pixelWidth=1.0, pixelHeight=1.0;
public static double pixelsPerUm = 12.33;
public static float minDistance = 10; //um
//GENERIC DIALOG PARAMETERS
static float minSize = 20;//minimum sperm size (um^2)
static float maxSize = 50;//maximum sperm size (um^2)
String male = "";
String date = "";
ImagePlus imp = new ImagePlus();
//Here we'll store the original images
ImagePlus completeImpOrig = null;
ImagePlus acrosomeImpOrig = null;
ImagePlus nucleusAImpOrig = null;
ImagePlus nucleusDImpOrig = null;
//These ImagePlus will be used to draw over them
ImagePlus completeImpDraw = null;
ImagePlus acrosomeImpDraw = null;
ImagePlus nucleusAImpDraw = null;
ImagePlus nucleusDImpDraw = null;
//These ImagePlus will be used to calculate mean gray values
ImagePlus completeImpGray = null;
ImagePlus acrosomeImpGray = null;
ImagePlus nucleusAImpGray = null;
ImagePlus nucleusDImpGray = null;
//These ImagePlus will be used to identify spermatozoa
ImagePlus completeImpTh = null;
ImagePlus acrosomeImpTh = null;
ImagePlus nucleusAImpTh = null;
ImagePlus nucleusDImpTh = null;
//These ImagePlus will be used to store outlines
ImagePlus completeImpOutline = null;
ImagePlus acrosomeImpOutline = null;
ImagePlus nucleusAImpOutline = null;
ImagePlus nucleusDImpOutline = null;
//Thresholds
double completeThreshold = -1.0;
double acrosomeThreshold = -1.0;
double nucleusAThreshold = -1.0;
double nucleusDThreshold = -1.0;
String completeThresholdMethod = "Otsu";
String acrosomeThresholdMethod = "Otsu";
String nucleusAThresholdMethod = "Otsu";
String nucleusDThresholdMethod = "Otsu";
boolean isThresholding = false;
//GUI elements and variables
int activeImage = 1; // 1-Complete;2-Acrosome;3-NucleusA;4-NucleusD
JSlider sldThreshold;
boolean hasCanvas = false;
ImageCanvas canvas;
boolean hideSliderEvent = false;//Used to make transparent Slider event
JLabel JLPencilUsed; //Used to identify manually subtypes
//Variables used to identify spermatozoa
List completeSpermatozoa = new ArrayList();
List acrosomeSpermatozoa = new ArrayList();
List nucleusASpermatozoa = new ArrayList();
List nucleusDSpermatozoa = new ArrayList();
//Variables used to show types
boolean showAll = true;
boolean showMNAN = false;
boolean showMDAN = false;
boolean showMNAD = false;
boolean showMDAD = false;
boolean showGreen = false;
boolean showUnknown = false;
//Results table
ResultsTable morphometrics = new ResultsTable();
/******************************************************/
/**
*
*/
public class particle {
String id="***";
float x;
float y;
int z;
boolean inTrack=false;//Used to identify different parts of the same spermatozoon
boolean flag=false;//Inherit from CASA_
//Boundary data
float bx;
float by;
float width;
float height;
//Selection variables
boolean selected = false;
//Color type
String type="Unknown";
//Morphometrics
//total
float total_area=-1;
float total_perimeter=-1;
float total_feret=-1;
float total_minFeret=-1;
//Acrosome
float acrosome_area=-1;
float acrosome_perimeter=-1;
//Nucleus
float nucleus_area=-1;
float nucleus_perimeter=-1;
float nucleus_feret=-1;
float nucleus_minFeret=-1;
public void copy(particle source) {
this.id=source.id;
this.x=source.x;
this.y=source.y;
this.z=source.z;
this.inTrack=source.inTrack;
this.flag=source.flag;
this.bx=source.bx;
this.by=source.by;
this.width=source.width;
this.height=source.height;
this.selected=source.selected;
this.type=source.type;
this.total_area=source.total_area;
this.total_perimeter=source.total_perimeter;
this.total_feret=source.total_feret;
this.total_minFeret=source.total_minFeret;
this.acrosome_area=source.acrosome_area;
this.acrosome_perimeter=source.acrosome_perimeter;
this.nucleus_area=source.nucleus_area;
this.nucleus_perimeter=source.nucleus_perimeter;
this.nucleus_feret=source.nucleus_feret;
this.nucleus_minFeret=source.nucleus_minFeret;
}
public float distance (particle p) {
return (float) Math.sqrt(sqr(this.x-p.x) + sqr(this.y-p.y));
}
}
/******************************************************/
/**
* @param arg String
*/
public void run(String arg) {
//the stuff below is the box that pops up to ask for pertinant values - why doesn't it remember the values entered????
GenericDialog gd = new GenericDialog("Sperm Analyzer");
gd.addMessage("PARAMETERS USED TO CALCULATE MORPHOMETRICS");
gd.addNumericField("Scale (pixels per um):", (float)pixelsPerUm,2);
gd.addMessage("____________________________________________");
gd.addMessage("PARAMETERS USED TO DETECT PARTICLES:");
gd.addNumericField("Minimum sperm size (um^2):", minSize,2);
gd.addNumericField("Maximum sperm size (um^2):", maxSize,2);
gd.addNumericField("Minimum distance between head and acrosome (um):", minDistance,2);
gd.addMessage("____________________________________________");
gd.addMessage("PARAMETERS USED TO IDENTIFY SAMPLES");
gd.addStringField("Male","");
gd.addStringField("Date","");
gd.showDialog();
if (gd.wasCanceled())
return;
//PARAMETERS USED TO CALCULATE MORPHOMETRICS
pixelsPerUm = (double)gd.getNextNumber();
//PARAMETERS USED TO DETECT PARTICLES
minSize = (float)gd.getNextNumber()*(float)Math.pow(pixelsPerUm,2);
maxSize = (float)gd.getNextNumber()*(float)Math.pow(pixelsPerUm,2);
minDistance = (float)gd.getNextNumber()*(float)pixelsPerUm;
//PARAMETERS USED TO IDENTIFY SAMPLES
male = gd.getNextString();
date = gd.getNextString();
createGUI();
morphometrics.show("Morphometrics");
}
/******************************************************/
/**
*
*/
public void setCanvas(){
ImageWindow win = imp.getWindow();
canvas = win.getCanvas();
canvas.addMouseListener(this);
}
/******************************************************/
/**
*
*/
public void createGUI() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//natural height, maximum width
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
JButton btnCompleteSpermatozoon = new JButton("Complete");
btnCompleteSpermatozoon.setBackground(Color.LIGHT_GRAY);
//Add action listener
btnCompleteSpermatozoon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
activeImage = 1;
if(completeImpOrig==null)
completeImpOrig = IJ.openImage();
if(completeImpOrig!=null){//Usefull when the user cancel before load an image
completeImpDraw = completeImpOrig.duplicate();
imp.setProcessor(completeImpDraw.getProcessor());
imp.setTitle("Complete");
imp.show();
if(!hasCanvas){
setCanvas();
hasCanvas=true;
}
setCompleteImage(false);
hideSliderEvent=true;
sldThreshold.setValue((int)completeThreshold);
}
}
} );
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
panel.add(btnCompleteSpermatozoon, c);
JButton btnAcrosome = new JButton("Acrosome");
btnAcrosome.setBackground(Color.GREEN);
btnAcrosome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(acrosomeImpOrig==null)
acrosomeImpOrig = IJ.openImage();
if(acrosomeImpOrig!=null){//Usefull when the user cancel before load an image
activeImage = 2;
acrosomeImpDraw = acrosomeImpOrig.duplicate();
imp.setProcessor(acrosomeImpDraw.getProcessor());
imp.setTitle("Acrosome");
imp.show();
if(!hasCanvas){
setCanvas();
hasCanvas=true;
}
setAcrosomeImage(false);
hideSliderEvent=true;
sldThreshold.setValue((int)acrosomeThreshold);
}
}
} );
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 2;
panel.add(btnAcrosome, c);
JButton btnNucleusA = new JButton("Nucleus A");
btnNucleusA.setBackground(Color.CYAN);
btnNucleusA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(nucleusAImpOrig==null)
nucleusAImpOrig = IJ.openImage();
if(nucleusAImpOrig!=null){//Usefull when the user cancel before load an image
activeImage = 3;
nucleusAImpDraw = nucleusAImpOrig.duplicate();
imp.setProcessor(nucleusAImpDraw.getProcessor());
imp.setTitle("Alive's Nucleus");
imp.show();
if(!hasCanvas){
setCanvas();
hasCanvas=true;
}
setNucleusAImage(false);
hideSliderEvent=true;
sldThreshold.setValue((int)nucleusAThreshold);
}
}
} );
c.gridx = 4;
c.gridy = 0;
c.gridwidth = 2;
panel.add(btnNucleusA, c);
JButton btnNucleusD = new JButton("Nucleus D");
btnNucleusD.setBackground(Color.MAGENTA);
btnNucleusD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(nucleusDImpOrig==null)
nucleusDImpOrig = IJ.openImage();
if(nucleusDImpOrig!=null){//Usefull when the user cancel before load an image
activeImage = 4;
nucleusDImpDraw = nucleusDImpOrig.duplicate();
imp.setProcessor(nucleusDImpDraw.getProcessor());
imp.setTitle("Dead's Nucleus");
imp.show();
if(!hasCanvas){
setCanvas();
hasCanvas=true;
}
setNucleusDImage(false);
hideSliderEvent=true;
sldThreshold.setValue((int)nucleusDThreshold);
}
}
} );
c.gridx = 6;
c.gridy = 0;
c.gridwidth = 2;
panel.add(btnNucleusD, c);
//RADIO BUTTONS
JRadioButton completeOtsuButton = new JRadioButton("Otsu");
completeOtsuButton.setSelected(true);
completeOtsuButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
completeThresholdMethod="Otsu";
completeThreshold = -1.0;
if((completeImpOrig!=null)&&(activeImage==1)){//Usefull when the user cancel before load an image
completeImpDraw = completeImpOrig.duplicate();
imp.setProcessor(completeImpDraw.getProcessor());
imp.setTitle("Complete");
imp.show();
if(!hasCanvas){
setCanvas();
hasCanvas=true;
}
setCompleteImage(false);
hideSliderEvent=true;
sldThreshold.setValue((int)completeThreshold);
}
}
} );
JRadioButton completeMinimumButton = new JRadioButton("Minimum");
completeMinimumButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
completeThresholdMethod="Minimum";
completeThreshold = -1.0;
if((completeImpOrig!=null)&&(activeImage==1)){//Usefull when the user cancel before load an image
completeImpDraw = completeImpOrig.duplicate();
imp.setProcessor(completeImpDraw.getProcessor());
imp.setTitle("Complete");
imp.show();
if(!hasCanvas){
setCanvas();
hasCanvas=true;
}
setCompleteImage(false);
hideSliderEvent=true;
sldThreshold.setValue((int)completeThreshold);
}
}
} );
//Group the radio buttons.
ButtonGroup completeGroup = new ButtonGroup();
completeGroup.add(completeOtsuButton);
completeGroup.add(completeMinimumButton);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
panel.add(completeOtsuButton,c);
c.gridy = 2;
panel.add(completeMinimumButton,c);
JRadioButton acrosomeOtsuButton = new JRadioButton("Otsu");
acrosomeOtsuButton.setSelected(true);
acrosomeOtsuButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
acrosomeThresholdMethod="Otsu";
acrosomeThreshold = -1.0;
if((acrosomeImpOrig!=null)&&(activeImage==2)){//Usefull when the user cancel before load an image
acrosomeImpDraw = acrosomeImpOrig.duplicate();
imp.setProcessor(acrosomeImpDraw.getProcessor());
imp.setTitle("Acrosome");
imp.show();
if(!hasCanvas){
setCanvas();
hasCanvas=true;
}
setAcrosomeImage(false);
hideSliderEvent=true;
sldThreshold.setValue((int)acrosomeThreshold);
}
}
} );
JRadioButton acrosomeMinimumButton = new JRadioButton("Minimum");
acrosomeMinimumButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
acrosomeThresholdMethod="Minimum";
acrosomeThreshold = -1.0;
if((acrosomeImpOrig!=null)&&(activeImage==2)){//Usefull when the user cancel before load an image
acrosomeImpDraw = acrosomeImpOrig.duplicate();
imp.setProcessor(acrosomeImpDraw.getProcessor());
imp.setTitle("Acrosome");
imp.show();
if(!hasCanvas){
setCanvas();
hasCanvas=true;
}
setAcrosomeImage(false);
hideSliderEvent=true;
sldThreshold.setValue((int)acrosomeThreshold);
}
}
} );
//Group the radio buttons.
ButtonGroup acrosomeGroup = new ButtonGroup();
acrosomeGroup.add(acrosomeOtsuButton);
acrosomeGroup.add(acrosomeMinimumButton);
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
panel.add(acrosomeOtsuButton,c);
c.gridy = 2;
panel.add(acrosomeMinimumButton,c);
JRadioButton nucleusAOtsuButton = new JRadioButton("Otsu");
nucleusAOtsuButton.setSelected(true);
nucleusAOtsuButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nucleusAThresholdMethod="Otsu";
nucleusAThreshold = -1.0;
if((nucleusAImpOrig!=null)&&(activeImage==3)){//Usefull when the user cancel before load an image
nucleusAImpDraw = nucleusAImpOrig.duplicate();
imp.setProcessor(nucleusAImpDraw.getProcessor());
imp.setTitle("NucleusA");
imp.show();
if(!hasCanvas){
setCanvas();
hasCanvas=true;
}
setNucleusAImage(false);
hideSliderEvent=true;
sldThreshold.setValue((int)nucleusAThreshold);
}
}
} );
JRadioButton nucleusAMinimumButton = new JRadioButton("Minimum");
nucleusAMinimumButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nucleusAThresholdMethod="Minimum";
nucleusAThreshold = -1.0;
if((nucleusAImpOrig!=null)&&(activeImage==3)){//Usefull when the user cancel before load an image
nucleusAImpDraw = nucleusAImpOrig.duplicate();
imp.setProcessor(nucleusAImpDraw.getProcessor());
imp.setTitle("NucleusA");
imp.show();
if(!hasCanvas){
setCanvas();
hasCanvas=true;
}
setNucleusAImage(false);
hideSliderEvent=true;
sldThreshold.setValue((int)nucleusAThreshold);
}
}
} );
//Group the radio buttons.
ButtonGroup nucleusAGroup = new ButtonGroup();
nucleusAGroup.add(nucleusAOtsuButton);
nucleusAGroup.add(nucleusAMinimumButton);
c.gridx = 4;
c.gridy = 1;
c.gridwidth = 1;
panel.add(nucleusAOtsuButton,c);
c.gridy = 2;
panel.add(nucleusAMinimumButton,c);
JRadioButton nucleusDOtsuButton = new JRadioButton("Otsu");
nucleusDOtsuButton.setSelected(true);
nucleusDOtsuButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nucleusDThresholdMethod="Otsu";
nucleusDThreshold = -1.0;
if((nucleusDImpOrig!=null)&&(activeImage==4)){//Usefull when the user cancel before load an image
nucleusDImpDraw = nucleusDImpOrig.duplicate();
imp.setProcessor(nucleusDImpDraw.getProcessor());
imp.setTitle("NucleusD");
imp.show();
if(!hasCanvas){
setCanvas();
hasCanvas=true;
}
setNucleusDImage(false);
hideSliderEvent=true;
sldThreshold.setValue((int)nucleusDThreshold);
}
}
} );
JRadioButton nucleusDMinimumButton = new JRadioButton("Minimum");
nucleusDMinimumButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nucleusDThresholdMethod="Minimum";
nucleusDThreshold = -1.0;
if((nucleusDImpOrig!=null)&&(activeImage==4)){//Usefull when the user cancel before load an image
nucleusDImpDraw = nucleusDImpOrig.duplicate();
imp.setProcessor(nucleusDImpDraw.getProcessor());
imp.setTitle("NucleusD");
imp.show();
if(!hasCanvas){
setCanvas();
hasCanvas=true;
}
setNucleusDImage(false);
hideSliderEvent=true;
sldThreshold.setValue((int)nucleusDThreshold);
}
}
} );
//Group the radio buttons.
ButtonGroup nucleusDGroup = new ButtonGroup();
nucleusDGroup.add(nucleusDOtsuButton);
nucleusDGroup.add(nucleusDMinimumButton);
c.gridx = 6;
c.gridy = 1;
c.gridwidth = 1;
panel.add(nucleusDOtsuButton,c);
c.gridy = 2;
panel.add(nucleusDMinimumButton,c);
// THRESHOLD SLIDERBAR
sldThreshold= new JSlider(JSlider.HORIZONTAL, 0, 255, 60);
sldThreshold.setMinorTickSpacing(2);
sldThreshold.setMajorTickSpacing(10);
sldThreshold.setPaintTicks(true);
sldThreshold.setPaintLabels(true);
// We'll just use the standard numeric labels for now...
sldThreshold.setLabelTable(sldThreshold.createStandardLabels(10));
sldThreshold.addChangeListener(this);
c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.gridwidth = 8;
c.gridx = 0;
c.gridy = 3;
panel.add(sldThreshold, c);
JButton btnMNAN = new JButton("MNAN");
btnMNAN.setBackground(Color.LIGHT_GRAY);
//Add action listener
btnMNAN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showAll = false;
showMNAN = true;
showMDAN = false;
showMNAD = false;
showMDAD = false;
showGreen = false;
showUnknown = false;
//refreshImage();
JLPencilUsed.setText("Pencil Used: MNAN");
}
} );
c.ipady = 0;
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 1;
panel.add(btnMNAN, c);
JButton btnMDAN = new JButton("MDAN");
btnMDAN.setBackground(Color.LIGHT_GRAY);
//Add action listener
btnMDAN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showAll = false;
showMNAN = false;
showMDAN = true;
showMNAD = false;
showMDAD = false;
showGreen = false;
showUnknown = false;
//refreshImage();
JLPencilUsed.setText("Pencil Used: MDAN");
}
} );
c.gridx = 1;
c.gridy = 4;
panel.add(btnMDAN, c);
JButton btnMNAD = new JButton("MNAD");
btnMNAD.setBackground(Color.LIGHT_GRAY);
//Add action listener
btnMNAD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showAll = false;
showMNAN = false;
showMDAN = false;
showMNAD = true;
showMDAD = false;
showGreen = false;
showUnknown = false;
//refreshImage();
JLPencilUsed.setText("Pencil Used: MNAD");
}
} );
c.gridx = 2;
c.gridy = 4;
panel.add(btnMNAD, c);
JButton btnMDAD = new JButton("MDAD");
btnMDAD.setBackground(Color.LIGHT_GRAY);
//Add action listener
btnMDAD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showAll = false;
showMNAN = false;
showMDAN = false;
showMNAD =false;
showMDAD = true;
showGreen = false;
showUnknown = false;
//refreshImage();
JLPencilUsed.setText("Pencil Used: MDAD");
}
} );
c.gridx = 3;
c.gridy =4;
panel.add(btnMDAD, c);
JButton btnGreen= new JButton("Green");
btnGreen.setBackground(Color.LIGHT_GRAY);
//Add action listener
btnGreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showAll = false;
showMNAN = false;
showMDAN = false;
showMNAD =false;
showMDAD = false;
showGreen = true;
showUnknown = false;
//refreshImage();
JLPencilUsed.setText("Pencil Used: Green");
}
} );
c.gridx = 4;
c.gridy = 4;
panel.add(btnGreen, c);
JButton btnUnknown= new JButton("Unknown");
btnUnknown.setBackground(Color.LIGHT_GRAY);
//Add action listener
btnUnknown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showAll = false;
showMNAN = false;
showMDAN = false;
showMNAD = false;
showMDAD = false;
showGreen = false;
showUnknown = true;
//refreshImage();
JLPencilUsed.setText("Pencil Used: Unknown");
}
} );
c.gridx = 5;
c.gridy = 4;
panel.add(btnUnknown, c);
JButton btnAll= new JButton("No Pencil");
btnAll.setBackground(Color.LIGHT_GRAY);
//Add action listener
btnAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showAll = true;
showMNAN = false;
showMDAN = false;
showMNAD = false;
showMDAD = false;
showGreen = false;
showUnknown = false;
//refreshImage();
JLPencilUsed.setText("Pencil Used: -");
}
} );
c.gridx = 6;
c.gridy = 4;
panel.add(btnAll, c);
JLPencilUsed = new JLabel("Pencil Used: -");//,JLabel.CENTER);
JLPencilUsed .setFont(new Font("Serif", Font.PLAIN, 22));
//c.ipady = 0;
c.gridx = 0;
c.gridy = 5;
c.gridwidth = 8;
panel.add(JLPencilUsed , c);
JButton btnResetImages = new JButton("Reset Images");
btnResetImages.setBackground(Color.YELLOW);
//Add action listener
btnResetImages.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
imp.close();
//Here we'll store the original images
completeImpOrig = null;
acrosomeImpOrig = null;
nucleusAImpOrig = null;
nucleusDImpOrig = null;
//These ImagePlus will be used to draw over them
completeImpDraw = null;
acrosomeImpDraw = null;
nucleusAImpDraw = null;
nucleusDImpDraw = null;
//These ImagePlus will be used to calculate mean gray values
completeImpGray = null;
acrosomeImpGray = null;
nucleusAImpGray = null;
nucleusDImpGray = null;
//These ImagePlus will be used to identify spermatozoa
completeImpTh = null;
acrosomeImpTh = null;
nucleusAImpTh = null;
nucleusDImpTh = null;
//These ImagePlus will be used to store outlines
completeImpOutline = null;
acrosomeImpOutline = null;
nucleusAImpOutline = null;
nucleusDImpOutline = null;
//Thresholds
completeThreshold = -1.0;
acrosomeThreshold = -1.0;
nucleusAThreshold = -1.0;
nucleusDThreshold = -1.0;
//Variables used to identify spermatozoa
completeSpermatozoa = new ArrayList();
acrosomeSpermatozoa = new ArrayList();
nucleusASpermatozoa = new ArrayList();
nucleusDSpermatozoa = new ArrayList();
//GUI elements and variables
hasCanvas = false;
}
} );
//c.weightx = 0.5;
//c.fill = GridBagConstraints.HORIZONTAL;
//c.ipady = 5;
c.gridx = 7;
c.gridy = 4;
c.gridwidth = 1;
panel.add(btnResetImages, c);
JFrame frame = new JFrame("Adjust Threshold");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
frame.setPreferredSize(new Dimension((int)width, 250));
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
/******************************************************/
/**
*
*/
public void setCompleteImage(boolean isEvent){
if(completeThreshold==-1 || isEvent){//First time
completeImpTh = completeImpOrig.duplicate();
convertToGrayscale(completeImpTh);
completeImpGray = completeImpTh.duplicate();
thresholdImagePlus(completeImpTh);
completeSpermatozoa = detectSpermatozoa(completeImpTh);
typeSpermatozoa(imp,completeSpermatozoa);
//Calculate outlines
completeImpOutline = completeImpTh.duplicate();
outlineThresholdImage(completeImpOutline);
idenfityCompletes();
}
drawOutline(imp,completeImpOutline);
drawBoundaries(imp,completeSpermatozoa);
imp.updateAndRepaintWindow();
}
/******************************************************/
/**
*
*/
public void setAcrosomeImage(boolean isEvent){
if(acrosomeThreshold==-1 || isEvent){//First time
acrosomeImpTh = acrosomeImpOrig.duplicate();
getGreenChannel(acrosomeImpOrig,acrosomeImpTh);
acrosomeImpGray = acrosomeImpTh.duplicate();
thresholdImagePlus(acrosomeImpTh);
acrosomeSpermatozoa = detectSpermatozoa(acrosomeImpTh);
//Calculate outlines
acrosomeImpOutline = acrosomeImpTh.duplicate();
outlineThresholdImage(acrosomeImpOutline);
//identifySpermatozoa(allSpermatozoa);
idenfityParts(acrosomeSpermatozoa);
}
drawOutline(imp,acrosomeImpOutline);
drawBoundaries(imp,acrosomeSpermatozoa);
imp.updateAndRepaintWindow();
}
/******************************************************/
/**
*
*/
public void setNucleusAImage(boolean isEvent){
if(nucleusAThreshold==-1 || isEvent){//First time
nucleusAImpTh = nucleusAImpOrig.duplicate();
getBlueChannel(nucleusAImpOrig,nucleusAImpTh);
nucleusAImpGray = nucleusAImpTh.duplicate();
thresholdImagePlus(nucleusAImpTh);
nucleusASpermatozoa = detectSpermatozoa(nucleusAImpTh);
//Calculate outlines
nucleusAImpOutline = nucleusAImpTh.duplicate();
outlineThresholdImage(nucleusAImpOutline);
idenfityParts(nucleusASpermatozoa);
}
drawOutline(imp,nucleusAImpOutline);
drawBoundaries(imp,nucleusASpermatozoa);
imp.updateAndRepaintWindow();
}
/******************************************************/
/**
*
*/
public void setNucleusDImage(boolean isEvent){
if(nucleusDThreshold==-1 || isEvent){//First time
nucleusDImpTh = nucleusDImpOrig.duplicate();
getRedChannel(nucleusDImpOrig,nucleusDImpTh);
nucleusDImpGray = nucleusDImpTh.duplicate();
thresholdImagePlus(nucleusDImpTh);
nucleusDSpermatozoa = detectSpermatozoa(nucleusDImpTh);
//Calculate outlines
nucleusDImpOutline = nucleusDImpTh.duplicate();
outlineThresholdImage(nucleusDImpOutline);
idenfityParts(nucleusDSpermatozoa);
}
drawOutline(imp,nucleusDImpOutline);
drawBoundaries(imp,nucleusDSpermatozoa);
imp.updateAndRepaintWindow();
}
/******************************************************/
/**
* @param imp ImagePlus
*/
public void convertToGrayscale (ImagePlus imp){
ImageConverter ic = new ImageConverter(imp);
ic.convertToGray8();
}
/******************************************************/
/**
* @param
*/
public void getRedChannel(ImagePlus impColor,ImagePlus impTh){
ImageStack redStack = ChannelSplitter.getChannel(impColor,1);
ImageProcessor ipRed = redStack.getProcessor(1);
impTh.setProcessor(ipRed);
}
/******************************************************/
/**
* @param
*/
public void getGreenChannel(ImagePlus impColor,ImagePlus impTh){
ImageStack greenStack = ChannelSplitter.getChannel(impColor,2);
ImageProcessor ipGreen = greenStack.getProcessor(1);
impTh.setProcessor(ipGreen);
}
/******************************************************/
/**
* @param
*/
public void getBlueChannel(ImagePlus impColor,ImagePlus impTh){
ImageStack blueStack = ChannelSplitter.getChannel(impColor,3);
ImageProcessor ipBlue = blueStack.getProcessor(1);
impTh.setProcessor(ipBlue);
}
/******************************************************/
/**
* @param imp ImagePlus
*/
public void thresholdImagePlus(ImagePlus imp){
ImageProcessor ip = imp.getProcessor();
double lowerThreshold = 0;
String thresholdMethod = "";
if(activeImage==1){
lowerThreshold = completeThreshold;
thresholdMethod = completeThresholdMethod;
}
else if(activeImage==2){
lowerThreshold = acrosomeThreshold;
thresholdMethod = acrosomeThresholdMethod;
}
else if(activeImage==3){
lowerThreshold = nucleusAThreshold;
thresholdMethod = nucleusAThresholdMethod;
}
else if(activeImage==4){
lowerThreshold = nucleusDThreshold;
thresholdMethod = nucleusDThresholdMethod;
}
//First we look at if threshold has been set before
//Else we have to calculate it
if(lowerThreshold==-1){
ImageStatistics st = ip.getStatistics();
long[] histlong = st.getHistogram();
int histogram[] = convertLongArrayToInt(histlong);
AutoThresholder at = new AutoThresholder();
lowerThreshold = (double)at.getThreshold(thresholdMethod,histogram);
hideSliderEvent=true;
sldThreshold.setValue((int)lowerThreshold);
if(activeImage==1)
completeThreshold = lowerThreshold;
else if(activeImage==2)
acrosomeThreshold = lowerThreshold;
else if(activeImage==3)
nucleusAThreshold = lowerThreshold;
else if (activeImage==4)
nucleusDThreshold = lowerThreshold;
}
//Upper threshold set to maximum
double upperThreshold = 255;
//Threshold image processor
thresholdImageProcessor(ip,lowerThreshold,upperThreshold);
}
/******************************************************/
/**
* @param imp ImagePlus
*/