Search in sources :

Example 16 with Neighborhood

use of net.imglib2.algorithm.neighborhood.Neighborhood in project imagej-ops by imagej.

the class LocalNiblackThreshold method unaryComputer.

@Override
protected CenterAwareComputerOp<T, BitType> unaryComputer(final T inClass, final BitType outClass) {
    final LocalThresholdMethod<T> op = new LocalThresholdMethod<T>() {

        private UnaryComputerOp<Iterable<T>, DoubleType> mean;

        private UnaryComputerOp<Iterable<T>, DoubleType> stdDeviation;

        @Override
        public void compute(final Iterable<T> neighborhood, final T center, final BitType output) {
            if (mean == null) {
                mean = Computers.unary(ops(), Ops.Stats.Mean.class, new DoubleType(), neighborhood);
            }
            if (stdDeviation == null) {
                stdDeviation = Computers.unary(ops(), Ops.Stats.StdDev.class, new DoubleType(), neighborhood);
            }
            final DoubleType m = new DoubleType();
            mean.compute(neighborhood, m);
            final DoubleType stdDev = new DoubleType();
            stdDeviation.compute(neighborhood, stdDev);
            output.set(center.getRealDouble() > m.getRealDouble() + k * stdDev.getRealDouble() - c);
        }
    };
    op.setEnvironment(ops());
    return op;
}
Also used : UnaryComputerOp(net.imagej.ops.special.computer.UnaryComputerOp) Ops(net.imagej.ops.Ops) BitType(net.imglib2.type.logic.BitType) DoubleType(net.imglib2.type.numeric.real.DoubleType) LocalThresholdMethod(net.imagej.ops.threshold.LocalThresholdMethod)

Example 17 with Neighborhood

use of net.imglib2.algorithm.neighborhood.Neighborhood in project imagej-ops by imagej.

the class LocalSauvolaThreshold method unaryComputer.

@Override
protected CenterAwareComputerOp<T, BitType> unaryComputer(final T inClass, final BitType outClass) {
    final LocalThresholdMethod<T> op = new LocalThresholdMethod<T>() {

        private UnaryComputerOp<Iterable<T>, DoubleType> mean;

        private UnaryComputerOp<Iterable<T>, DoubleType> stdDeviation;

        @Override
        public void compute(final Iterable<T> neighborhood, final T center, final BitType output) {
            if (mean == null) {
                mean = Computers.unary(ops(), Ops.Stats.Mean.class, new DoubleType(), neighborhood);
            }
            if (stdDeviation == null) {
                stdDeviation = Computers.unary(ops(), Ops.Stats.StdDev.class, new DoubleType(), neighborhood);
            }
            final DoubleType meanValue = new DoubleType();
            mean.compute(neighborhood, meanValue);
            final DoubleType stdDevValue = new DoubleType();
            stdDeviation.compute(neighborhood, stdDevValue);
            double threshold = meanValue.get() * (1.0d + k * ((Math.sqrt(stdDevValue.get()) / r) - 1.0));
            output.set(center.getRealDouble() >= threshold);
        }
    };
    op.setEnvironment(ops());
    return op;
}
Also used : UnaryComputerOp(net.imagej.ops.special.computer.UnaryComputerOp) Ops(net.imagej.ops.Ops) BitType(net.imglib2.type.logic.BitType) DoubleType(net.imglib2.type.numeric.real.DoubleType) LocalThresholdMethod(net.imagej.ops.threshold.LocalThresholdMethod)

Example 18 with Neighborhood

use of net.imglib2.algorithm.neighborhood.Neighborhood in project imagej-ops by imagej.

the class DefaultSigmaFilter method unaryComputer.

@Override
protected CenterAwareComputerOp<T, V> unaryComputer(final T inType, final V outType) {
    final AbstractCenterAwareComputerOp<T, V> op = new AbstractCenterAwareComputerOp<T, V>() {

        private UnaryComputerOp<Iterable<T>, DoubleType> variance;

        @Override
        public void compute(final Iterable<T> neighborhood, final T center, final V output) {
            if (variance == null) {
                variance = Computers.unary(ops(), Ops.Stats.Variance.class, DoubleType.class, neighborhood);
            }
            DoubleType varianceResult = new DoubleType();
            variance.compute(neighborhood, varianceResult);
            double varianceValue = varianceResult.getRealDouble() * range;
            final double centerValue = center.getRealDouble();
            double sumAll = 0;
            double sumWithin = 0;
            long countAll = 0;
            long countWithin = 0;
            for (T neighbor : neighborhood) {
                final double pixelValue = neighbor.getRealDouble();
                final double diff = centerValue - pixelValue;
                sumAll += pixelValue;
                ++countAll;
                if (diff > varianceValue || diff < -varianceValue) {
                    continue;
                }
                // pixel within variance range
                sumWithin += pixelValue;
                ++countWithin;
            }
            if (countWithin < (int) (minPixelFraction * countAll)) {
                // simply mean
                output.setReal(sumAll / countAll);
            } else {
                // mean over pixels in variance range only
                output.setReal(sumWithin / countWithin);
            }
        }
    };
    op.setEnvironment(ops());
    return op;
}
Also used : AbstractCenterAwareComputerOp(net.imagej.ops.map.neighborhood.AbstractCenterAwareComputerOp) UnaryComputerOp(net.imagej.ops.special.computer.UnaryComputerOp) Ops(net.imagej.ops.Ops) DoubleType(net.imglib2.type.numeric.real.DoubleType)

