Search in sources :

Example 1 with RectangleNeighborhood

use of net.imglib2.algorithm.neighborhood.RectangleNeighborhood 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 2 with RectangleNeighborhood

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

the class IntegralCursorTest method testIntegralCursor.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testIntegralCursor() {
    Shape rectangleShape = new RectangleShape(1, false);
    IterableInterval<Neighborhood<ByteType>> ii = rectangleShape.neighborhoodsSafe(img);
    Cursor<Neighborhood<ByteType>> cursor = ii.cursor();
    // Manually select the neighborhood that is centered on the image
    cursor.fwd();
    cursor.fwd();
    cursor.fwd();
    cursor.fwd();
    cursor.fwd();
    IntegralCursor<ByteType> integralCursor = new IntegralCursor<>((RectangleNeighborhood) cursor.get());
    assertEquals(integralCursor.next(), new ByteType((byte) 1));
    assertEquals(integralCursor.next(), new ByteType((byte) 2));
    assertEquals(integralCursor.next(), new ByteType((byte) 5));
    assertEquals(integralCursor.next(), new ByteType((byte) 4));
    assertFalse(integralCursor.hasNext());
    integralCursor.reset();
    assertEquals(integralCursor.next(), new ByteType((byte) 1));
    assertEquals(integralCursor.next(), new ByteType((byte) 2));
    assertEquals(integralCursor.next(), new ByteType((byte) 5));
    assertEquals(integralCursor.next(), new ByteType((byte) 4));
    assertFalse(integralCursor.hasNext());
}
Also used : Shape(net.imglib2.algorithm.neighborhood.Shape) RectangleShape(net.imglib2.algorithm.neighborhood.RectangleShape) RectangleShape(net.imglib2.algorithm.neighborhood.RectangleShape) ByteType(net.imglib2.type.numeric.integer.ByteType) Neighborhood(net.imglib2.algorithm.neighborhood.Neighborhood) RectangleNeighborhood(net.imglib2.algorithm.neighborhood.RectangleNeighborhood) Test(org.junit.Test)

Example 3 with RectangleNeighborhood

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

the class IntegralCursorTest method testIntegralCursorCopyConstructor.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testIntegralCursorCopyConstructor() {
    Shape rectangleShape = new RectangleShape(1, false);
    IterableInterval<Neighborhood<ByteType>> ii = rectangleShape.neighborhoodsSafe(img);
    Cursor<Neighborhood<ByteType>> cursor = ii.cursor();
    // Manually select the neighborhood that is centered on the image
    cursor.fwd();
    cursor.fwd();
    cursor.fwd();
    cursor.fwd();
    cursor.fwd();
    IntegralCursor<ByteType> integralCursor = new IntegralCursor<>((RectangleNeighborhood) cursor.get());
    assertEquals(integralCursor.next(), new ByteType((byte) 1));
    assertEquals(integralCursor.next(), new ByteType((byte) 2));
    IntegralCursor<ByteType> integralCursor2 = new IntegralCursor<>(integralCursor);
    assertEquals(integralCursor2.next(), new ByteType((byte) 5));
    assertEquals(integralCursor2.next(), new ByteType((byte) 4));
    assertFalse(integralCursor2.hasNext());
}
Also used : Shape(net.imglib2.algorithm.neighborhood.Shape) RectangleShape(net.imglib2.algorithm.neighborhood.RectangleShape) RectangleShape(net.imglib2.algorithm.neighborhood.RectangleShape) ByteType(net.imglib2.type.numeric.integer.ByteType) Neighborhood(net.imglib2.algorithm.neighborhood.Neighborhood) RectangleNeighborhood(net.imglib2.algorithm.neighborhood.RectangleNeighborhood) Test(org.junit.Test)

Example 4 with RectangleNeighborhood

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

the class IntegralMean 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>> cursor = new IntegralCursor<>(input);
    final int dimensions = input.numDimensions();
    // Compute \sum (-1)^{dim - ||cornerVector||_{1}} * I(x^{cornerVector})
    final DoubleType sum = new DoubleType();
    sum.setZero();
    // Convert from input to return type
    final Converter<I, DoubleType> conv = new RealDoubleConverter<>();
    final DoubleType valueAsDoubleType = new DoubleType();
    while (cursor.hasNext()) {
        final I value = cursor.next().get(0).copy();
        conv.convert(value, valueAsDoubleType);
        // Obtain the cursor position encoded as corner vector
        final int cornerInteger = cursor.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(cornerInteger)));
        valueAsDoubleType.mul(factor);
        sum.add(valueAsDoubleType);
    }
    final int area = (int) Intervals.numElements(Intervals.expand(input, -1l));
    // Compute mean by dividing the sum divided by the number of elements
    // NB: Reuse DoubleType
    valueAsDoubleType.set(area);
    sum.div(valueAsDoubleType);
    output.set(sum);
}
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 5 with RectangleNeighborhood

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

the class IntegralSum method compute.

@Override
public void compute(final RectangleNeighborhood<I> input, final DoubleType output) {
    // computation according to
    // https://en.wikipedia.org/wiki/Summed_area_table
    final IntegralCursor<I> cursor = new IntegralCursor<>(input);
    final int dimensions = input.numDimensions();
    // Compute \sum (-1)^{dim - ||cornerVector||_{1}} * I(x^{cornerVector})
    final DoubleType sum = new DoubleType();
    sum.setZero();
    // Convert from input to return type
    final Converter<I, DoubleType> conv = new RealDoubleConverter<>();
    final DoubleType valueAsDoubleType = new DoubleType();
    while (cursor.hasNext()) {
        final I value = cursor.next().copy();
        conv.convert(value, valueAsDoubleType);
        // Obtain the cursor position encoded as corner vector
        final int cornerInteger = cursor.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(cornerInteger)));
        valueAsDoubleType.mul(factor);
        sum.add(valueAsDoubleType);
    }
    output.set(sum);
}
Also used : DoubleType(net.imglib2.type.numeric.real.DoubleType) IntegralCursor(net.imagej.ops.image.integral.IntegralCursor) RealDoubleConverter(net.imglib2.converter.RealDoubleConverter)

Aggregations

IntegralCursor (net.imagej.ops.image.integral.IntegralCursor)3 RealDoubleConverter (net.imglib2.converter.RealDoubleConverter)3 DoubleType (net.imglib2.type.numeric.real.DoubleType)3 Neighborhood (net.imglib2.algorithm.neighborhood.Neighborhood)2 RectangleNeighborhood (net.imglib2.algorithm.neighborhood.RectangleNeighborhood)2 RectangleShape (net.imglib2.algorithm.neighborhood.RectangleShape)2 Shape (net.imglib2.algorithm.neighborhood.Shape)2 ByteType (net.imglib2.type.numeric.integer.ByteType)2 Composite (net.imglib2.view.composite.Composite)2 Test (org.junit.Test)2