-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDoGDetectorOp.java
More file actions
260 lines (225 loc) · 9.16 KB
/
DoGDetectorOp.java
File metadata and controls
260 lines (225 loc) · 9.16 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
/*-
* #%L
* mastodon-tracking
* %%
* Copyright (C) 2017 - 2025 Tobias Pietzsch, Jean-Yves Tinevez
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.mastodon.tracking.detection;
import static org.mastodon.tracking.detection.DetectorKeys.KEY_DETECTION_TYPE;
import static org.mastodon.tracking.detection.DetectorKeys.KEY_DO_SUBPIXEL_LOCALIZATION;
import static org.mastodon.tracking.detection.DetectorKeys.KEY_MAX_TIMEPOINT;
import static org.mastodon.tracking.detection.DetectorKeys.KEY_MIN_TIMEPOINT;
import static org.mastodon.tracking.detection.DetectorKeys.KEY_RADIUS;
import static org.mastodon.tracking.detection.DetectorKeys.KEY_ROI;
import static org.mastodon.tracking.detection.DetectorKeys.KEY_SETUP_ID;
import static org.mastodon.tracking.detection.DetectorKeys.KEY_THRESHOLD;
import java.util.ArrayList;
import java.util.List;
import org.mastodon.tracking.detection.DetectionCreatorFactory.DetectionCreator;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.thread.ThreadService;
import bdv.viewer.SourceAndConverter;
import net.imglib2.FinalInterval;
import net.imglib2.Interval;
import net.imglib2.Point;
import net.imglib2.RandomAccessible;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.RealPoint;
import net.imglib2.algorithm.Benchmark;
import net.imglib2.algorithm.dog.DogDetection;
import net.imglib2.algorithm.dog.DogDetection.ExtremaType;
import net.imglib2.algorithm.localextrema.RefinedPeak;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.util.Intervals;
import net.imglib2.view.Views;
/**
* Difference of Gaussian detector.
*
* @author Tobias Pietzsch
* @author Jean-Yves Tinevez
*/
@Plugin( type = DetectorOp.class )
public class DoGDetectorOp
extends AbstractDetectorOp
implements DetectorOp, Benchmark
{
@Parameter
private ThreadService threadService;
/**
* The minimal diameter size, in pixel, under which we stop down-sampling.
*/
public static final double MIN_SPOT_PIXEL_SIZE = 5d;
private long processingTime;
@Override
public void mutate1( final DetectionCreatorFactory detectionCreatorFactory, final List< SourceAndConverter< ? > > sources )
{
ok = false;
final long start = System.currentTimeMillis();
final StringBuilder str = new StringBuilder();
if ( !DetectionUtil.checkSettingsValidity( settings, str ) )
{
processingTime = System.currentTimeMillis() - start;
statusService.clearStatus();
errorMessage = str.toString();
return;
}
final int minTimepoint = ( int ) settings.get( KEY_MIN_TIMEPOINT );
final int maxTimepoint = ( int ) settings.get( KEY_MAX_TIMEPOINT );
final int setup = ( int ) settings.get( KEY_SETUP_ID );
final double radius = ( double ) settings.get( KEY_RADIUS );
final double threshold = ( double ) settings.get( KEY_THRESHOLD );
final Interval roi = ( Interval ) settings.get( KEY_ROI );
final DetectionType detectionType = DetectionType.getOrDefault( ( String ) settings.get( KEY_DETECTION_TYPE ), DetectionType.MINIMA );
final boolean doSubpixelLocalization = ( boolean ) settings.get( KEY_DO_SUBPIXEL_LOCALIZATION );
statusService.showStatus( "DoG detection." );
for ( int tp = minTimepoint; tp <= maxTimepoint; tp++ )
{
statusService.showProgress( tp - minTimepoint + 1, maxTimepoint - minTimepoint + 1 );
// Did we get canceled?
if ( isCanceled() )
break;
// Check if there is some data at this timepoint.
if ( !DetectionUtil.isPresent( sources, setup, tp ) )
continue;
/*
* Determine optimal level for detection.
*/
final int level = DetectionUtil.determineOptimalResolutionLevel( sources, radius, MIN_SPOT_PIXEL_SIZE / 2., tp, setup );
/*
* Load and extends image data.
*/
final RandomAccessibleInterval< ? > img = DetectionUtil.getImage( sources, tp, setup, level );
if ( !DetectionUtil.isReallyPresent( img ) )
continue;
// If 2D, the 3rd dimension will be dropped here.
final RandomAccessibleInterval< ? > zeroMin = Views.dropSingletonDimensions( Views.zeroMin( img ) );
@SuppressWarnings( { "unchecked", "rawtypes" } )
final RandomAccessible< FloatType > source = DetectionUtil.asExtendedFloat( ( RandomAccessibleInterval ) zeroMin );
/*
* Transform ROI in higher level.
*/
final Interval interval;
if ( null == roi )
{
interval = zeroMin;
}
else
{
final double[] minSource = new double[ 3 ];
final double[] maxSource = new double[ 3 ];
roi.realMin( minSource );
roi.realMax( maxSource );
final double[] minTarget = new double[ 3 ];
final double[] maxTarget = new double[ 3 ];
final AffineTransform3D mipmapTransform = DetectionUtil.getMipmapTransform( sources, tp, setup, level );
mipmapTransform.applyInverse( minTarget, minSource );
mipmapTransform.applyInverse( maxTarget, maxSource );
// Only take 2D or 3D version of the transformed interval.
final long[] tmin = new long[ zeroMin.numDimensions() ];
final long[] tmax = new long[ zeroMin.numDimensions() ];
for ( int d = 0; d < zeroMin.numDimensions(); d++ )
{
tmin[ d ] = ( long ) Math.ceil( minTarget[ d ] );
tmax[ d ] = ( long ) Math.floor( maxTarget[ d ] );
}
final FinalInterval transformedRoi = new FinalInterval( tmin, tmax );
interval = Intervals.intersect( transformedRoi, zeroMin );
}
// Ensure that the interval size is at least 3 in all dimensions.
final long[] min = new long[interval.numDimensions()];
interval.min( min );
final long[] max = new long[interval.numDimensions()];
interval.max( max );
for ( int d = 0; d < interval.numDimensions(); d++ )
if ( interval.dimension( d ) < 3 )
{
min[ d ]--;
max[ d ]++;
}
final FinalInterval minInterval = new FinalInterval( min, max );
/*
* Process image.
*/
final int stepsPerOctave = 4;
final double k = Math.pow( 2.0, 1.0 / stepsPerOctave );
final double sigma = radius / Math.sqrt( zeroMin.numDimensions() );
final double sigmaSmaller = sigma;
final double sigmaLarger = k * sigmaSmaller;
final double normalization = ( ( detectionType == DetectionType.MAXIMA ) ? 1.0 : -1.0 )
/ ( sigmaLarger / sigmaSmaller - 1.0 );
final double[] pixelSize = DetectionUtil.getPixelSize( sources, tp, setup, level );
final DogDetection< FloatType > dog = new DogDetection<>(
source,
minInterval,
pixelSize,
sigmaSmaller,
sigmaLarger,
( detectionType == DetectionType.MAXIMA ) ? ExtremaType.MAXIMA : ExtremaType.MINIMA,
threshold,
true );
dog.setExecutorService( threadService.getExecutorService() );
final ArrayList< RefinedPeak< Point > > refinedPeaks = doSubpixelLocalization ?
dog.getSubpixelPeaks() :
dog.getPeaks().stream().map( p -> new RefinedPeak< Point >( p, p, 0, false ) ).collect( ArrayList::new, ArrayList::add, ArrayList::addAll );
final double[] pos = new double[ 3 ];
final RealPoint sp = RealPoint.wrap( pos );
final RealPoint p3d = new RealPoint( 3 );
final AffineTransform3D transform = DetectionUtil.getTransform( sources, tp, setup, level );
final DetectionCreator detectionCreator = detectionCreatorFactory.create( tp );
detectionCreator.preAddition();
try
{
for ( final RefinedPeak< Point > p : refinedPeaks )
{
final double value = p.getValue();
final double normalizedValue = value * normalization;
/*
* In case p is 2D we pass it to a 3D RealPoint to work
* nicely with the 3D transform.
*/
for ( int d = 0; d < p.numDimensions(); d++ )
p3d.setPosition( p.getDoublePosition( d ), d );
transform.apply( p3d, sp );
detectionCreator.createDetection( pos, radius, normalizedValue );
}
}
finally
{
detectionCreator.postAddition();
}
}
final long end = System.currentTimeMillis();
processingTime = end - start;
statusService.clearStatus();
ok = true;
}
@Override
public long getProcessingTime()
{
return processingTime;
}
}