Example 19 with Neighborhood

use of net.imglib2.algorithm.neighborhood.Neighborhood in project imagej-ops by imagej.

the class DefaultBilateral method compute.

@Override
public void compute(final RandomAccessibleInterval<I> input, final RandomAccessibleInterval<O> output) {
    final long[] size = new long[input.numDimensions()];
    input.dimensions(size);
    final RandomAccess<O> outputRA = output.randomAccess();
    final Cursor<I> inputCursor = Views.iterable(input).localizingCursor();
    final long[] currentPos = new long[input.numDimensions()];
    final long[] neighborhoodPos = new long[input.numDimensions()];
    final long[] neighborhoodMin = new long[input.numDimensions()];
    final long[] neighborhoodMax = new long[input.numDimensions()];
    Neighborhood<I> neighborhood;
    Cursor<I> neighborhoodCursor;
    final RectangleNeighborhoodFactory<I> fac = RectangleNeighborhood.factory();
    while (inputCursor.hasNext()) {
        inputCursor.fwd();
        inputCursor.localize(currentPos);
        double distance;
        inputCursor.localize(neighborhoodMin);
        inputCursor.localize(neighborhoodMax);
        neighborhoodMin[0] = Math.max(0, neighborhoodMin[0] - radius);
        neighborhoodMin[1] = Math.max(0, neighborhoodMin[1] - radius);
        neighborhoodMax[0] = Math.min(input.max(0), neighborhoodMax[0] + radius);
        neighborhoodMax[1] = Math.min(input.max(1), neighborhoodMax[1] + radius);
        final Interval interval = new FinalInterval(neighborhoodMin, neighborhoodMax);
        neighborhood = fac.create(currentPos, neighborhoodMin, neighborhoodMax, interval, input.randomAccess());
        neighborhoodCursor = neighborhood.localizingCursor();
        double weight, v = 0.0;
        double w = 0.0;
        do {
            neighborhoodCursor.fwd();
            neighborhoodCursor.localize(neighborhoodPos);
            distance = getDistance(currentPos, neighborhoodPos);
            // spatial kernel
            weight = gauss(distance, sigmaS);
            // intensity
            distance = Math.abs(inputCursor.get().getRealDouble() - neighborhoodCursor.get().getRealDouble());
            // difference
            // range kernel, then exponent addition
            weight *= gauss(distance, sigmaR);
            v += weight * neighborhoodCursor.get().getRealDouble();
            w += weight;
        } while (neighborhoodCursor.hasNext());
        outputRA.setPosition(currentPos);
        outputRA.get().setReal(v / w);
    }
}
Also used : FinalInterval(net.imglib2.FinalInterval) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) Interval(net.imglib2.Interval) FinalInterval(net.imglib2.FinalInterval)

Aggregations

BitType (net.imglib2.type.logic.BitType)11 Ops (net.imagej.ops.Ops)7 RectangleShape (net.imglib2.algorithm.neighborhood.RectangleShape)7 UnaryComputerOp (net.imagej.ops.special.computer.UnaryComputerOp)6 IntType (net.imglib2.type.numeric.integer.IntType)6 DoubleType (net.imglib2.type.numeric.real.DoubleType)6 LocalThresholdMethod (net.imagej.ops.threshold.LocalThresholdMethod)5 Shape (net.imglib2.algorithm.neighborhood.Shape)5 ImgLabeling (net.imglib2.roi.labeling.ImgLabeling)5 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)4 Neighborhood (net.imglib2.algorithm.neighborhood.Neighborhood)4 ByteType (net.imglib2.type.numeric.integer.ByteType)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 AbstractOpTest (net.imagej.ops.AbstractOpTest)2 Op (net.imagej.ops.Op)2 AbstractUnaryComputerOp (net.imagej.ops.special.computer.AbstractUnaryComputerOp)2 Interval (net.imglib2.Interval)2 RandomAccess (net.imglib2.RandomAccess)2 DiamondShape (net.imglib2.algorithm.neighborhood.DiamondShape)2