Search in sources :

Example 11 with Cursor

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

the class DefaultDirectionalityFeature method compute.

@SuppressWarnings("unchecked")
@Override
public void compute(final RandomAccessibleInterval<I> input, final O output) {
    // List to store all directions occuring within the image on borders
    ArrayList<DoubleType> dirList = new ArrayList<>();
    // Dimension of input region
    long[] dims = new long[input.numDimensions()];
    input.dimensions(dims);
    // create image for derivations in x and y direction
    Img<I> derX = imgCreator.calculate(input);
    Img<I> derY = imgCreator.calculate(input);
    // calculate derivations in x and y direction
    PartialDerivative.gradientCentralDifference2(Views.extendMirrorSingle(input), derX, 0);
    PartialDerivative.gradientCentralDifference2(Views.extendMirrorSingle(input), derY, 1);
    // calculate theta at each position: theta = atan(dX/dY) + pi/2
    Cursor<I> cX = derX.cursor();
    Cursor<I> cY = derY.cursor();
    // for each position calculate magnitude and direction
    while (cX.hasNext()) {
        cX.next();
        cY.next();
        double dx = cX.get().getRealDouble();
        double dy = cY.get().getRealDouble();
        double dir = 0.0;
        double mag = 0.0;
        mag = Math.sqrt(dx * dx + dy * dy);
        if (dx != 0 && mag > 0.0) {
            dir = Math.atan(dy / dx) + Math.PI / 2;
            dirList.add(new DoubleType(dir));
        }
    }
    // No directions: output is zero
    if (dirList.isEmpty()) {
        output.setReal(0.0);
    } else // Otherwise compute histogram over all occuring directions
    // and calculate inverse second moment on it as output
    {
        Histogram1d<Integer> hist = histOp.calculate(dirList);
        double std = stdOp.calculate(hist).getRealDouble();
        output.setReal(1 / std);
    }
}
Also used : DoubleType(net.imglib2.type.numeric.real.DoubleType) ArrayList(java.util.ArrayList)

Example 12 with Cursor

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

the class HistogramOfOrientedGradients2D method compute.

