Search in sources :

Example 6 with Dimensions

use of net.imglib2.Dimensions in project imagej-ops by imagej.

the class IntegralVariance method compute.

@Override
public void compute(final RectangleNeighborhood<Composite<I>> input, final DoubleType output) {
    // computation according to
    // https://en.wikipedia.org/wiki/Summed_area_table
    final IntegralCursor<Composite<I>> cursorS1 = new IntegralCursor<>(input);
    final int dimensions = input.numDimensions();
    // Compute \sum (-1)^{dim - ||cornerVector||_{1}} * I(x^{cornerVector})
    final DoubleType sum1 = new DoubleType();
    sum1.setZero();
    // Convert from input to return type
    final Converter<I, DoubleType> conv = new RealDoubleConverter<>();
    // Compute \sum (-1)^{dim - ||cornerVector||_{1}} * I(x^{cornerVector})
    final DoubleType sum2 = new DoubleType();
    sum2.setZero();
    final DoubleType valueAsDoubleType = new DoubleType();
    while (cursorS1.hasNext()) {
        final Composite<I> compositeValue = cursorS1.next();
        final I value1 = compositeValue.get(0).copy();
        conv.convert(value1, valueAsDoubleType);
        // Obtain the cursor position encoded as corner vector
        final int cornerInteger1 = cursorS1.getCornerRepresentation();
        // Determine if the value has to be added (factor==1) or subtracted
        // (factor==-1)
        final DoubleType factor = new DoubleType(Math.pow(-1.0d, dimensions - IntegralMean.norm(cornerInteger1)));
        valueAsDoubleType.mul(factor);
        sum1.add(valueAsDoubleType);
        final I value2 = compositeValue.get(1).copy();
        conv.convert(value2, valueAsDoubleType);
        // Determine if the value has to be added (factor==1) or subtracted
        // (factor==-1)
        valueAsDoubleType.mul(factor);
        sum2.add(valueAsDoubleType);
    }
    final int area = (int) Intervals.numElements(Intervals.expand(input, -1l));
    // NB: Reuse available DoubleType
    valueAsDoubleType.set(area);
    sum1.mul(sum1);
    // NB
    sum1.div(valueAsDoubleType);
    sum2.sub(sum1);
    // NB
    valueAsDoubleType.sub(new DoubleType(1));
    // NB
    sum2.div(valueAsDoubleType);
    output.set(sum2);
}
Also used : Composite(net.imglib2.view.composite.Composite) DoubleType(net.imglib2.type.numeric.real.DoubleType) IntegralCursor(net.imagej.ops.image.integral.IntegralCursor) RealDoubleConverter(net.imglib2.converter.RealDoubleConverter)

Example 7 with Dimensions

use of net.imglib2.Dimensions in project imagej-ops by imagej.

the class PartialDerivativeRAI method initialize.

