|
| 1 | +/* #%L |
| 2 | + * ImageJ software for multidimensional image processing and analysis. |
| 3 | + * %% |
| 4 | + * Copyright (C) 2014 - 2016 Board of Regents of the University of |
| 5 | + * Wisconsin-Madison, University of Konstanz and Brian Northan. |
| 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.sobel; |
| 31 | + |
| 32 | +import net.imagej.ops.Ops; |
| 33 | +import net.imagej.ops.Ops.Math.Sqr; |
| 34 | +import net.imagej.ops.Ops.Math.Sqrt; |
| 35 | +import net.imagej.ops.special.chain.RAIs; |
| 36 | +import net.imagej.ops.special.computer.BinaryComputerOp; |
| 37 | +import net.imagej.ops.special.computer.UnaryComputerOp; |
| 38 | +import net.imagej.ops.special.function.UnaryFunctionOp; |
| 39 | +import net.imagej.ops.special.hybrid.AbstractUnaryHybridCF; |
| 40 | +import net.imglib2.RandomAccessibleInterval; |
| 41 | +import net.imglib2.type.numeric.RealType; |
| 42 | + |
| 43 | +import org.scijava.plugin.Plugin; |
| 44 | + |
| 45 | +/** |
| 46 | + * Sobel filter implementation using separated sobel kernel. |
| 47 | + * |
| 48 | + * @author Eike Heinz, University of Konstanz |
| 49 | + * |
| 50 | + * @param <T> |
| 51 | + * type of input |
| 52 | + */ |
| 53 | + |
| 54 | +@Plugin(type = Ops.Filter.Sobel.class) |
| 55 | +public class SobelRAI<T extends RealType<T>> |
| 56 | + extends AbstractUnaryHybridCF<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>> implements Ops.Filter.Sobel { |
| 57 | + |
| 58 | + private UnaryFunctionOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>> createRAI; |
| 59 | + |
| 60 | + private UnaryComputerOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>> squareMapOp; |
| 61 | + |
| 62 | + private UnaryComputerOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>> sqrtMapOp; |
| 63 | + |
| 64 | + private BinaryComputerOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>, RandomAccessibleInterval<T>> addOp; |
| 65 | + |
| 66 | + private UnaryComputerOp<RandomAccessibleInterval<T>, RandomAccessibleInterval<T>>[] derivativeComputers; |
| 67 | + |
| 68 | + @SuppressWarnings("unchecked") |
| 69 | + @Override |
| 70 | + public void initialize() { |
| 71 | + createRAI = RAIs.function(ops(), Ops.Create.Img.class, in()); |
| 72 | + |
| 73 | + Sqr squareOp = ops().op(Ops.Math.Sqr.class, RealType.class, RealType.class); |
| 74 | + squareMapOp = RAIs.computer(ops(), Ops.Map.class, in(), squareOp); |
| 75 | + Sqrt sqrtOp = ops().op(Ops.Math.Sqrt.class, RealType.class, RealType.class); |
| 76 | + sqrtMapOp = RAIs.computer(ops(), Ops.Map.class, in(), sqrtOp); |
| 77 | + addOp = RAIs.binaryComputer(ops(), Ops.Math.Add.class, in(), in()); |
| 78 | + |
| 79 | + derivativeComputers = new UnaryComputerOp[in().numDimensions()]; |
| 80 | + for (int i = 0; i < in().numDimensions(); i++) { |
| 81 | + derivativeComputers[i] = RAIs.computer(ops(), Ops.Filter.PartialDerivative.class, in(), i); |
| 82 | + } |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void compute(RandomAccessibleInterval<T> input, RandomAccessibleInterval<T> output) { |
| 88 | + |
| 89 | + for (int i = 0; i < derivativeComputers.length; i++) { |
| 90 | + RandomAccessibleInterval<T> derivative = createRAI.calculate(input); |
| 91 | + derivativeComputers[i].compute(input, derivative); |
| 92 | + squareMapOp.compute(derivative, derivative); |
| 93 | + addOp.compute(output, derivative, output); |
| 94 | + } |
| 95 | + sqrtMapOp.compute(output, output); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public RandomAccessibleInterval<T> createOutput(RandomAccessibleInterval<T> input) { |
| 100 | + return createRAI.calculate(input); |
| 101 | + } |
| 102 | +} |
0 commit comments