Search in sources :

Example 1 with AbstractCenterAwareComputerOp

use of net.imagej.ops.map.neighborhood.AbstractCenterAwareComputerOp 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)

Aggregations

Ops (net.imagej.ops.Ops)1 AbstractCenterAwareComputerOp (net.imagej.ops.map.neighborhood.AbstractCenterAwareComputerOp)1 UnaryComputerOp (net.imagej.ops.special.computer.UnaryComputerOp)1 DoubleType (net.imglib2.type.numeric.real.DoubleType)1