forked from fiji/SNT
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Tubeness.java
290 lines (249 loc) · 11.1 KB
/
Tubeness.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
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
/*-
* #%L
* Fiji distribution of ImageJ for the life sciences.
* %%
* Copyright (C) 2010 - 2023 Fiji developers.
* %%
* 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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
package sc.fiji.snt.filter;
import net.imagej.ops.Ops;
import net.imagej.ops.special.computer.AbstractUnaryComputerOp;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.convolution.fast_gauss.FastGauss;
import net.imglib2.algorithm.gradient.HessianMatrix;
import net.imglib2.algorithm.linalg.eigen.TensorEigenValues;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.loops.LoopBuilder;
import net.imglib2.outofbounds.OutOfBoundsBorderFactory;
import net.imglib2.parallel.DefaultTaskExecutor;
import net.imglib2.parallel.TaskExecutor;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.util.Intervals;
import net.imglib2.view.IntervalView;
import net.imglib2.view.Views;
import org.scijava.Priority;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
/**
* Y. Sato, S. Nakajima, N. Shiraga, H. Atsumi, S. Yoshida, T. Koller, G. Gerig, and R. Kikinis,
* “Three-dimensional multi-scale line filter for segmentation and visualization of curvilinear
* structures in medical images,”
* Med Image Anal., vol. 2, no. 2, pp. 143-168, June 1998.
*
* @author Cameron Arshadi
*/
@Plugin(type = Ops.Filter.Tubeness.class, priority = Priority.NORMAL)
public class Tubeness<T extends RealType<T>, U extends RealType<U>> extends
AbstractUnaryComputerOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<U>>
implements Ops.Filter.Tubeness, Consumer<RandomAccessibleInterval<U>>
{
@Parameter
private double[] spacing;
@Parameter
private double[] scales;
@Parameter
private int numThreads;
public Tubeness(final double[] scales, final double[] spacing) {
this(scales, spacing, Runtime.getRuntime().availableProcessors());
}
public Tubeness(final double[] scales, final double[] spacing, final int numThreads) {
this.scales = scales;
this.spacing = spacing;
this.numThreads = numThreads;
}
@Override
public void run() {
compute(in(), out());
}
@Override
public RandomAccessibleInterval<U> run(final RandomAccessibleInterval<U> output) {
compute(in(), output);
return output;
}
@Override
public void accept(final RandomAccessibleInterval<U> output) {
compute(in(), output);
}
@Override
public void compute(final RandomAccessibleInterval<T> input, final RandomAccessibleInterval<U> output) {
final int nDim = input.numDimensions();
if (nDim > 3 || nDim < 2) {
throw new IllegalArgumentException("Only 2D and 3D images are supported");
}
if (output.numDimensions() != nDim) {
throw new IllegalArgumentException("input and output must have same dimensions");
}
final boolean is3D = (nDim == 3);
if (scales == null)
throw new IllegalArgumentException("scales array is null");
if (spacing == null)
throw new IllegalArgumentException("spacing array is null");
if (numThreads < 1)
this.numThreads = 1;
else
this.numThreads = Math.min(numThreads, Runtime.getRuntime().availableProcessors());
// Convert the desired scales (physical units) into sigmas (pixel units)
final List<double[]> sigmas = new ArrayList<>();
for (final double sc : scales) {
final double[] sigma = new double[nDim];
for (int d = 0; d < nDim; ++d) {
sigma[d] = sc / spacing[d];
}
sigmas.add(sigma);
}
final long[] gaussianPad = new long[nDim];
final long[] gaussianOffset = new long[nDim];
final long[] gradientPad = new long[nDim + 1];
gradientPad[gradientPad.length - 1] = nDim;
final long[] gradientOffset = new long[nDim + 1];
final long[] hessianPad = new long[nDim + 1];
hessianPad[hessianPad.length - 1] = nDim * (nDim + 1) / 2;
final long[] hessianOffset = new long[nDim + 1];
final ExecutorService es = Executors.newFixedThreadPool(numThreads);
try (final TaskExecutor ex = new DefaultTaskExecutor(es)) {
for (final double[] sigma : sigmas) {
final long[] blockSize = Intervals.dimensionsAsLongArray(output);
for (int d = 0; d < blockSize.length; ++d) {
gaussianPad[d] = blockSize[d] + 6;
gaussianOffset[d] = output.min(d) - 2;
}
RandomAccessibleInterval<DoubleType> tmpGaussian = Views.translate(
ArrayImgs.doubles(gaussianPad), gaussianOffset);
FastGauss.convolve(sigma, Views.extendBorder(input), tmpGaussian);
for (int d = 0; d < gradientPad.length - 1; ++d) {
gradientPad[d] = blockSize[d] + 4;
gradientOffset[d] = output.min(d) - 1;
hessianPad[d] = blockSize[d];
hessianOffset[d] = output.min(d);
}
RandomAccessibleInterval<DoubleType> tmpGradient = ArrayImgs.doubles(gradientPad);
RandomAccessibleInterval<DoubleType> tmpHessian = ArrayImgs.doubles(hessianPad);
HessianMatrix.calculateMatrix(
Views.extendBorder(tmpGaussian),
Views.translate(tmpGradient, gradientOffset),
Views.translate(tmpHessian, hessianOffset),
new OutOfBoundsBorderFactory<>(),
numThreads,
es);
RandomAccessibleInterval<DoubleType> tmpEigenvalues = TensorEigenValues.createAppropriateResultImg(
tmpHessian,
new ArrayImgFactory<>(new DoubleType()));
TensorEigenValues.calculateEigenValuesSymmetric(
tmpHessian,
tmpEigenvalues,
numThreads,
es);
// FIXME: this normalizes the filter response using the average voxel separation at a scale
double avgSigma = 0d;
for (double s : sigma) {
avgSigma += s;
}
avgSigma /= sigma.length;
RandomAccessibleInterval<DoubleType> tmpTubeness = ArrayImgs.doubles(blockSize);
if (is3D) {
tubeness3D(tmpEigenvalues, tmpTubeness, avgSigma, ex);
} else {
tubeness2D(tmpEigenvalues, tmpTubeness, avgSigma, ex);
}
LoopBuilder.setImages(output, tmpTubeness)
.multiThreaded(ex)
.forEachPixel((b, t) -> b.setReal(Math.max(b.getRealDouble(), t.getRealDouble())));
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
System.err.println("Out of memory computing Tubeness. Try Lazy processing instead.");
e.printStackTrace();
}
}
private void tubeness2D(final RandomAccessibleInterval<DoubleType> eigenvalueRai,
final RandomAccessibleInterval<DoubleType> tubenessRai,
final double sigma,
final TaskExecutor ex)
{
final int d = eigenvalueRai.numDimensions() - 1;
final IntervalView<DoubleType> evs0 = Views.hyperSlice(eigenvalueRai, d, 0);
final IntervalView<DoubleType> evs1 = Views.hyperSlice(eigenvalueRai, d, 1);
// normalize filter response for fair comparison at multiple scales
final double norm = sigma * sigma;
LoopBuilder.setImages(evs0, evs1, tubenessRai).multiThreaded(ex).forEachPixel(
(ev0, ev1, v) -> {
double e0 = ev0.getRealDouble();
double e1 = ev1.getRealDouble();
// Sort by absolute value
if (Math.abs(e0) > Math.abs(e1)) {
e1 = e0;
}
double result = 0;
if (e1 < 0) {
result = norm * Math.abs(e1);
}
v.setReal(result);
}
);
}
private void tubeness3D(final RandomAccessibleInterval<DoubleType> eigenvalueRai,
final RandomAccessibleInterval<DoubleType> tubenessRai,
final double sigma,
final TaskExecutor ex)
{
final int d = eigenvalueRai.numDimensions() - 1;
final IntervalView<DoubleType> evs0 = Views.hyperSlice(eigenvalueRai, d, 0);
final IntervalView<DoubleType> evs1 = Views.hyperSlice(eigenvalueRai, d, 1);
final IntervalView<DoubleType> evs2 = Views.hyperSlice(eigenvalueRai, d, 2);
// normalize filter response for fair comparison at multiple scales
final double norm = sigma * sigma;
LoopBuilder.setImages(evs0, evs1, evs2, tubenessRai).multiThreaded(ex).forEachPixel(
(ev0, ev1, ev2, v) -> {
double e0 = ev0.getRealDouble();
double e1 = ev1.getRealDouble();
double e2 = ev2.getRealDouble();
// Sort by absolute value
double temp;
if (Math.abs(e0) > Math.abs(e1)) {
temp = e0;
e0 = e1;
e1 = temp;
}
if (Math.abs(e1) > Math.abs(e2)) {
temp = e1;
e1 = e2;
e2 = temp;
}
if (Math.abs(e0) > Math.abs(e1)) {
// skip assignment to e0, not used in filter
e1 = e0;
}
double result = 0;
if (e1 < 0 && e2 < 0) {
result = norm * Math.sqrt(e1 * e2);
}
v.setReal(result);
}
);
}
}