Skip to content

Commit c81ad7a

Browse files
committed
Second cut: Improved Thresholding
1 parent b44f7bb commit c81ad7a

5 files changed

Lines changed: 69 additions & 62 deletions

File tree

scijava-ops-image/src/main/java/org/scijava/ops/image/create/Creators.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import net.imglib2.type.logic.BitType;
5555
import net.imglib2.type.numeric.ComplexType;
5656
import net.imglib2.type.numeric.IntegerType;
57+
import net.imglib2.type.numeric.RealType;
5758
import net.imglib2.type.numeric.complex.ComplexDoubleType;
5859
import net.imglib2.type.numeric.complex.ComplexFloatType;
5960
import net.imglib2.type.numeric.integer.ByteType;
@@ -72,6 +73,7 @@
7273
import org.joml.Vector3f;
7374
import org.scijava.function.Functions;
7475
import org.scijava.function.Producer;
76+
import org.scijava.ops.spi.OpDependency;
7577

7678
public class Creators<N extends NativeType<N>, L, I extends IntegerType<I>, T extends Type<T>, C extends ComplexType<C>, W extends ComplexType<W> & NativeType<W>, B extends BooleanType<B>, A extends ArrayDataAccess<A>> {
7779

@@ -196,16 +198,19 @@ public class Creators<N extends NativeType<N>, L, I extends IntegerType<I>, T ex
196198
.factory(), img, img.firstElement());
197199

198200
/**
199-
* @input interval
200-
* @output img
201-
* @implNote op names='create.img, engine.create', priority='-100.'
201+
* @param typeCreator a {@link Producer} that knows how to create the resulting image type.
202+
* @param interval the interval of the resulting {@link Img}
203+
* @return an {@link Img}
204+
* @implNote op names='create.img, engine.create', priority='-1000.'
202205
*/
203-
public final Function<Interval, Img<DoubleType>> imgFromInterval = (
204-
interval) -> {
205-
var type = new DoubleType();
206+
public static <T extends Type<T>> Img<T> imgFromInterval( //
207+
@OpDependency(name="engine.create") Producer<T> typeCreator, //
208+
Interval interval //
209+
) {
210+
var type = typeCreator.create();
206211
return Imgs.create(Util.getSuitableImgFactory(interval, type), interval,
207-
type);
208-
};
212+
type);
213+
}
209214

