Skip to content

Commit 92fac01

Browse files
committed
change hessian to use compositeview
1 parent a9b06e6 commit 92fac01

File tree

3 files changed

+40
-73
lines changed

3 files changed

+40
-73
lines changed

src/main/java/net/imagej/ops/filter/FilterNamespace.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -857,28 +857,16 @@ RandomAccessibleInterval<V> gauss(final RandomAccessibleInterval<T> in,
857857
// -- hessian --
858858

859859
@OpMethod(op = net.imagej.ops.filter.hessian.HessianRAI.class)
860-
public <T extends RealType<T>> RandomAccessibleInterval<T> hessian(
860+
public <T extends RealType<T>> CompositeIntervalView<T, RealComposite<T>> hessian(
861861
final RandomAccessibleInterval<T> in)
862862
{
863863
@SuppressWarnings("unchecked")
864-
final RandomAccessibleInterval<T> result =
865-
(RandomAccessibleInterval<T>) ops().run(
864+
final CompositeIntervalView<T, RealComposite<T>> result =
865+
(CompositeIntervalView<T, RealComposite<T>>) ops().run(
866866
net.imagej.ops.filter.hessian.HessianRAI.class, in);
867867
return result;
868868
}
869869

870-
@OpMethod(op = net.imagej.ops.filter.hessian.HessianRAI.class)
871-
public <T extends RealType<T>> RandomAccessibleInterval<T> hessian(
872-
final RandomAccessibleInterval<T> out,
873-
final RandomAccessibleInterval<T> in)
874-
{
875-
@SuppressWarnings("unchecked")
876-
final RandomAccessibleInterval<T> result =
877-
(RandomAccessibleInterval<T>) ops().run(
878-
net.imagej.ops.filter.hessian.HessianRAI.class, out, in);
879-
return result;
880-
}
881-
882870
// -- ifft --
883871

884872
/** Executes the "ifft" operation on the given arguments. */

src/main/java/net/imagej/ops/filter/hessian/HessianRAI.java

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,19 @@
2929

3030
package net.imagej.ops.filter.hessian;
3131

32+
import java.util.ArrayList;
33+
import java.util.List;
34+
3235
import net.imagej.ops.Ops;
33-
import net.imagej.ops.Ops.Filter.Hessian;
3436
import net.imagej.ops.special.chain.RAIs;
3537
import net.imagej.ops.special.computer.UnaryComputerOp;
36-
import net.imagej.ops.special.function.Functions;
38+
import net.imagej.ops.special.function.AbstractUnaryFunctionOp;
3739
import net.imagej.ops.special.function.UnaryFunctionOp;
38-
import net.imagej.ops.special.hybrid.AbstractUnaryHybridCF;
39-
import net.imglib2.Dimensions;
40-
import net.imglib2.FinalDimensions;
4140
import net.imglib2.RandomAccessibleInterval;
4241
import net.imglib2.type.numeric.RealType;
43-
import net.imglib2.util.Util;
44-
import net.imglib2.view.IntervalView;
4542
import net.imglib2.view.Views;
43+
import net.imglib2.view.composite.CompositeIntervalView;
44+
import net.imglib2.view.composite.RealComposite;
4645

4746
import org.scijava.plugin.Plugin;
4847

@@ -57,20 +56,16 @@
5756

5857
@Plugin(type = Ops.Filter.Hessian.class)
5958
public class HessianRAI<T extends RealType<T>>
60-
extends AbstractUnaryHybridCF<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>> implements Hessian {
59+
extends AbstractUnaryFunctionOp<RandomAccessibleInterval<T>, CompositeIntervalView<T, RealComposite<T>>>
60+
implements Ops.Filter.Hessian {
6161

6262
private UnaryComputerOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>>[] derivativeComputers;
6363

64-
@SuppressWarnings("rawtypes")
65-
private UnaryFunctionOp<Dimensions, RandomAccessibleInterval> createRAIFromDim;
66-
6764
private UnaryFunctionOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>> createRAIFromRAI;
6865

6966
@SuppressWarnings("unchecked")
7067
@Override
7168
public void initialize() {
72-
createRAIFromDim = Functions.unary(ops(), Ops.Create.Img.class, RandomAccessibleInterval.class,
73-
Dimensions.class, Util.getTypeFromInterval(in()).createVariable());
7469
createRAIFromRAI = RAIs.function(ops(), Ops.Create.Img.class, in());
7570

7671
derivativeComputers = new UnaryComputerOp[in().numDimensions()];
@@ -80,29 +75,19 @@ public void initialize() {
8075
}
8176

8277
@Override
83-
public void compute1(RandomAccessibleInterval<T> input, RandomAccessibleInterval<T> output) {
84-
int iteration = 0;
85-
86-
for (int i = 0; i < input.numDimensions(); i++) {
78+
public CompositeIntervalView<T, RealComposite<T>> compute1(RandomAccessibleInterval<T> input) {
79+
List<RandomAccessibleInterval<T>> derivatives = new ArrayList<>();
80+
for (int i = 0; i < derivativeComputers.length; i++) {
8781
RandomAccessibleInterval<T> derivative = createRAIFromRAI.compute1(input);
8882
derivativeComputers[i].compute1(input, derivative);
89-
for (int j = 0; j < input.numDimensions(); j++) {
90-
IntervalView<T> out = Views.hyperSlice(Views.hyperSlice(output, 3, 0), 2, iteration);
83+
for (int j = 0; j < derivativeComputers.length; j++) {
84+
RandomAccessibleInterval<T> out = createRAIFromRAI.compute1(input);
9185
derivativeComputers[j].compute1(derivative, out);
92-
iteration++;
86+
derivatives.add(out);
9387
}
9488
}
89+
RandomAccessibleInterval<T> stackedDerivatives = Views.stack(derivatives);
90+
return Views.collapseReal(stackedDerivatives);
9591
}
9692

97-
@SuppressWarnings("unchecked")
98-
@Override
99-
public RandomAccessibleInterval<T> createOutput(RandomAccessibleInterval<T> input) {
100-
long[] dims = new long[input.numDimensions() + 2];
101-
for (int i = 0; i < dims.length - 1; i++) {
102-
dims[i] = input.dimension(i);
103-
}
104-
dims[dims.length - 1] = input.numDimensions() * input.numDimensions();
105-
Dimensions dim = FinalDimensions.wrap(dims);
106-
return createRAIFromDim.compute1(dim);
107-
}
10893
}

src/test/java/net/imagej/ops/filter/hessian/HessianFilterTest.java

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333

3434
import net.imagej.ops.AbstractOpTest;
3535
import net.imglib2.Cursor;
36-
import net.imglib2.RandomAccess;
37-
import net.imglib2.RandomAccessibleInterval;
3836
import net.imglib2.img.Img;
3937
import net.imglib2.type.numeric.real.FloatType;
4038
import net.imglib2.util.Util;
41-
import net.imglib2.view.IntervalView;
4239
import net.imglib2.view.Views;
40+
import net.imglib2.view.composite.CompositeIntervalView;
41+
import net.imglib2.view.composite.CompositeView;
42+
import net.imglib2.view.composite.RealComposite;
4343

4444
import org.junit.Test;
4545

@@ -77,49 +77,43 @@ public void test() {
7777
}
7878
}
7979

80-
RandomAccessibleInterval<FloatType> out = ops.filter().hessian(img);
81-
82-
IntervalView<FloatType> xx = Views.hyperSlice(Views.hyperSlice(out, 3, 0), 2, 0);
83-
IntervalView<FloatType> xy = Views.hyperSlice(Views.hyperSlice(out, 3, 0), 2, 1);
84-
IntervalView<FloatType> yx = Views.hyperSlice(Views.hyperSlice(out, 3, 0), 2, 2);
85-
IntervalView<FloatType> yy = Views.hyperSlice(Views.hyperSlice(out, 3, 0), 2, 3);
86-
87-
Cursor<FloatType> xyCursor = xy.cursor();
88-
Cursor<FloatType> yxCursor = yx.cursor();
89-
while (xyCursor.hasNext()) {
90-
xyCursor.fwd();
91-
yxCursor.fwd();
92-
assertEquals(xyCursor.get(), yxCursor.get());
80+
CompositeIntervalView<FloatType, RealComposite<FloatType>> out = ops.filter().hessian(img);
81+
82+
83+
Cursor<RealComposite<FloatType>> outCursor = Views.iterable(out).cursor();
84+
85+
while (outCursor.hasNext()) {
86+
RealComposite<FloatType> values = outCursor.next();
87+
assertEquals(values.get(1), values.get(2));
9388
}
89+
90+
CompositeView<FloatType, RealComposite<FloatType>>.CompositeRandomAccess outRA = out.randomAccess();
91+
9492
// two numbers represent a coordinate: 20|0 ; 21|0 ...
9593
int[] positions = new int[] { 20, 0, 21, 0, 19, 31, 19, 30 };
9694
float[] valuesXX = new float[] { 16.0f, -16.0f, 15.0f, 11.0f };
9795
float[] valuesXY = new float[] { 0.0f, 0.0f, 1.0f, 3.0f };
9896
float[] valuesYY = new float[] { 0.0f, 0.0f, 15.0f, 15.0f };
9997

100-
RandomAccess<FloatType> xxRA = xx.randomAccess();
101-
RandomAccess<FloatType> xyRA = xy.randomAccess();
102-
RandomAccess<FloatType> yyRA = yy.randomAccess();
103-
104-
FloatType type = Util.getTypeFromInterval(out).createVariable();
98+
FloatType type = Util.getTypeFromInterval(img).createVariable();
10599
int i = 0;
106100
int j = 0;
107101
while (i < positions.length - 1) {
108102
int[] pos = new int[2];
109103
pos[0] = positions[i];
110104
pos[1] = positions[i + 1];
111105

112-
xxRA.setPosition(pos);
106+
outRA.setPosition(pos);
113107
type.set(valuesXX[j]);
114-
assertEquals(type, xxRA.get());
108+
assertEquals(type, outRA.get().get(0));
115109

116-
xyRA.setPosition(pos);
110+
outRA.setPosition(pos);
117111
type.set(valuesXY[j]);
118-
assertEquals(type, xyRA.get());
112+
assertEquals(type, outRA.get().get(1));
119113

120-
yyRA.setPosition(pos);
114+
outRA.setPosition(pos);
121115
type.set(valuesYY[j]);
122-
assertEquals(type, yyRA.get());
116+
assertEquals(type, outRA.get().get(3));
123117

124118
i += 2;
125119
j++;

0 commit comments

Comments
 (0)