/
TrackMateSpotDetector.java
195 lines (159 loc) · 6.09 KB
/
TrackMateSpotDetector.java
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
package ch.fmi;
import com.google.common.primitives.Doubles;
import com.google.common.primitives.Ints;
import fiji.plugin.trackmate.Model;
import fiji.plugin.trackmate.Settings;
import fiji.plugin.trackmate.Spot;
import fiji.plugin.trackmate.SpotCollection;
import fiji.plugin.trackmate.TrackMate;
import fiji.plugin.trackmate.detection.DetectorKeys;
import fiji.plugin.trackmate.detection.LogDetectorFactory;
import fiji.plugin.trackmate.features.FeatureFilter;
import fiji.plugin.trackmate.features.spot.SpotContrastAnalyzerFactory;
import fiji.plugin.trackmate.features.spot.SpotIntensityAnalyzerFactory;
import fiji.plugin.trackmate.features.spot.SpotRadiusEstimatorFactory;
import ij.ImagePlus;
import ij.gui.Roi;
import ij.plugin.filter.ThresholdToSelection;
import ij.process.ImageProcessor;
import java.util.ArrayList;
import org.scijava.ItemIO;
import org.scijava.command.Command;
import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
@Plugin(type = Command.class, headless = true, menuPath = "FMI>Spot Detection (Subpixel localization)")
public class TrackMateSpotDetector implements Command {
@Parameter
private LogService log;
@Parameter(label = "Input image")
private ImagePlus imp;
@Parameter(label = "ROI mask", required = false)
private ImagePlus mask;
@Parameter(label = "Spot radius")
private double spotSize = DetectorKeys.DEFAULT_RADIUS;
@Parameter(label = "Spot quality threshold")
private double spotThreshold = DetectorKeys.DEFAULT_THRESHOLD;
@Parameter(label = "Filter max quality spot per frame", required = false)
private boolean filterMaxQuality = false;
@Parameter(label = "Remove calibration?")
private boolean removeCalibration = false;
@Parameter(label = "Use mask?")
private boolean useMask = true;
@Parameter(type = ItemIO.OUTPUT)
private int nSpotsFound;
@Parameter(type = ItemIO.OUTPUT)
private int[] spotID;
@Parameter(type = ItemIO.OUTPUT)
private double[] x;
@Parameter(type = ItemIO.OUTPUT)
private double[] y;
@Parameter(type = ItemIO.OUTPUT)
private double[] z;
@Parameter(type = ItemIO.OUTPUT)
private double[] frame;
@Parameter(type = ItemIO.OUTPUT)
private double[] radius;
@Parameter(type = ItemIO.OUTPUT)
private double[] quality;
@Parameter(type = ItemIO.OUTPUT)
private double[] totalIntensity;
@Parameter(type = ItemIO.OUTPUT)
private double[] meanIntensity;
@Parameter(type = ItemIO.OUTPUT)
private double[] estDiameter;
@Parameter(type = ItemIO.OUTPUT)
private double[] contrast;
@Override
public void run() {
// Set mask ROI on input image
if (mask != null && useMask) {
mask.getProcessor().setThreshold(1.0, Double.POSITIVE_INFINITY,
ImageProcessor.NO_LUT_UPDATE);
Roi roi = ThresholdToSelection.run(mask);
imp.setRoi(roi);
}
// Create TrackMate instance with settings
Model model = new Model();
// optionally remove calibration from imp
if (removeCalibration) {
imp.setCalibration(null);
}
Settings settings = new Settings();
settings.setFrom(imp);
// TODO make detector choice optional
settings.detectorFactory = new LogDetectorFactory<>();
settings.detectorSettings = settings.detectorFactory
.getDefaultSettings();
settings.detectorSettings.put( //
DetectorKeys.KEY_DO_SUBPIXEL_LOCALIZATION, true);
settings.detectorSettings.put( //
DetectorKeys.KEY_RADIUS, spotSize);
settings.detectorSettings.put( //
DetectorKeys.KEY_THRESHOLD, spotThreshold);
settings.addSpotAnalyzerFactory(new SpotIntensityAnalyzerFactory<>());
settings.addSpotAnalyzerFactory(new SpotRadiusEstimatorFactory<>());
settings.addSpotAnalyzerFactory(new SpotContrastAnalyzerFactory<>());
settings.addSpotAnalyzerFactory(new MaxQualitySpotAnalyzerFactory<>());
if (filterMaxQuality) {
settings.addSpotFilter(new FeatureFilter(
MaxQualitySpotAnalyzerFactory.HAS_MAX_QUALITY_IN_FRAME,
0.5, true));
}
TrackMate trackmate = new TrackMate(model, settings);
// Prepare lists to collect results
ArrayList<Integer> spotIDlist = new ArrayList<>();
ArrayList<Double> xList = new ArrayList<>();
ArrayList<Double> yList = new ArrayList<>();
ArrayList<Double> zList = new ArrayList<>();
ArrayList<Double> frameList = new ArrayList<>();
ArrayList<Double> radiusList = new ArrayList<>();
ArrayList<Double> qualityList = new ArrayList<>();
ArrayList<Double> totalIntensityList = new ArrayList<>();
ArrayList<Double> meanIntensityList = new ArrayList<>();
ArrayList<Double> estDiameterList = new ArrayList<>();
ArrayList<Double> contrastList = new ArrayList<>();
// TODO add other outputs
if (trackmate.execDetection() && trackmate.computeSpotFeatures(true)
&& trackmate.execSpotFiltering(true)) {
// Get spot collection (all spots)
SpotCollection spotCollection = model.getSpots();
for (Spot spot : spotCollection.iterable(false)) {
spotIDlist.add(spot.ID());
xList.add(spot.getDoublePosition(0));
yList.add(spot.getDoublePosition(1));
zList.add(spot.getDoublePosition(2));
frameList.add(spot.getFeature(Spot.FRAME));
radiusList.add(spot.getFeature(Spot.RADIUS));
qualityList.add(spot.getFeature(Spot.QUALITY));
totalIntensityList
.add(spot
.getFeature(SpotIntensityAnalyzerFactory.TOTAL_INTENSITY));
meanIntensityList
.add(spot
.getFeature(SpotIntensityAnalyzerFactory.MEAN_INTENSITY));
estDiameterList
.add(spot
.getFeature(SpotRadiusEstimatorFactory.ESTIMATED_DIAMETER));
contrastList.add(spot
.getFeature(SpotContrastAnalyzerFactory.KEY));
}
} else {
log.warn(trackmate.getErrorMessage());
}
// Get results (spot list)
spotID = Ints.toArray(spotIDlist);
x = Doubles.toArray(xList);
y = Doubles.toArray(yList);
z = Doubles.toArray(zList);
frame = Doubles.toArray(frameList);
radius = Doubles.toArray(radiusList);
quality = Doubles.toArray(qualityList);
totalIntensity = Doubles.toArray(totalIntensityList);
meanIntensity = Doubles.toArray(meanIntensityList);
estDiameter = Doubles.toArray(estDiameterList);
contrast = Doubles.toArray(contrastList);
// Return summary values
nSpotsFound = model.getSpots().getNSpots(false);
}
}