210215
/**
211216
* @input rai

scijava-ops-image/src/main/java/org/scijava/ops/image/threshold/AbstractApplyThresholdImg.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.function.Function;
3333

3434
import net.imglib2.histogram.Histogram1d;
35+
import net.imglib2.type.logic.BitType;
3536
import net.imglib2.type.numeric.RealType;
3637

3738
import org.scijava.function.Computers;
@@ -41,15 +42,15 @@
4142
* @author Curtis Rueden
4243
* @author Christian Dietz (University of Konstanz)
4344
*/
44-
public abstract class AbstractApplyThresholdImg<T extends RealType<T>> extends
45-
AbstractApplyThresholdIterable<T>
45+
public abstract class AbstractApplyThresholdImg<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
46+
AbstractApplyThresholdIterable<T, U, V>
4647
{
4748

4849
@OpDependency(name = "image.histogram")
49-
private Function<Iterable<T>, Histogram1d<T>> createHistogramOp;
50+
private Function<U, Histogram1d<T>> createHistogramOp;
5051

5152
@Override
52-
protected T computeThreshold(final Iterable<T> input) {
53+
protected T computeThreshold(final U input) {
5354
final var inputHistogram = createHistogramOp.apply(input);
5455
final var threshold = input.iterator().next().createVariable();
5556
final var computeThresholdOp =

scijava-ops-image/src/main/java/org/scijava/ops/image/threshold/AbstractApplyThresholdIterable.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838
* @author Curtis Rueden
3939
* @author Christian Dietz (University of Konstanz)
4040
*/
41-
public abstract class AbstractApplyThresholdIterable<T> implements
42-
Computers.Arity1<Iterable<T>, Iterable<BitType>>
41+
public abstract class AbstractApplyThresholdIterable<T, U extends Iterable<T>, V extends Iterable<BitType>> implements
42+
Computers.Arity1<U, V>
4343
{
4444

4545
@OpDependency(name = "threshold.apply")
46-
private Computers.Arity2<Iterable<T>, T, Iterable<BitType>> applyThresholdOp;
46+
private Computers.Arity2<U, T, V> applyThresholdOp;
4747

4848
/**
4949
* TODO
@@ -52,9 +52,9 @@ public abstract class AbstractApplyThresholdIterable<T> implements
5252
* @param output
5353
*/
5454
@Override
55-
public void compute(final Iterable<T> input, final Iterable<BitType> output) {
55+
public void compute(final U input, final V output) {
5656
applyThresholdOp.compute(input, computeThreshold(input), output);
5757
}
5858

59-
protected abstract T computeThreshold(Iterable<T> input);
59+
protected abstract T computeThreshold(U input);
6060
}

scijava-ops-image/src/main/java/org/scijava/ops/image/threshold/ApplyThresholdMethod.java

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package org.scijava.ops.image.threshold;
3131

3232
import net.imglib2.histogram.Histogram1d;
33+
import net.imglib2.type.logic.BitType;
3334
import net.imglib2.type.numeric.RealType;
3435

3536
import org.scijava.function.Computers;
@@ -51,8 +52,8 @@ private ApplyThresholdMethod() {
5152
/**
5253
* @implNote op names='threshold.huang'
5354
*/
54-
public static class Huang<T extends RealType<T>> extends
55-
AbstractApplyThresholdImg<T>
55+
public static class Huang<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
56+
AbstractApplyThresholdImg<T, U, V>
5657
{
5758

5859
@OpDependency(name = "threshold.huang")
@@ -67,8 +68,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
6768
/**
6869
* @implNote op names='threshold.ij1'
6970
*/
70-
public static class IJ1<T extends RealType<T>> extends
71-
AbstractApplyThresholdImg<T>
71+
public static class IJ1<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
72+
AbstractApplyThresholdImg<T, U, V>
7273
{
7374

7475
@OpDependency(name = "threshold.ij1")
@@ -83,8 +84,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
8384
/**
8485
* @implNote op names='threshold.intermodes'
8586
*/
86-
public static class Intermodes<T extends RealType<T>> extends
87-
AbstractApplyThresholdImg<T>
87+
public static class Intermodes<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
88+
AbstractApplyThresholdImg<T, U, V>
8889
{
8990

9091
@OpDependency(name = "threshold.intermodes")
@@ -99,8 +100,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
99100
/**
100101
* @implNote op names='threshold.isoData'
101102
*/
102-
public static class IsoData<T extends RealType<T>> extends
103-
AbstractApplyThresholdImg<T>
103+
public static class IsoData<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
104+
AbstractApplyThresholdImg<T, U, V>
104105
{
105106

106107
@OpDependency(name = "threshold.isoData")
@@ -115,8 +116,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
115116
/**
116117
* @implNote op names='threshold.li'
117118
*/
118-
public static class Li<T extends RealType<T>> extends
119-
AbstractApplyThresholdImg<T>
119+
public static class Li<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
120+
AbstractApplyThresholdImg<T, U, V>
120121
{
121122

122123
@OpDependency(name = "threshold.li")
@@ -131,8 +132,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
131132
/**
132133
* @implNote op names='threshold.maxEntropy'
133134
*/
134-
public static class MaxEntropy<T extends RealType<T>> extends
135-
AbstractApplyThresholdImg<T>
135+
public static class MaxEntropy<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
136+
AbstractApplyThresholdImg<T, U, V>
136137
{
137138

138139
@OpDependency(name = "threshold.maxEntropy")
@@ -147,8 +148,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
147148
/**
148149
* @implNote op names='threshold.maxLikelihood'
149150
*/
150-
public static class MaxLikelihood<T extends RealType<T>> extends
151-
AbstractApplyThresholdImg<T>
151+
public static class MaxLikelihood<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
152+
AbstractApplyThresholdImg<T, U, V>
152153
{
153154

154155
@OpDependency(name = "threshold.maxLikelihood")
@@ -163,8 +164,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
163164
/**
164165
* @implNote op names='threshold.mean'
165166
*/
166-
public static class Mean<T extends RealType<T>> extends
167-
AbstractApplyThresholdImg<T>
167+
public static class Mean<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
168+
AbstractApplyThresholdImg<T, U, V>
168169
{
169170

170171
@OpDependency(name = "threshold.mean")
@@ -179,8 +180,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
179180
/**
180181
* @implNote op names='threshold.minError'
181182
*/
182-
public static class MinError<T extends RealType<T>> extends
183-
AbstractApplyThresholdImg<T>
183+
public static class MinError<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
184+
AbstractApplyThresholdImg<T, U, V>
184185
{
185186

186187
@OpDependency(name = "threshold.minError")
@@ -195,8 +196,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
195196
/**
196197
* @implNote op names='threshold.minimum'
197198
*/
198-
public static class Minimum<T extends RealType<T>> extends
199-
AbstractApplyThresholdImg<T>
199+
public static class Minimum<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
200+
AbstractApplyThresholdImg<T, U, V>
200201
{
201202

202203
@OpDependency(name = "threshold.minimum")
@@ -211,8 +212,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
211212
/**
212213
* @implNote op names='threshold.moments'
213214
*/
214-
public static class Moments<T extends RealType<T>> extends
215-
AbstractApplyThresholdImg<T>
215+
public static class Moments<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
216+
AbstractApplyThresholdImg<T, U, V>
216217
{
217218

218219
@OpDependency(name = "threshold.moments")
@@ -227,8 +228,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
227228
/**
228229
* @implNote op names='threshold.otsu'
229230
*/
230-
public static class Otsu<T extends RealType<T>> extends
231-
AbstractApplyThresholdImg<T>
231+
public static class Otsu<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
232+
AbstractApplyThresholdImg<T, U, V>
232233
{
233234

234235
@OpDependency(name = "threshold.otsu")
@@ -243,8 +244,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
243244
/**
244245
* @implNote op names='threshold.percentile'
245246
*/
246-
public static class Percentile<T extends RealType<T>> extends
247-
AbstractApplyThresholdImg<T>
247+
public static class Percentile<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
248+
AbstractApplyThresholdImg<T, U, V>
248249
{
249250

250251
@OpDependency(name = "threshold.percentile")
@@ -259,8 +260,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
259260
/**
260261
* @implNote op names='threshold.renyiEntropy'
261262
*/
262-
public static class RenyiEntropy<T extends RealType<T>> extends
263-
AbstractApplyThresholdImg<T>
263+
public static class RenyiEntropy<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
264+
AbstractApplyThresholdImg<T, U, V>
264265
{
265266

266267
@OpDependency(name = "threshold.renyiEntropy")
@@ -275,8 +276,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
275276
/**
276277
* @implNote op names='threshold.rosin'
277278
*/
278-
public static class Rosin<T extends RealType<T>> extends
279-
AbstractApplyThresholdImg<T>
279+
public static class Rosin<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
280+
AbstractApplyThresholdImg<T, U, V>
280281
{
281282

282283
@OpDependency(name = "threshold.rosin")
@@ -291,8 +292,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
291292
/**
292293
* @implNote op names='threshold.shanbhag'
293294
*/
294-
public static class Shanbhag<T extends RealType<T>> extends
295-
AbstractApplyThresholdImg<T>
295+
public static class Shanbhag<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
296+
AbstractApplyThresholdImg<T, U, V>
296297
{
297298

298299
@OpDependency(name = "threshold.shanbhag")
@@ -307,8 +308,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
307308
/**
308309
* @implNote op names='threshold.triangle'
309310
*/
310-
public static class Triangle<T extends RealType<T>> extends
311-
AbstractApplyThresholdImg<T>
311+
public static class Triangle<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
312+
AbstractApplyThresholdImg<T, U, V>
312313
{
313314

314315
@OpDependency(name = "threshold.triangle")
@@ -323,8 +324,8 @@ protected Computers.Arity1<Histogram1d<T>, T> getComputeThresholdOp() {
323324
/**
324325
* @implNote op names='threshold.yen'
325326
*/
326-
public static class Yen<T extends RealType<T>> extends
327-
AbstractApplyThresholdImg<T>
327+
public static class Yen<T extends RealType<T>, U extends Iterable<T>, V extends Iterable<BitType>> extends
328+
AbstractApplyThresholdImg<T, U, V>
328329
{
329330

330331
@OpDependency(name = "threshold.yen")

scijava-ops-image/src/test/java/org/scijava/ops/image/threshold/apply/ApplyManualThresholdTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
package org.scijava.ops.image.threshold.apply;
3131

3232
import java.util.Comparator;
33+
import java.util.Random;
3334

35+
import net.imglib2.RandomAccessibleInterval;
3436
import org.scijava.ops.image.threshold.AbstractThresholdTest;
3537
import net.imglib2.exception.IncompatibleTypeException;
3638
import net.imglib2.img.Img;
@@ -51,19 +53,17 @@ public class ApplyManualThresholdTest extends AbstractThresholdTest {
5153

5254
@Test
5355
public void testApplyThreshold() throws IncompatibleTypeException {
54-
Computers.Arity3<Img<UnsignedShortType>, UnsignedShortType, Comparator<UnsignedShortType>, Iterable<BitType>> createFunc =
55-
OpBuilder.matchComputer(ops, "threshold.apply",
56-
new Nil<Img<UnsignedShortType>>()
57-
{}, new Nil<UnsignedShortType>() {},
58-
new Nil<Comparator<UnsignedShortType>>()
59-
{}, new Nil<Iterable<BitType>>() {});
60-
61-
final Img<BitType> out = bitmap();
6256
final UnsignedShortType threshold = new UnsignedShortType(30000);
6357
Comparator<UnsignedShortType> comparator = (c1, c2) -> (int) Math.ceil(c1
6458
.getRealDouble() - c2.getRealDouble());
65-
createFunc.compute(in, threshold, comparator, out);
59+
final Img<BitType> out = bitmap();
60+
ops.op("threshold.apply").input(in, threshold, comparator).output(out).compute();
6661
assertCount(out, 54);
6762
}
6863

64+
@Test
65+
public void testApplyThresholdMethod() {
66+
ops.op("threshold.mean").input(in).outType(new Nil<RandomAccessibleInterval<BitType>>() {}).apply();
67+
}
68+
6969
}

0 commit comments

Comments
 (0)