Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 86 additions & 13 deletions src/main/java/net/imagej/ops/AbstractOpEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@
import net.imagej.ops.stats.StatsNamespace;
import net.imagej.ops.thread.ThreadNamespace;
import net.imagej.ops.threshold.ThresholdNamespace;
import net.imglib2.Interval;
import net.imglib2.IterableInterval;
import net.imglib2.RandomAccessible;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.neighborhood.RectangleShape;
import net.imglib2.algorithm.neighborhood.Shape;
import net.imglib2.img.array.ArrayImg;
import net.imglib2.outofbounds.OutOfBoundsFactory;
import net.imglib2.type.NativeType;
import net.imglib2.type.Type;
import net.imglib2.type.numeric.RealType;

Expand Down Expand Up @@ -181,8 +186,9 @@ public <I extends RealType<I>, O extends RealType<O>> O convert(final O out,
{
@SuppressWarnings("unchecked")
final IterableInterval<O> result =
(IterableInterval<O>) run(net.imagej.ops.convert.ConvertIterableInterval.class, out,
in, pixConvert);
(IterableInterval<O>) run(
net.imagej.ops.convert.ConvertIterableInterval.class, out, in,
pixConvert);
return result;
}

Expand Down Expand Up @@ -223,7 +229,6 @@ public String help(final Namespace namespace) {
return result;
}


@Override
public String help() {
final String result =
Expand Down Expand Up @@ -264,8 +269,8 @@ public Object join(final Object... args) {
}

@Override
public <A, B, C> C join(final C out, final A in, final ComputerOp<A, B> first,
final ComputerOp<B, C> second)
public <A, B, C> C join(final C out, final A in,
final ComputerOp<A, B> first, final ComputerOp<B, C> second)
{
@SuppressWarnings("unchecked")
final C result =
Expand All @@ -275,8 +280,9 @@ public <A, B, C> C join(final C out, final A in, final ComputerOp<A, B> first,
}

@Override
public <A, B, C> C join(final C out, final A in, final ComputerOp<A, B> first,
final ComputerOp<B, C> second, final BufferFactory<A, B> bufferFactory)
public <A, B, C> C join(final C out, final A in,
final ComputerOp<A, B> first, final ComputerOp<B, C> second,
final BufferFactory<A, B> bufferFactory)
{
@SuppressWarnings("unchecked")
final C result =
Expand All @@ -303,8 +309,8 @@ public <A> A join(final A out, final A in,
{
@SuppressWarnings("unchecked")
final A result =
(A) run(net.imagej.ops.join.DefaultJoinComputers.class, out, in,
ops, bufferFactory);
(A) run(net.imagej.ops.join.DefaultJoinComputers.class, out, in, ops,
bufferFactory);
return result;
}

Expand Down Expand Up @@ -420,8 +426,7 @@ public <A, B extends Type<B>> IterableInterval<B> map(
@SuppressWarnings("unchecked")
final IterableInterval<B> result =
(IterableInterval<B>) run(
net.imagej.ops.map.MapIterableIntervalToView.class, input, op,
type);
net.imagej.ops.map.MapIterableIntervalToView.class, input, op, type);
return result;
}

Expand Down Expand Up @@ -489,7 +494,8 @@ public <I, O> RandomAccessibleInterval<O> map(
@SuppressWarnings("unchecked")
final RandomAccessibleInterval<O> result =
(RandomAccessibleInterval<O>) run(
net.imagej.ops.map.neighborhood.MapNeighborhood.class, out, in, op, shape);
net.imagej.ops.map.neighborhood.MapNeighborhood.class, out, in, op,
shape);
return result;
}

Expand All @@ -502,7 +508,74 @@ public <I, O> RandomAccessibleInterval<O> map(
@SuppressWarnings("unchecked")
final RandomAccessibleInterval<O> result =
(RandomAccessibleInterval<O>) run(
net.imagej.ops.map.neighborhood.MapNeighborhoodWithCenter.class, out, in, func, shape);
net.imagej.ops.map.neighborhood.MapNeighborhoodWithCenter.class, out,
in, func, shape);
return result;
}

@Override
public <I extends NativeType<I>, O extends NativeType<O>> ArrayImg<O, ?> map(
ArrayImg<O, ?> out, ArrayImg<I, ?> in, ComputerOp<Iterable<I>, O> op,
RectangleShape shape)
{
@SuppressWarnings("unchecked")
final ArrayImg<O, ?> result =
(ArrayImg<O, ?>) run(
net.imagej.ops.map.neighborhood.array.MapNeighborhoodNativeTypeExtended.class,
out, in, op, shape);
return result;
}

@Override
public <I extends NativeType<I>, O extends NativeType<O>> ArrayImg<O, ?> map(
ArrayImg<O, ?> out, ArrayImg<I, ?> in, ComputerOp<Iterable<I>, O> op,
RectangleShape shape, OutOfBoundsFactory<I, ?> oobFactory)
{
@SuppressWarnings("unchecked")
final ArrayImg<O, ?> result =
(ArrayImg<O, ?>) run(
net.imagej.ops.map.neighborhood.array.MapNeighborhoodNativeTypeExtended.class,
out, in, op, shape, oobFactory);
return result;
}

@Override
public <I extends NativeType<I>, O extends NativeType<O>> ArrayImg<O, ?> map(
final ArrayImg<O, ?> out, final ArrayImg<I, ?> in,
final CenterAwareComputerOp<I, O> op, final int span)
{
@SuppressWarnings("unchecked")
final ArrayImg<O, ?> result =
(ArrayImg<O, ?>) run(
net.imagej.ops.map.neighborhood.array.MapNeighborhoodWithCenterNativeType.class,
out, in, op, span);
return result;
}

@Override
public <I extends NativeType<I>, O extends NativeType<O>> ArrayImg<O, ?> map(
final ArrayImg<O, ?> out, final ArrayImg<I, ?> in,
final ComputerOp<Iterable<I>, O> op, final int span)
{
@SuppressWarnings("unchecked")
final ArrayImg<O, ?> result =
(ArrayImg<O, ?>) run(
net.imagej.ops.map.neighborhood.array.MapNeighborhoodNativeType.class,
out, in, op, span);
return result;
}

@Override
public <I extends NativeType<I>, O extends NativeType<O>> ArrayImg<O, ?>
map(final ArrayImg<O, ?> out, final ArrayImg<I, ?> in,
final ComputerOp<Iterable<I>, O> op, final int span,
final Interval interval)
{
@SuppressWarnings("unchecked")
final ArrayImg<O, ?> result =
(ArrayImg<O, ?>) run(
net.imagej.ops.map.neighborhood.array.MapNeighborhoodNativeType.class,
out, in, op, span, interval);
return result;
}

Expand Down
51 changes: 48 additions & 3 deletions src/main/java/net/imagej/ops/OpEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@
import net.imagej.ops.stats.StatsNamespace;
import net.imagej.ops.thread.ThreadNamespace;
import net.imagej.ops.threshold.ThresholdNamespace;
import net.imglib2.Interval;
import net.imglib2.IterableInterval;
import net.imglib2.RandomAccessible;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.neighborhood.RectangleShape;
import net.imglib2.algorithm.neighborhood.Shape;
import net.imglib2.img.array.ArrayImg;
import net.imglib2.outofbounds.OutOfBoundsFactory;
import net.imglib2.type.NativeType;
import net.imglib2.type.Type;
import net.imglib2.type.numeric.RealType;

Expand Down Expand Up @@ -292,12 +297,13 @@ public interface OpEnvironment extends Contextual {

/** Executes the "join" operation on the given arguments. */
@OpMethod(op = net.imagej.ops.join.DefaultJoinComputerAndComputer.class)
<A, B, C> C join(C out, A in, ComputerOp<A, B> first, ComputerOp<B, C> second);
<A, B, C> C
join(C out, A in, ComputerOp<A, B> first, ComputerOp<B, C> second);

/** Executes the "join" operation on the given arguments. */
@OpMethod(op = net.imagej.ops.join.DefaultJoinComputerAndComputer.class)
<A, B, C> C join(C out, A in, ComputerOp<A, B> first, ComputerOp<B, C> second,
BufferFactory<A, B> bufferFactory);
<A, B, C> C join(C out, A in, ComputerOp<A, B> first,
ComputerOp<B, C> second, BufferFactory<A, B> bufferFactory);

/** Executes the "join" operation on the given arguments. */
@OpMethod(op = net.imagej.ops.join.DefaultJoinInplaceAndInplace.class)
Expand Down Expand Up @@ -399,6 +405,45 @@ <I, O> RandomAccessibleInterval<O> map(RandomAccessibleInterval<O> out,
RandomAccessibleInterval<I> in, CenterAwareComputerOp<Iterable<I>, O> op,
Shape shape);

/** Executes the "map" operation on the given arguments. */
@OpMethod(
op = net.imagej.ops.map.neighborhood.array.MapNeighborhoodNativeTypeExtended.class)
public
<I extends NativeType<I>, O extends NativeType<O>> ArrayImg<O, ?> map(
ArrayImg<O, ?> out, ArrayImg<I, ?> in, ComputerOp<Iterable<I>, O> op,
RectangleShape shape);

/** Executes the "map" operation on the given arguments. */
@OpMethod(
op = net.imagej.ops.map.neighborhood.array.MapNeighborhoodNativeTypeExtended.class)
public
<I extends NativeType<I>, O extends NativeType<O>> ArrayImg<O, ?> map(
ArrayImg<O, ?> out, ArrayImg<I, ?> in, ComputerOp<Iterable<I>, O> op,
RectangleShape shape, OutOfBoundsFactory<I, ?> oobFactory);

/** Executes the "map" operation on the given arguments. */
@OpMethod(
op = net.imagej.ops.map.neighborhood.array.MapNeighborhoodWithCenterNativeType.class)
public
<I extends NativeType<I>, O extends NativeType<O>> ArrayImg<O, ?> map(
final ArrayImg<O, ?> out, final ArrayImg<I, ?> in,
final CenterAwareComputerOp<I, O> op, final int span);

/** Executes the "map" operation on the given arguments. */
@OpMethod(
op = net.imagej.ops.map.neighborhood.array.MapNeighborhoodNativeType.class)
public <I extends NativeType<I>, O extends NativeType<O>> ArrayImg<O, ?> map(
final ArrayImg<O, ?> out, final ArrayImg<I, ?> in,
final ComputerOp<Iterable<I>, O> op, final int span);

/** Executes the "map" operation on the given arguments. */
@OpMethod(
op = net.imagej.ops.map.neighborhood.array.MapNeighborhoodNativeType.class)
public <I extends NativeType<I>, O extends NativeType<O>> ArrayImg<O, ?>
map(final ArrayImg<O, ?> out, final ArrayImg<I, ?> in,
final ComputerOp<Iterable<I>, O> op, final int span,
final Interval interval);

/** Executes the "map" operation on the given arguments. */
@OpMethod(op = net.imagej.ops.map.MapIterableToIterable.class)
<A, B> Iterable<B> map(Iterable<B> out, Iterable<A> in, ComputerOp<A, B> op);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@

package net.imagej.ops.map.neighborhood.array;

import net.imagej.ops.ComputerOp;
import net.imagej.ops.Contingent;
import net.imagej.ops.Op;
import net.imagej.ops.Ops;
import net.imagej.ops.map.AbstractMapComputer;
import net.imglib2.FinalInterval;
import net.imglib2.Interval;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImg;
import net.imglib2.type.NativeType;

import org.scijava.Priority;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

/**
* Optimized neighborhood map implementation for 1D/2D/3D {@link Img}. This
* implementation uses access to the underlying types, which bypasses
* OutOfBounds checks, though. This means that pixels which are out of bounds
* are not considered as belonging to the neighborhood of a pixel. This can
* change results of averages over a neighborhood in comparison to using an out
* of bounds strategy which "creates" pixels in the neighborhood where there are
* none after the bounds of the image.
*
* @author Jonathan Hale
* @param <I> Input {@link NativeType}
* @param <O> Ouput {@link NativeType}
* @see MapNeighborhoodWithCenterNativeType
*/
@Plugin(type = Op.class, name = Ops.Map.NAME,
priority = Priority.LOW_PRIORITY + 10)
public class MapNeighborhoodNativeType<I extends NativeType<I>, O extends NativeType<O>>
extends AbstractMapComputer<Iterable<I>, O, ArrayImg<I, ?>, ArrayImg<O, ?>>
implements Contingent
{

/**
* Span of the centered rectangle shape to use.
*/
@Parameter
private int span;

/**
* Interval for input and output images to constrain interation to.
*/
@Parameter(required = false)
private Interval interval;

@Override
public void compute(final ArrayImg<I, ?> input, final ArrayImg<O, ?> output) {
final I in = input.firstElement();
final O out = output.firstElement();

final int width = (int) input.dimension(0);
final int height =
(input.numDimensions() > 1) ? (int) input.dimension(1) : 1;
final int depth =
(input.numDimensions() > 2) ? (int) input.dimension(2) : 1;

final Interval i = (interval == null) ? input : interval;

final int minX = (int) i.min(0);
final int minY = (i.numDimensions() > 1) ? (int) i.min(1) : 0;
final int minZ = (i.numDimensions() > 2) ? (int) i.min(2) : 0;

final int maxX = (int) i.max(0);
final int maxY = (i.numDimensions() > 1) ? (int) i.max(1) : 0;
final int maxZ = (i.numDimensions() > 2) ? (int) i.max(2) : 0;

final int skipX = width - (maxX - minX + 1);
final int skipY = width * (height - (maxY - minY + 1));

final ComputerOp<Iterable<I>, O> op = getOp();

int index = minX + minY * width + minZ * height * width;
in.updateIndex(index);
out.updateIndex(index);

for (int z = minZ; z <= maxZ; ++z) {
for (int y = minY; y <= maxY; ++y) {
for (int x = minX; x <= maxX; ++x) {
// save the current index, since it will be changed by the
// NeighborhoodIterable. Increment to save doing that later.
index = in.getIndex() + 1;

final Iterable<I> neighborhood =
new NeighborhoodIterableNativeType<I>(in, x, y, z, width, height,
depth, span);

op.compute(neighborhood, out);

in.updateIndex(index);
out.incIndex();
}
in.incIndex(skipX);
out.incIndex(skipX);
}
in.incIndex(skipY);
out.incIndex(skipY);
}
}

/**
* @return The interval which constraints iteration or <code>null</code>, if
* none is set.
*/
public Interval getInterval() {
return interval;
}

/**
* Set the interval which constraints iteration.
*/
public void setInterval(Interval i) {
interval = i;
}

/**
* Convenience method to set the interval which constraints iteration.
*
* @see #setInterval(Interval)
*/
public void setInterval(long[] min, long[] max) {
interval = new FinalInterval(min, max);
}

@Override
public boolean conforms() {
return getInput().numDimensions() > 0 || getInput().numDimensions() <= 3;
}

}
Loading