Skip to content

Commit 39b0d21

Browse files
wiedenmctrueden
authored andcommitted
WIP: Port threshold
1 parent 6f2f14b commit 39b0d21

45 files changed

Lines changed: 6320 additions & 691 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2014 - 2018 ImageJ developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package net.imagej.ops.filter;
31+
32+
import net.imglib2.Cursor;
33+
import net.imglib2.IterableInterval;
34+
import net.imglib2.RandomAccess;
35+
import net.imglib2.RandomAccessibleInterval;
36+
import net.imglib2.algorithm.neighborhood.Shape;
37+
import net.imglib2.outofbounds.OutOfBoundsBorderFactory;
38+
import net.imglib2.outofbounds.OutOfBoundsFactory;
39+
import net.imglib2.view.Views;
40+
41+
import org.scijava.ops.core.computer.BiComputer;
42+
import org.scijava.param.Mutable;
43+
44+
public abstract class AbstractCenterAwareNeighborhoodBasedFilter<I, O> {
45+
46+
private static final OutOfBoundsFactory<?, ?> DEFAULT_OUT_OF_BOUNDS_FACTORY =
47+
new OutOfBoundsBorderFactory<>();
48+
49+
@SuppressWarnings("unchecked")
50+
public static <I> OutOfBoundsFactory<I, RandomAccessibleInterval<I>>
51+
defaultOutOfBoundsFactory()
52+
{
53+
return (OutOfBoundsFactory<I, RandomAccessibleInterval<I>>) DEFAULT_OUT_OF_BOUNDS_FACTORY;
54+
}
55+
56+
protected void computeInternal(final RandomAccessibleInterval<I> input,
57+
final Shape inputNeighborhoodShape,
58+
OutOfBoundsFactory<I, RandomAccessibleInterval<I>> outOfBoundsFactory,
59+
final BiComputer<Iterable<I>, I, O> filterOp,
60+
@Mutable final IterableInterval<O> output)
61+
{
62+
if (outOfBoundsFactory == null) outOfBoundsFactory =
63+
defaultOutOfBoundsFactory();
64+
final RandomAccessibleInterval<I> inputCenterPixels = Views.interval(Views
65+
.extend(input, outOfBoundsFactory), input);
66+
final IterableInterval<? extends Iterable<I>> inputNeighborhoods =
67+
inputNeighborhoodShape.neighborhoodsSafe(inputCenterPixels);
68+
map(inputNeighborhoods, inputCenterPixels, filterOp, output);
69+
}
70+
71+
private static <I1, I2, O> void map(
72+
final IterableInterval<? extends I1> inputNeighborhoods,
73+
final RandomAccessibleInterval<I2> inputCenterPixels,
74+
final BiComputer<I1, I2, O> filterOp, final IterableInterval<O> output)
75+
{
76+
// TODO: This used to be done via a net.imagej.ops.Ops.Map meta op. We may
77+
// want to revert to that approach if this proves to be too inflexible.
78+
// (Parallelization would be useful, for instance.)
79+
final Cursor<? extends I1> neighborhoodCursor = inputNeighborhoods
80+
.localizingCursor();
81+
final RandomAccess<I2> centerPixelsAccess = inputCenterPixels
82+
.randomAccess();
83+
final Cursor<O> outputCursor = output.cursor();
84+
while (neighborhoodCursor.hasNext()) {
85+
neighborhoodCursor.fwd();
86+
centerPixelsAccess.setPosition(neighborhoodCursor);
87+
filterOp.compute(neighborhoodCursor.get(), centerPixelsAccess.get(),
88+
outputCursor.next());
89+
}
90+
}
91+
92+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2014 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison and University of Konstanz.
7+
* %%
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright notice,
12+
* this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright notice,
14+
* this list of conditions and the following disclaimer in the documentation
15+
* and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
* POSSIBILITY OF SUCH DAMAGE.
28+
* #L%
29+
*/
30+
31+
package net.imagej.ops.threshold;
32+
33+
import java.util.function.Function;
34+
35+
import net.imglib2.histogram.Histogram1d;
36+
import net.imglib2.type.numeric.RealType;
37+
38+
import org.scijava.ops.OpDependency;
39+
import org.scijava.ops.core.computer.Computer;
40+
41+
/**
42+
* @author Curtis Rueden
43+
* @author Christian Dietz (University of Konstanz)
44+
*/
45+
public abstract class AbstractApplyThresholdImg<T extends RealType<T>> extends
46+
AbstractApplyThresholdIterable<T>
47+
{
48+
49+
@OpDependency(name = "image.histogram")
50+
private Function<Iterable<T>, Histogram1d<T>> createHistogramOp;
51+
52+
@Override
53+
protected T computeThreshold(final Iterable<T> input) {
54+
final Histogram1d<T> inputHistogram = createHistogramOp.apply(input);
55+
final T threshold = input.iterator().next().createVariable();
56+
final Computer<Histogram1d<T>, T> computeThresholdOp =
57+
getComputeThresholdOp();
58+
computeThresholdOp.compute(inputHistogram, threshold);
59+
return threshold;
60+
}
61+
62+
protected abstract Computer<Histogram1d<T>, T> getComputeThresholdOp();
63+
64+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2014 - 2018 ImageJ developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package net.imagej.ops.threshold;
31+
32+
import net.imglib2.type.logic.BitType;
33+
34+
import org.scijava.ops.OpDependency;
35+
import org.scijava.ops.core.computer.BiComputer;
36+
import org.scijava.ops.core.computer.Computer;
37+
import org.scijava.param.Mutable;
38+
import org.scijava.param.Parameter;
39+
import org.scijava.struct.ItemIO;
40+
41+
/**
42+
* @author Curtis Rueden
43+
* @author Christian Dietz (University of Konstanz)
44+
*/
45+
@Parameter(key = "input")
46+
@Parameter(key = "output", type = ItemIO.BOTH)
47+
public abstract class AbstractApplyThresholdIterable<T> implements
48+
Computer<Iterable<T>, Iterable<BitType>>
49+
{
50+
51+
@OpDependency(name = "threshold.apply")
52+
private BiComputer<Iterable<T>, T, Iterable<BitType>> applyThresholdOp;
53+
54+
@Override
55+
public void compute(final Iterable<T> input,
56+
@Mutable final Iterable<BitType> output)
57+
{
58+
applyThresholdOp.compute(input, computeThreshold(input), output);
59+
}
60+
61+
protected abstract T computeThreshold(Iterable<T> input);
62+
}

src/main/java/net/imagej/ops/threshold/AbstractComputeThresholdHistogram.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* %%
77
* Redistribution and use in source and binary forms, with or without
88
* modification, are permitted provided that the following conditions are met:
9-
*
9+
*
1010
* 1. Redistributions of source code must retain the above copyright notice,
1111
* this list of conditions and the following disclaimer.
1212
* 2. Redistributions in binary form must reproduce the above copyright notice,
1313
* this list of conditions and the following disclaimer in the documentation
1414
* and/or other materials provided with the distribution.
15-
*
15+
*
1616
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1717
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1818
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -42,13 +42,14 @@
4242
public abstract class AbstractComputeThresholdHistogram<T extends RealType<T>>
4343
implements Computer<Histogram1d<T>, T>
4444
{
45+
4546
@Override
4647
public void compute(final Histogram1d<T> input, final T output) {
4748
final long binPos = computeBin(input);
4849

4950
// convert bin number to corresponding gray level
5051
input.getCenterValue(binPos, output);
5152
}
52-
53-
abstract protected long computeBin(final Histogram1d<T> input);
53+
54+
protected abstract long computeBin(final Histogram1d<T> input);
5455
}

0 commit comments

Comments
 (0)