@SuppressWarnings("unchecked")
@Override
public void initialize() {
    RandomAccessibleInterval<T> kernel = ops().create().kernelSobel(Util.getTypeFromInterval(in()));
    RandomAccessibleInterval<T> kernelA = Views.hyperSlice(Views.hyperSlice(kernel, 3, 0), 2, 0);
    RandomAccessibleInterval<T> kernelB = Views.hyperSlice(Views.hyperSlice(kernel, 3, 0), 2, 1);
    // add dimensions to kernel to rotate properly
    if (in().numDimensions() > 2) {
        RandomAccessible<T> expandedKernelA = Views.addDimension(kernelA);
        RandomAccessible<T> expandedKernelB = Views.addDimension(kernelB);
        for (int i = 0; i < in().numDimensions() - 3; i++) {
            expandedKernelA = Views.addDimension(expandedKernelA);
            expandedKernelB = Views.addDimension(expandedKernelB);
        }
        long[] dims = new long[in().numDimensions()];
        for (int j = 0; j < in().numDimensions(); j++) {
            dims[j] = 1;
        }
        dims[0] = 3;
        Interval kernelInterval = new FinalInterval(dims);
        kernelA = Views.interval(expandedKernelA, kernelInterval);
        kernelB = Views.interval(expandedKernelB, kernelInterval);
    }
    long[] dims = new long[in().numDimensions()];
    if (dimension == 0) {
        // FIXME hack
        kernelBConvolveOp = RAIs.computer(ops(), Ops.Filter.Convolve.class, in(), new Object[] { kernelB });
    } else {
        // rotate kernel B to dimension
        for (int j = 0; j < in().numDimensions(); j++) {
            if (j == dimension) {
                dims[j] = 3;
            } else {
                dims[j] = 1;
            }
        }
        Img<DoubleType> kernelInterval = ops().create().img(dims);
        RandomAccessibleInterval<T> rotatedKernelB = kernelB;
        for (int i = 0; i < dimension; i++) {
            rotatedKernelB = Views.rotate(rotatedKernelB, i, i + 1);
        }
        rotatedKernelB = Views.interval(rotatedKernelB, kernelInterval);
        kernelBConvolveOp = RAIs.computer(ops(), Ops.Filter.Convolve.class, in(), new Object[] { rotatedKernelB });
    }
    dims = null;
    // rotate kernel A to all other dimensions
    kernelAConvolveOps = new UnaryComputerOp[in().numDimensions()];
    if (dimension != 0) {
        kernelAConvolveOps[0] = RAIs.computer(ops(), Ops.Filter.Convolve.class, in(), new Object[] { kernelA });
    }
    RandomAccessibleInterval<T> rotatedKernelA = kernelA;
    for (int i = 1; i < in().numDimensions(); i++) {
        if (i != dimension) {
            dims = new long[in().numDimensions()];
            for (int j = 0; j < in().numDimensions(); j++) {
                if (i == j) {
                    dims[j] = 3;
                } else {
                    dims[j] = 1;
                }
            }
            Img<DoubleType> kernelInterval = ops().create().img(dims);
            for (int j = 0; j < i; j++) {
                rotatedKernelA = Views.rotate(rotatedKernelA, j, j + 1);
            }
            kernelAConvolveOps[i] = RAIs.computer(ops(), Ops.Filter.Convolve.class, in(), new Object[] { Views.interval(rotatedKernelA, kernelInterval) });
            rotatedKernelA = kernelA;
        }
    }
    addOp = RAIs.binaryComputer(ops(), Ops.Math.Add.class, in(), in());
    createRAI = RAIs.function(ops(), Ops.Create.Img.class, in());
}
Also used : Img(net.imglib2.img.Img) Ops(net.imagej.ops.Ops) DoubleType(net.imglib2.type.numeric.real.DoubleType) FinalInterval(net.imglib2.FinalInterval) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) Interval(net.imglib2.Interval) FinalInterval(net.imglib2.FinalInterval)

Example 8 with Dimensions

use of net.imglib2.Dimensions in project imagej-ops by imagej.

the class DefaultTubeness method compute.

@Override
public void compute(final RandomAccessibleInterval<T> input, final IterableInterval<DoubleType> tubeness) {
    cancelReason = null;
    final int numDimensions = input.numDimensions();
    // Sigmas in pixel units.
    final double[] sigmas = new double[numDimensions];
    for (int d = 0; d < sigmas.length; d++) {
        final double cal = d < calibration.length ? calibration[d] : 1;
        sigmas[d] = sigma / cal;
    }
    /*
		 * Hessian.
		 */
    // Get a suitable image factory.
    final long[] gradientDims = new long[numDimensions + 1];
    final long[] hessianDims = new long[numDimensions + 1];
    for (int d = 0; d < numDimensions; d++) {
        hessianDims[d] = input.dimension(d);
        gradientDims[d] = input.dimension(d);
    }
    hessianDims[numDimensions] = numDimensions * (numDimensions + 1) / 2;
    gradientDims[numDimensions] = numDimensions;
    final Dimensions hessianDimensions = FinalDimensions.wrap(hessianDims);
    final FinalDimensions gradientDimensions = FinalDimensions.wrap(gradientDims);
    final ImgFactory<DoubleType> factory = ops().create().imgFactory(hessianDimensions);
    final Img<DoubleType> hessian = factory.create(hessianDimensions, new DoubleType());
    final Img<DoubleType> gradient = factory.create(gradientDimensions, new DoubleType());
    final Img<DoubleType> gaussian = factory.create(input, new DoubleType());
    // Handle multithreading.
    final int nThreads = Runtime.getRuntime().availableProcessors();
    final ExecutorService es = threadService.getExecutorService();
    try {
        // Hessian calculation.
        HessianMatrix.calculateMatrix(Views.extendBorder(input), gaussian, gradient, hessian, new OutOfBoundsBorderFactory<>(), nThreads, es, sigma);
        statusService.showProgress(1, 3);
        if (isCanceled())
            return;
        // Hessian eigenvalues.
        final RandomAccessibleInterval<DoubleType> evs = TensorEigenValues.calculateEigenValuesSymmetric(hessian, TensorEigenValues.createAppropriateResultImg(hessian, factory, new DoubleType()), nThreads, es);
        statusService.showProgress(2, 3);
        if (isCanceled())
            return;
        final AbstractUnaryComputerOp<Iterable<DoubleType>, DoubleType> method;
        switch(numDimensions) {
            case 2:
                method = new Tubeness2D(sigma);
                break;
            case 3:
                method = new Tubeness3D(sigma);
                break;
            default:
                System.err.println("Cannot compute tubeness for " + numDimensions + "D images.");
                return;
        }
        ops().transform().project(tubeness, evs, method, numDimensions);
        statusService.showProgress(3, 3);
        return;
    } catch (final IncompatibleTypeException | InterruptedException | ExecutionException e) {
        e.printStackTrace();
        return;
    }
}
Also used : Dimensions(net.imglib2.Dimensions) FinalDimensions(net.imglib2.FinalDimensions) FinalDimensions(net.imglib2.FinalDimensions) DoubleType(net.imglib2.type.numeric.real.DoubleType) ExecutorService(java.util.concurrent.ExecutorService) IncompatibleTypeException(net.imglib2.exception.IncompatibleTypeException) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with Dimensions