@SuppressWarnings("unchecked")
@Override
public void compute(RandomAccessibleInterval<T> in, RandomAccessibleInterval<T> out) {
    final RandomAccessible<FloatType> convertedIn = Converters.convert(Views.extendMirrorDouble(in), converterToFloat, new FloatType());
    // compute partial derivative for each dimension
    RandomAccessibleInterval<FloatType> derivative0 = createImgOp.calculate();
    RandomAccessibleInterval<FloatType> derivative1 = createImgOp.calculate();
    // case of grayscale image
    if (in.numDimensions() == 2) {
        PartialDerivative.gradientCentralDifference(convertedIn, derivative0, 0);
        PartialDerivative.gradientCentralDifference(convertedIn, derivative1, 1);
    } else // case of color image
    {
        List<RandomAccessibleInterval<FloatType>> listDerivs0 = new ArrayList<>();
        List<RandomAccessibleInterval<FloatType>> listDerivs1 = new ArrayList<>();
        for (int i = 0; i < in.dimension(2); i++) {
            final RandomAccessibleInterval<FloatType> deriv0 = createImgOp.calculate();
            final RandomAccessibleInterval<FloatType> deriv1 = createImgOp.calculate();
            PartialDerivative.gradientCentralDifference(Views.interval(convertedIn, new long[] { 0, 0, i }, new long[] { in.max(0), in.max(1), i }), deriv0, 0);
            PartialDerivative.gradientCentralDifference(Views.interval(convertedIn, new long[] { 0, 0, i }, new long[] { in.max(0), in.max(1), i }), deriv1, 1);
            listDerivs0.add(deriv0);
            listDerivs1.add(deriv1);
        }
        derivative0 = Converters.convert(Views.collapse(Views.stack(listDerivs0)), converterGetMax, new FloatType());
        derivative1 = Converters.convert(Views.collapse(Views.stack(listDerivs1)), converterGetMax, new FloatType());
    }
    final RandomAccessibleInterval<FloatType> finalderivative0 = derivative0;
    final RandomAccessibleInterval<FloatType> finalderivative1 = derivative1;
    // compute angles and magnitudes
    final RandomAccessibleInterval<FloatType> angles = createImgOp.calculate();
    final RandomAccessibleInterval<FloatType> magnitudes = createImgOp.calculate();
    final CursorBasedChunk chunkable = new CursorBasedChunk() {

        @Override
        public void execute(int startIndex, int stepSize, int numSteps) {
            final Cursor<FloatType> cursorAngles = Views.flatIterable(angles).localizingCursor();
            final Cursor<FloatType> cursorMagnitudes = Views.flatIterable(magnitudes).localizingCursor();
            final Cursor<FloatType> cursorDerivative0 = Views.flatIterable(finalderivative0).localizingCursor();
            final Cursor<FloatType> cursorDerivative1 = Views.flatIterable(finalderivative1).localizingCursor();
            setToStart(cursorAngles, startIndex);
            setToStart(cursorMagnitudes, startIndex);
            setToStart(cursorDerivative0, startIndex);
            setToStart(cursorDerivative1, startIndex);
            for (int i = 0; i < numSteps; i++) {
                final float x = cursorDerivative0.get().getRealFloat();
                final float y = cursorDerivative1.get().getRealFloat();
                cursorAngles.get().setReal(getAngle(x, y));
                cursorMagnitudes.get().setReal(getMagnitude(x, y));
                cursorAngles.jumpFwd(stepSize);
                cursorMagnitudes.jumpFwd(stepSize);
                cursorDerivative0.jumpFwd(stepSize);
                cursorDerivative1.jumpFwd(stepSize);
            }
        }
    };
    ops().thread().chunker(chunkable, Views.flatIterable(magnitudes).size());
    // stores each Thread to execute
    final List<Callable<Void>> listCallables = new ArrayList<>();
    // compute descriptor (default 3x3, i.e. 9 channels: one channel for
    // each bin)
    final RectangleShape shape = new RectangleShape(spanOfNeighborhood, false);
    final NeighborhoodsAccessible<FloatType> neighborHood = shape.neighborhoodsRandomAccessible(angles);
    for (int i = 0; i < in.dimension(0); i++) {
        listCallables.add(new ComputeDescriptor(Views.interval(convertedIn, in), i, angles.randomAccess(), magnitudes.randomAccess(), (RandomAccess<FloatType>) out.randomAccess(), neighborHood.randomAccess()));
    }
    try {
        es.invokeAll(listCallables);
    } catch (final InterruptedException e) {
        throw new RuntimeException(e);
    }
    listCallables.clear();
}
Also used : CursorBasedChunk(net.imagej.ops.thread.chunker.CursorBasedChunk) ArrayList(java.util.ArrayList) RandomAccess(net.imglib2.RandomAccess) Callable(java.util.concurrent.Callable) FloatType(net.imglib2.type.numeric.real.FloatType) RectangleShape(net.imglib2.algorithm.neighborhood.RectangleShape) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval)

Example 13 with Cursor

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

the class DefaultCenterOfGravity method calculate.

@Override
public RealLocalizable calculate(final IterableInterval<T> input) {
    final int numDimensions = input.numDimensions();
    final double[] output = new double[numDimensions];
    final double[] intensityValues = new double[numDimensions];
    final Cursor<T> c = input.localizingCursor();
    while (c.hasNext()) {
        c.fwd();
        for (int i = 0; i < output.length; i++) {
            output[i] += c.getDoublePosition(i) * c.get().getRealDouble();
            intensityValues[i] += c.get().getRealDouble();
        }
    }
    for (int i = 0; i < output.length; i++) {
        output[i] = output[i] / intensityValues[i];
    }
    return new RealPoint(output);
}
Also used : RealPoint(net.imglib2.RealPoint) RealPoint(net.imglib2.RealPoint)

Example 14 with Cursor

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

the class ConvolveNaiveC method compute.

