-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThresholdChangeListener.java
More file actions
126 lines (106 loc) · 3.18 KB
/
Copy pathThresholdChangeListener.java
File metadata and controls
126 lines (106 loc) · 3.18 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
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import ij.IJ;
import ij.ImagePlus;
import ij.WindowManager;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
*
* @author Jesus Cuenca
* @see ij.plugin.Thresholder and ij.plugin.frame.ThresholdAdjuster
*/
public class ThresholdChangeListener implements ChangeListener, AdjustmentListener {
private ChannelHistogram slider;
public ChannelHistogram getSlider() {
return slider;
}
public void setSlider(ChannelHistogram slider) {
this.slider = slider;
}
public int getChannel() {
if(getSlider()!= null)
return getSlider().getNumber();
return 0;
}
ThresholdChangeListener(ChannelHistogram slider) {
this.slider=slider;
}
protected double defaultMaxThreshold=255, defaultMinThreshold=0;
/**
* When the user changes channel in the Hyperstack viewer, this class is notified through this method
*/
public void adjustmentValueChanged(AdjustmentEvent e) {
if(getImagePlus() != null)
try{
applyCurrentThreshold();
}catch (NullPointerException ex){
// sometimes the call to applyCurrentThreshold fails for channel #2 (with a null pointer exception),
// hence the need to catch it and repeat the call
applyCurrentThreshold();
}
}
/**
* There was a change in one of the threshold sliders, which triggers this stateChanged method
*/
public void stateChanged(ChangeEvent event){
if(updateThreshold(event))
setThresholdForCurrentChannel(getLowThreshold(), getHighThreshold());
}
private JSlider getSlider(ChangeEvent event){
return (JSlider)event.getSource();
}
protected int getValue(ChangeEvent event){
return (getSlider(event).getValue());
}
protected boolean isValueAdjusting(ChangeEvent event){
boolean result= false;
try{
result = getSlider(event).getValueIsAdjusting();
}catch (ClassCastException ex){
result = false;
}
return result;
}
private ImagePlus getImagePlus(){
if(WindowManager.getCurrentWindow() == null)
return null;
else
return WindowManager.getCurrentImage();
}
protected boolean updateThreshold(ChangeEvent event){
return (!isValueAdjusting(event)) && getImagePlus() != null;
}
protected double getHighThreshold(){
if(getSlider() != null)
return getSlider().getHigh();
else
return defaultMaxThreshold;
}
protected double getLowThreshold(){
if(getSlider() != null)
return getSlider().getLow();
else
return defaultMinThreshold;
}
protected void setThresholdForCurrentChannel(double low, double high){
if(getImagePlus() != null){
int currentChannel = getImagePlus().getChannel();
// int currentChannel = getSlider().getNumber();
if(getChannel() == currentChannel){
if(getSlider().isSelected()){
IJ.setThreshold(low,high,Distance_Measure.THRESHOLD_METHOD);
// IJ.setThreshold(getImagePlus(), low, high, "black");
// getImageProcessor().setThreshold(min, max, ImageProcessor.NO_LUT_UPDATE);
// getImagePlus().updateAndDraw();
}else{
IJ.resetThreshold();
}
}
}
}
protected void applyCurrentThreshold(){
setThresholdForCurrentChannel(getLowThreshold(), getHighThreshold());
}
}