use of net.imglib2.Dimensions in project imagej-ops by imagej.

the class FFTMethodsOpF method calculate.

@Override
public RandomAccessibleInterval<C> calculate(final RandomAccessibleInterval<T> input) {
    // calculate the padded size
    long[] paddedSize = new long[in().numDimensions()];
    for (int d = 0; d < in().numDimensions(); d++) {
        paddedSize[d] = in().dimension(d);
        if (borderSize != null) {
            paddedSize[d] += borderSize[d];
        }
    }
    Dimensions paddedDimensions = new FinalDimensions(paddedSize);
    // create the complex output
    RandomAccessibleInterval<C> output = createOp.calculate(paddedDimensions);
    // pad the input
    RandomAccessibleInterval<T> paddedInput = padOp.calculate(input, paddedDimensions);
    // compute and return fft
    fftMethodsOp.compute(paddedInput, output);
    return output;
}
Also used : FinalDimensions(net.imglib2.FinalDimensions) Dimensions(net.imglib2.Dimensions) FinalDimensions(net.imglib2.FinalDimensions)

Example 10 with Dimensions

use of net.imglib2.Dimensions in project imagej-ops by imagej.

the class AbstractThresholdTest method initialize.

@Before
public void initialize() {
    final long[] dimensions = new long[] { xSize, ySize };
    final Random r = new Random(0xdeadbeef);
    // create image and output
    in = ArrayImgs.unsignedShorts(dimensions);
    final RandomAccess<UnsignedShortType> ra = in.randomAccess();
    // populate pixel values with a ramp function + a constant
    for (int x = 0; x < xSize; x++) {
        for (int y = 0; y < ySize; y++) {
            ra.setPosition(new int[] { x, y });
            ra.get().setReal(r.nextInt(65535));
        }
    }
}
Also used : Random(java.util.Random) UnsignedShortType(net.imglib2.type.numeric.integer.UnsignedShortType) Before(org.junit.Before)

Aggregations

AbstractOpTest (net.imagej.ops.AbstractOpTest)14 Img (net.imglib2.img.Img)14 Test (org.junit.Test)14 DoubleType (net.imglib2.type.numeric.real.DoubleType)13 FinalDimensions (net.imglib2.FinalDimensions)12 Dimensions (net.imglib2.Dimensions)10 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)9 FinalInterval (net.imglib2.FinalInterval)8 FloatType (net.imglib2.type.numeric.real.FloatType)8 Ops (net.imagej.ops.Ops)6 CreateImgFromImg (net.imagej.ops.create.img.CreateImgFromImg)5 BitType (net.imglib2.type.logic.BitType)5 ArrayList (java.util.ArrayList)4 Random (java.util.Random)4 CreateImgFromDimsAndType (net.imagej.ops.create.img.CreateImgFromDimsAndType)4 File (java.io.File)3 Dataset (net.imagej.Dataset)3 IntegralCursor (net.imagej.ops.image.integral.IntegralCursor)3 ByteType (net.imglib2.type.numeric.integer.ByteType)3 UnsignedByteType (net.imglib2.type.numeric.integer.UnsignedByteType)3