@Override
public void compute(final RandomAccessible<I> input, final RandomAccessibleInterval<O> output) {
    // TODO: try a decomposition of the kernel into n 1-dim kernels
    final long[] min = new long[input.numDimensions()];
    final long[] max = new long[input.numDimensions()];
    for (int d = 0; d < kernel.numDimensions(); d++) {
        min[d] = -kernel.dimension(d);
        max[d] = kernel.dimension(d) + output.dimension(d);
    }
    final RandomAccess<I> inRA = input.randomAccess(new FinalInterval(min, max));
    final Cursor<K> kernelC = Views.iterable(kernel).localizingCursor();
    final Cursor<O> outC = Views.iterable(output).localizingCursor();
    final long[] pos = new long[input.numDimensions()];
    final long[] kernelRadius = new long[kernel.numDimensions()];
    for (int i = 0; i < kernelRadius.length; i++) {
        kernelRadius[i] = kernel.dimension(i) / 2;
    }
    float val;
    while (outC.hasNext()) {
        // image
        outC.fwd();
        outC.localize(pos);
        // kernel inlined version of the method convolve
        val = 0;
        inRA.setPosition(pos);
        kernelC.reset();
        while (kernelC.hasNext()) {
            kernelC.fwd();
            for (int i = 0; i < kernelRadius.length; i++) {
                // dimension can have zero extension e.g. vertical 1d kernel
                if (kernelRadius[i] > 0) {
                    inRA.setPosition(pos[i] + kernelC.getLongPosition(i) - kernelRadius[i], i);
                }
            }
            val += inRA.get().getRealDouble() * kernelC.get().getRealDouble();
        }
        outC.get().setReal(val);
    }
}
Also used : FinalInterval(net.imglib2.FinalInterval)

Example 15 with Cursor

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

the class InvertTest method testDoubleTypeCustomInvert.

@Test
public void testDoubleTypeCustomInvert() {
    final Img<DoubleType> inDoubleType = generateDoubleArrayTestImg(true, 5, 5);
    final Img<DoubleType> outDoubleType = inDoubleType.factory().create(inDoubleType, new DoubleType());
    // set this array of doubles to be the pixel values.
    final double[] nums = new double[] { Double.MAX_VALUE, Double.MIN_VALUE, 1d, -1d, 0d, Double.MAX_VALUE + 1, -Double.MAX_VALUE - 1, Math.PI, Math.E, Math.sqrt(Math.PI), Math.pow(25, 25), 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY };
    final Cursor<DoubleType> c = inDoubleType.localizingCursor();
    for (final double i : nums) {
        c.next();
        c.get().set(i);
    }
    assertDefaultInvert(inDoubleType, outDoubleType);
    assertDefaultInvertMinMaxProvided(inDoubleType, outDoubleType, new DoubleType(437d), new DoubleType(8008d));
}
Also used : DoubleType(net.imglib2.type.numeric.real.DoubleType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)43 AbstractOpTest (net.imagej.ops.AbstractOpTest)40 DoubleType (net.imglib2.type.numeric.real.DoubleType)30 Random (java.util.Random)21 FinalInterval (net.imglib2.FinalInterval)21 ByteType (net.imglib2.type.numeric.integer.ByteType)14 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)12 RectangleShape (net.imglib2.algorithm.neighborhood.RectangleShape)11 ArrayList (java.util.ArrayList)10 DiamondShape (net.imglib2.algorithm.neighborhood.DiamondShape)10 Shape (net.imglib2.algorithm.neighborhood.Shape)10 BitType (net.imglib2.type.logic.BitType)8 FloatType (net.imglib2.type.numeric.real.FloatType)8 Img (net.imglib2.img.Img)7 HorizontalLineShape (net.imglib2.algorithm.neighborhood.HorizontalLineShape)6 UnsignedByteType (net.imglib2.type.numeric.integer.UnsignedByteType)6 Before (org.junit.Before)6 IterableInterval (net.imglib2.IterableInterval)4 Point (net.imglib2.Point)4 RealPoint (net.imglib2.RealPoint)4