Skip to content

Commit 8b6d89d

Browse files
committed
Beautify Watershed Ops
1 parent a8cabdc commit 8b6d89d

8 files changed

Lines changed: 288 additions & 357 deletions

File tree

scijava-ops-image/src/main/java/org/scijava/ops/image/image/watershed/Watershed.java

Lines changed: 13 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
import net.imglib2.util.Intervals;
5454
import net.imglib2.view.ExtendedRandomAccessibleInterval;
5555
import net.imglib2.view.Views;
56-
5756
import org.scijava.function.Computers;
5857
import org.scijava.function.Functions;
58+
import org.scijava.ops.spi.Nullable;
5959
import org.scijava.ops.spi.OpDependency;
6060

6161
/**
@@ -92,9 +92,6 @@
9292
public class Watershed<T extends RealType<T>, B extends BooleanType<B>> implements
9393
Computers.Arity4<RandomAccessibleInterval<T>, Boolean, Boolean, RandomAccessibleInterval<B>, ImgLabeling<Integer, IntType>> {
9494

95-
// @SuppressWarnings("rawtypes")
96-
// private UnaryFunctionOp<Interval, ImgLabeling> createOp;
97-
9895
@OpDependency(name = "create.img")
9996
BiFunction<Dimensions, IntType, RandomAccessibleInterval<IntType>> imgCreator;
10097

@@ -110,16 +107,20 @@ public class Watershed<T extends RealType<T>, B extends BooleanType<B>> implemen
110107
/**
111108
* TODO
112109
*
113-
* @param input
110+
* @param in
114111
* @param useEightConnectivity
115112
* @param drawWatersheds
116113
* @param mask
117114
* @param outputLabeling
118115
*/
119116
@Override
120-
public void compute(final RandomAccessibleInterval<T> in, final Boolean useEightConnectivity,
121-
final Boolean drawWatersheds, final RandomAccessibleInterval<B> mask,
122-
final ImgLabeling<Integer, IntType> out) {
117+
public void compute(
118+
final RandomAccessibleInterval<T> in, //
119+
final Boolean useEightConnectivity, //
120+
final Boolean drawWatersheds, //
121+
@Nullable final RandomAccessibleInterval<B> mask, //
122+
final ImgLabeling<Integer, IntType> outputLabeling //
123+
) {
123124
final RandomAccess<T> raIn = in.randomAccess();
124125

125126
RandomAccess<B> raMask = null;
@@ -327,7 +328,7 @@ public void compute(final RandomAccessibleInterval<T> in, final Boolean useEight
327328
/*
328329
* Draw output and remove as the case may be the watersheds
329330
*/
330-
final Cursor<LabelingType<Integer>> cursorOut = out.cursor();
331+
final Cursor<LabelingType<Integer>> cursorOut = outputLabeling.cursor();
331332
while (cursorOut.hasNext()) {
332333
cursorOut.fwd();
333334
boolean maskValue = true;
@@ -369,9 +370,9 @@ public void compute(final RandomAccessibleInterval<T> in, final Boolean useEight
369370
/*
370371
* Merge already present labels before calculation of watershed
371372
*/
372-
if (out != null) {
373-
final Cursor<LabelingType<Integer>> cursor = out.cursor();
374-
final RandomAccess<LabelingType<Integer>> raOut = out.randomAccess();
373+
if (outputLabeling != null) {
374+
final Cursor<LabelingType<Integer>> cursor = outputLabeling.cursor();
375+
final RandomAccess<LabelingType<Integer>> raOut = outputLabeling.randomAccess();
375376
while (cursor.hasNext()) {
376377
cursor.fwd();
377378
raOut.setPosition(cursor);
@@ -384,84 +385,3 @@ public void compute(final RandomAccessibleInterval<T> in, final Boolean useEight
384385

385386
}
386387

387-
/**
388-
*@implNote op names='image.watershed'
389-
*/
390-
class WatershedMaskless<T extends RealType<T>, B extends BooleanType<B>> implements
391-
Computers.Arity3<RandomAccessibleInterval<T>, Boolean, Boolean, ImgLabeling<Integer, IntType>> {
392-
393-
@OpDependency(name = "image.watershed")
394-
private Computers.Arity4<RandomAccessibleInterval<T>, Boolean, Boolean, RandomAccessibleInterval<B>, ImgLabeling<Integer, IntType>> watershedOp;
395-
396-
/**
397-
* TODO
398-
*
399-
* @param input
400-
* @param useEightConnectivity
401-
* @param drawWatersheds
402-
* @param outputLabeling
403-
*/
404-
@Override
405-
public void compute(RandomAccessibleInterval<T> in, Boolean useEightConnectivity, Boolean drawWatersheds,
406-
ImgLabeling<Integer, IntType> outputLabeling) {
407-
watershedOp.compute(in, useEightConnectivity, drawWatersheds, null, outputLabeling);
408-
409-
}
410-
}
411-
412-
/**
413-
*@implNote op names='image.watershed'
414-
*/
415-
class WatershedFunction<T extends RealType<T>, B extends BooleanType<B>>
416-
implements Functions.Arity4<RandomAccessibleInterval<T>, Boolean, Boolean, RandomAccessibleInterval<B>, ImgLabeling<Integer, IntType>> {
417-
418-
@OpDependency(name = "image.watershed")
419-
private Computers.Arity4<RandomAccessibleInterval<T>, Boolean, Boolean, RandomAccessibleInterval<B>, ImgLabeling<Integer, IntType>> watershedOp;
420-
@OpDependency(name = "create.imgLabeling")
421-
private BiFunction<Dimensions, IntType, ImgLabeling<Integer, IntType>> labelingCreator;
422-
423-
/**
424-
* TODO
425-
*
426-
* @param input
427-
* @param useEightConnectivity
428-
* @param drawWatersheds
429-
* @param mask
430-
* @return the outputLabeling
431-
*/
432-
@Override
433-
public ImgLabeling<Integer, IntType> apply(RandomAccessibleInterval<T> in, Boolean useEightConnectivity,
434-
Boolean drawWatersheds, RandomAccessibleInterval<B> mask) {
435-
ImgLabeling<Integer, IntType> outputLabeling = labelingCreator.apply(in, new IntType());
436-
watershedOp.compute(in, useEightConnectivity, drawWatersheds, mask, outputLabeling);
437-
return outputLabeling;
438-
}
439-
}
440-
441-
/**
442-
*@implNote op names='image.watershed'
443-
*/
444-
class WatershedFunctionMaskless<T extends RealType<T>, B extends BooleanType<B>>
445-
implements Functions.Arity3<RandomAccessibleInterval<T>, Boolean, Boolean, ImgLabeling<Integer, IntType>> {
446-
447-
@OpDependency(name = "image.watershed")
448-
private Computers.Arity3<RandomAccessibleInterval<T>, Boolean, Boolean, ImgLabeling<Integer, IntType>> watershedOp;
449-
@OpDependency(name = "create.imgLabeling")
450-
private BiFunction<Dimensions, IntType, ImgLabeling<Integer, IntType>> labelingCreator;
451-
452-
/**
453-
* TODO
454-
*
455-
* @param input
456-
* @param useEightConnectivity
457-
* @param drawWatersheds
458-
* @return the outputLabeling
459-
*/
460-
@Override
461-
public ImgLabeling<Integer, IntType> apply(RandomAccessibleInterval<T> in, Boolean useEightConnectivity,
462-
Boolean drawWatersheds) {
463-
ImgLabeling<Integer, IntType> outputLabeling = labelingCreator.apply(in, new IntType());
464-
watershedOp.compute(in, useEightConnectivity, drawWatersheds, outputLabeling);
465-
return outputLabeling;
466-
}
467-
}

scijava-ops-image/src/main/java/org/scijava/ops/image/image/watershed/WatershedBinary.java

Lines changed: 9 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import org.scijava.function.Computers;
3434
import org.scijava.function.Functions;
35+
import org.scijava.ops.spi.Nullable;
3536
import org.scijava.ops.spi.OpDependency;
3637

3738
import net.imglib2.Dimensions;
@@ -105,9 +106,14 @@ public class WatershedBinary<T extends BooleanType<T>, B extends BooleanType<B>>
105106
* @param out
106107
*/
107108
@Override
108-
public void compute(final RandomAccessibleInterval<T> in, final Boolean useEightConnectivity,
109-
final Boolean drawWatersheds, final double[] sigma, final RandomAccessibleInterval<B> mask,
110-
final ImgLabeling<Integer, IntType> out) {
109+
public void compute( //
110+
final RandomAccessibleInterval<T> in, //
111+
final Boolean useEightConnectivity, //
112+
final Boolean drawWatersheds, //
113+
final double[] sigma, //
114+
@Nullable RandomAccessibleInterval<B> mask, //
115+
final ImgLabeling<Integer, IntType> out //
116+
) {
111117

112118
// make sure that the params conform to the requirements of the op (copied from
113119
// the old implementation)
@@ -136,87 +142,3 @@ public void compute(final RandomAccessibleInterval<T> in, final Boolean useEight
136142

137143
}
138144

139-
/**
140-
*@implNote op names='image.watershed'
141-
*/
142-
class WatershedBinaryMaskless<T extends BooleanType<T>, B extends BooleanType<B>> implements
143-
Computers.Arity4<RandomAccessibleInterval<T>, Boolean, Boolean, double[], ImgLabeling<Integer, IntType>> {
144-
145-
@OpDependency(name = "image.watershed")
146-
private Computers.Arity5<RandomAccessibleInterval<T>, Boolean, Boolean, double[], RandomAccessibleInterval<B>, ImgLabeling<Integer, IntType>> watershedOp;
147-
148-
/**
149-
* TODO
150-
*
151-
* @param in
152-
* @param useEightConnectivity
153-
* @param drawWatersheds
154-
* @param sigma
155-
* @param outputLabeling
156-
*/
157-
@Override
158-
public void compute(RandomAccessibleInterval<T> in, Boolean useEightConnectivity, Boolean drawWatersheds,
159-
double[] sigma, ImgLabeling<Integer, IntType> outputLabeling) {
160-
watershedOp.compute(in, useEightConnectivity, drawWatersheds, sigma, null, outputLabeling);
161-
162-
}
163-
}
164-
165-
/**
166-
*@implNote op names='image.watershed'
167-
*/
168-
class WatershedBinaryFunction<T extends BooleanType<T>, B extends BooleanType<B>> implements
169-
Functions.Arity5<RandomAccessibleInterval<T>, Boolean, Boolean, double[], RandomAccessibleInterval<B>, ImgLabeling<Integer, IntType>> {
170-
171-
@OpDependency(name = "image.watershed")
172-
private Computers.Arity5<RandomAccessibleInterval<T>, Boolean, Boolean, double[], RandomAccessibleInterval<B>, ImgLabeling<Integer, IntType>> watershedOp;
173-
@OpDependency(name = "create.imgLabeling")
174-
private BiFunction<Dimensions, IntType, ImgLabeling<Integer, IntType>> labelingCreator;
175-
176-
/**
177-
* TODO
178-
*
179-
* @param in
180-
* @param useEightConnectivity
181-
* @param drawWatersheds
182-
* @param sigma
183-
* @param mask
184-
* @return the outputLabeling
185-
*/
186-
@Override
187-
public ImgLabeling<Integer, IntType> apply(RandomAccessibleInterval<T> in, Boolean useEightConnectivity,
188-
Boolean drawWatersheds, double[] sigma, RandomAccessibleInterval<B> mask) {
189-
ImgLabeling<Integer, IntType> outputLabeling = labelingCreator.apply(in, new IntType());
190-
watershedOp.compute(in, useEightConnectivity, drawWatersheds, sigma, mask, outputLabeling);
191-
return outputLabeling;
192-
}
193-
}
194-
195-
/**
196-
*@implNote op names='image.watershed'
197-
*/
198-
class WatershedBinaryFunctionMaskless<T extends BooleanType<T>, B extends BooleanType<B>>
199-
implements Functions.Arity4<RandomAccessibleInterval<T>, Boolean, Boolean, double[], ImgLabeling<Integer, IntType>> {
200-
201-
@OpDependency(name = "image.watershed")
202-
private Computers.Arity4<RandomAccessibleInterval<T>, Boolean, Boolean, double[], ImgLabeling<Integer, IntType>> watershedOp;
203-
@OpDependency(name = "create.imgLabeling")
204-
private BiFunction<Dimensions, IntType, ImgLabeling<Integer, IntType>> labelingCreator;
205-
206-
/**
207-
* TODO
208-
*
209-
* @param in
210-
* @param useEightConnectivity
211-
* @param drawWatersheds
212-
* @param sigma
213-
* @return the outputLabeling
214-
*/
215-
@Override
216-
public ImgLabeling<Integer, IntType> apply(RandomAccessibleInterval<T> in, Boolean useEightConnectivity,
217-
Boolean drawWatersheds, double[] sigma) {
218-
ImgLabeling<Integer, IntType> outputLabeling = labelingCreator.apply(in, new IntType());
219-
watershedOp.compute(in, useEightConnectivity, drawWatersheds, sigma, outputLabeling);
220-
return outputLabeling;
221-
}
222-
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.scijava.ops.image.image.watershed;
2+
3+
import java.util.function.BiFunction;
4+
5+
import net.imglib2.Dimensions;
6+
import net.imglib2.RandomAccessibleInterval;
7+
import net.imglib2.roi.labeling.ImgLabeling;
8+
import net.imglib2.type.BooleanType;
9+
import net.imglib2.type.numeric.integer.IntType;
10+
import org.scijava.function.Computers;
11+
import org.scijava.function.Functions;
12+
import org.scijava.ops.spi.Nullable;
13+
import org.scijava.ops.spi.OpDependency;
14+
15+
/**
16+
* <p>
17+
* The Watershed algorithm segments and labels a grayscale image analogous to a
18+
* heightmap. In short, a drop of water following the gradient of an image flows
19+
* along a path to finally reach a local minimum.
20+
* </p>
21+
* This Op wraps {@link WatershedBinary} as a Function for convenience.
22+
*
23+
* @author Gabriel Selzer
24+
* @implNote op names='image.watershed'
25+
*/
26+
public class WatershedBinaryFunction<T extends BooleanType<T>, B extends BooleanType<B>>
27+
implements
28+
Functions.Arity5<RandomAccessibleInterval<T>, Boolean, Boolean, double[], RandomAccessibleInterval<B>, ImgLabeling<Integer, IntType>>
29+
{
30+
31+
@OpDependency(name = "image.watershed")
32+
private Computers.Arity5<RandomAccessibleInterval<T>, Boolean, Boolean, double[], RandomAccessibleInterval<B>, ImgLabeling<Integer, IntType>>
33+
watershedOp;
34+
@OpDependency(name = "create.imgLabeling")
35+
private BiFunction<Dimensions, IntType, ImgLabeling<Integer, IntType>>
36+
labelingCreator;
37+
38+
/**
39+
* TODO
40+
*
41+
* @param in
42+
* @param useEightConnectivity
43+
* @param drawWatersheds
44+
* @param sigma
45+
* @param mask
46+
* @return the outputLabeling
47+
*/
48+
@Override public ImgLabeling<Integer, IntType> apply( //
49+
RandomAccessibleInterval<T> in, //
50+
Boolean useEightConnectivity, //
51+
Boolean drawWatersheds, //
52+
double[] sigma, //
53+
@Nullable RandomAccessibleInterval<B> mask //
54+
)
55+
{
56+
ImgLabeling<Integer, IntType> outputLabeling =
57+
labelingCreator.apply(in, new IntType());
58+
watershedOp.compute(in, useEightConnectivity, drawWatersheds, sigma, mask,
59+
outputLabeling);
60+
return outputLabeling;
61+
}
62+
}

0 commit comments

Comments
 (0)