Search in sources :

Example 26 with ArrayImgFactory

use of net.imglib2.img.array.ArrayImgFactory in project imagej-ops by imagej.

the class CopyIITest method copyTypeTest.

@Test
public void copyTypeTest() {
    Img<FloatType> inputFloat = new ArrayImgFactory<FloatType>().create(new int[] { 120, 100 }, new FloatType());
    @SuppressWarnings("unchecked") Img<FloatType> output = (Img<FloatType>) ops.run(CopyII.class, inputFloat);
    assertTrue("Should be FloatType.", output.firstElement() instanceof FloatType);
}
Also used : Img(net.imglib2.img.Img) FloatType(net.imglib2.type.numeric.real.FloatType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 27 with ArrayImgFactory

use of net.imglib2.img.array.ArrayImgFactory in project imagej-ops by imagej.

the class CopyImgTest method createData.

@Before
public void createData() {
    input = new ArrayImgFactory<DoubleType>().create(new int[] { 120, 100 }, new DoubleType());
    final Random r = new Random(System.currentTimeMillis());
    final Cursor<DoubleType> inc = input.cursor();
    while (inc.hasNext()) {
        inc.next().set(r.nextDouble());
    }
}
Also used : Random(java.util.Random) DoubleType(net.imglib2.type.numeric.real.DoubleType) ArrayImgFactory(net.imglib2.img.array.ArrayImgFactory) Before(org.junit.Before)

Example 28 with ArrayImgFactory

use of net.imglib2.img.array.ArrayImgFactory in project imagej-ops by imagej.

the class DeconvolveTest method testDeconvolve.

@Test
public void testDeconvolve() {
    int[] size = new int[] { 225, 167 };
    int[] kernelSize = new int[] { 27, 39 };
    // create an input with a small sphere at the center
    Img<FloatType> in = new ArrayImgFactory<FloatType>().create(size, new FloatType());
    placeSphereInCenter(in);
    // create a kernel with a small sphere in the center
    Img<FloatType> kernel = new ArrayImgFactory<FloatType>().create(kernelSize, new FloatType());
    placeSphereInCenter(kernel);
    // convolve and calculate the sum of output
    @SuppressWarnings("unchecked") final Img<FloatType> convolved = (Img<FloatType>) ops.run(ConvolveFFTF.class, in, kernel);
    @SuppressWarnings("unchecked") final RandomAccessibleInterval<FloatType> deconvolved2 = (RandomAccessibleInterval<FloatType>) ops.run(RichardsonLucyF.class, convolved, kernel, null, new OutOfBoundsConstantValueFactory<>(Util.getTypeFromInterval(in).createVariable()), 10);
    assertEquals(size[0], deconvolved2.dimension(0));
    assertEquals(size[1], deconvolved2.dimension(1));
    final Cursor<FloatType> deconvolved2Cursor = Views.iterable(deconvolved2).cursor();
    float[] deconvolved2Values = { 1.0936068E-14f, 2.9685445E-14f, 4.280788E-15f, 3.032084E-18f, 1.1261E-39f, 0.0f, -8.7E-44f, -8.11881E-31f, -2.821192E-18f, 1.8687104E-20f, -2.927517E-23f, 1.2815774E-29f, -1.0611375E-19f, -5.2774515E-21f, -6.154334E-20f };
    for (int i = 0; i < deconvolved2Values.length; i++) {
        assertEquals(deconvolved2Values[i], deconvolved2Cursor.next().get(), 0.0f);
    }
}
Also used : Img(net.imglib2.img.Img) ConvolveFFTF(net.imagej.ops.filter.convolve.ConvolveFFTF) Point(net.imglib2.Point) FloatType(net.imglib2.type.numeric.real.FloatType) OutOfBoundsConstantValueFactory(net.imglib2.outofbounds.OutOfBoundsConstantValueFactory) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Example 29 with ArrayImgFactory

use of net.imglib2.img.array.ArrayImgFactory in project vcell by virtualcell.

the class ConstructTIRFGeometry method run.

@Override
public void run() {
    // Calculate constant d in TIRF exponential decay function
    // Angle of incidence in radians
    theta = theta * 2 * Math.PI / 360;
    // Refractive index of glass
    final double n1 = 1.52;
    // Refractive index of cytosol
    final double n2 = 1.38;
    final double d = lambda * Math.pow((Math.pow(n1, 2) * Math.pow(Math.sin(theta), 2) - Math.pow(n2, 2)), -0.5) / (4 * Math.PI);
    System.out.println("d: " + d);
    final double fluorPerMolecule = 250;
    // Get frame of interest to define geometry
    long maxX = data.dimension(0) - 1;
    long maxY = data.dimension(1) - 1;
    Interval interval = Intervals.createMinMax(0, 0, sliceIndex, maxX, maxY, sliceIndex);
    RandomAccessibleInterval<T> croppedRAI = ops.transform().crop(data, interval, true);
    // Subtract lowest pixel value
    IterableInterval<T> dataII = Views.iterable(croppedRAI);
    double min = ops.stats().min(dataII).getRealDouble();
    Cursor<T> dataCursor = dataII.cursor();
    while (dataCursor.hasNext()) {
        double val = dataCursor.next().getRealDouble();
        dataCursor.get().setReal(val - min);
    }
    // Perform Gaussian blur
    RandomAccessibleInterval<T> blurredRAI = ops.filter().gauss(croppedRAI, 2);
    IterableInterval<T> blurredII = Views.iterable(blurredRAI);
    // Segment slice by threshold and fill holes
    IterableInterval<BitType> thresholded = ops.threshold().huang(blurredII);
    Img<BitType> thresholdedImg = ops.convert().bit(thresholded);
    RandomAccessibleInterval<BitType> thresholdedRAI = ops.morphology().fillHoles(thresholdedImg);
    // Get the largest region
    RandomAccessibleInterval<LabelingType<ByteType>> labeling = ops.labeling().cca(thresholdedRAI, ConnectedComponents.StructuringElement.EIGHT_CONNECTED);
    LabelRegions<ByteType> labelRegions = new LabelRegions<>(labeling);
    Iterator<LabelRegion<ByteType>> iterator = labelRegions.iterator();
    LabelRegion<ByteType> maxRegion = iterator.next();
    while (iterator.hasNext()) {
        LabelRegion<ByteType> currRegion = iterator.next();
        if (currRegion.size() > maxRegion.size()) {
            maxRegion = currRegion;
        }
    }
    // Generate z index map
    double iMax = ops.stats().max(dataII).getRealDouble();
    Img<UnsignedShortType> dataImg = ops.convert().uint16(dataII);
    Img<UnsignedShortType> zMap = ops.convert().uint16(ops.create().img(dataII));
    LabelRegionCursor cursor = maxRegion.localizingCursor();
    RandomAccess<UnsignedShortType> zMapRA = zMap.randomAccess();
    RandomAccess<UnsignedShortType> dataRA = dataImg.randomAccess();
    while (cursor.hasNext()) {
        cursor.fwd();
        zMapRA.setPosition(cursor);
        dataRA.setPosition(cursor);
        double val = dataRA.get().getRealDouble();
        // Log of 0 is undefined
        if (val < 1) {
            val = 1;
        }
        int z = (int) Math.round(-d * Math.log(val / iMax) / zRes);
        zMapRA.get().set(z);
    }
    System.out.println("6");
    // Use map to construct 3D geometry
    // Add 5 slices of padding on top
    int maxZ = (int) ops.stats().max(zMap).getRealDouble() + 5;
    long[] resultDimensions = { maxX + 1, maxY + 1, maxZ };
    Img<BitType> result = new ArrayImgFactory<BitType>().create(resultDimensions, new BitType());
    RandomAccess<BitType> resultRA = result.randomAccess();
    System.out.println(maxZ);
    cursor.reset();
    while (cursor.hasNext()) {
        cursor.fwd();
        zMapRA.setPosition(cursor);
        int zIndex = zMapRA.get().get();
        int[] position = { cursor.getIntPosition(0), cursor.getIntPosition(1), zIndex };
        while (position[2] < maxZ) {
            resultRA.setPosition(position);
            resultRA.get().set(true);
            position[2]++;
        }
    }
    output = datasetService.create(result);
    CalibratedAxis[] axes = new DefaultLinearAxis[] { new DefaultLinearAxis(Axes.X), new DefaultLinearAxis(Axes.Y), new DefaultLinearAxis(Axes.Z) };
    output.setAxes(axes);
    System.out.println("Done constructing geometry");
}
Also used : ByteType(net.imglib2.type.numeric.integer.ByteType) DefaultLinearAxis(net.imagej.axis.DefaultLinearAxis) BitType(net.imglib2.type.logic.BitType) LabelRegionCursor(net.imglib2.roi.labeling.LabelRegionCursor) LabelingType(net.imglib2.roi.labeling.LabelingType) UnsignedShortType(net.imglib2.type.numeric.integer.UnsignedShortType) LabelRegion(net.imglib2.roi.labeling.LabelRegion) CalibratedAxis(net.imagej.axis.CalibratedAxis) LabelRegions(net.imglib2.roi.labeling.LabelRegions) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) Interval(net.imglib2.Interval) IterableInterval(net.imglib2.IterableInterval)

Example 30 with ArrayImgFactory

use of net.imglib2.img.array.ArrayImgFactory in project imagej-ops by imagej.

the class AddDimensionViewTest method addDimensionTest.

@Test
public void addDimensionTest() {
    Img<DoubleType> img = new ArrayImgFactory<DoubleType>().create(new int[] { 10, 10 }, new DoubleType());
    MixedTransformView<DoubleType> il2 = Views.addDimension((RandomAccessible<DoubleType>) img);
    MixedTransformView<DoubleType> opr = ops.transform().addDimensionView((RandomAccessible<DoubleType>) img);
    assertEquals(il2.numDimensions(), opr.numDimensions());
    boolean[] il2Transform = new boolean[3];
    boolean[] oprTransform = new boolean[3];
    il2.getTransformToSource().getComponentZero(il2Transform);
    opr.getTransformToSource().getComponentZero(oprTransform);
    for (int i = 0; i < il2Transform.length; i++) {
        assertEquals(il2Transform[i], oprTransform[i]);
    }
}
Also used : DoubleType(net.imglib2.type.numeric.real.DoubleType) AbstractOpTest(net.imagej.ops.AbstractOpTest) Test(org.junit.Test)

Aggregations

AbstractOpTest (net.imagej.ops.AbstractOpTest)52 Test (org.junit.Test)52 DoubleType (net.imglib2.type.numeric.real.DoubleType)47 Random (java.util.Random)14 Img (net.imglib2.img.Img)8 FinalInterval (net.imglib2.FinalInterval)7 ArrayImgFactory (net.imglib2.img.array.ArrayImgFactory)7 FloatType (net.imglib2.type.numeric.real.FloatType)6 FinalDimensions (net.imglib2.FinalDimensions)4 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)4 ArrayList (java.util.ArrayList)3 Dimensions (net.imglib2.Dimensions)3 Point (net.imglib2.Point)3 MixedTransformView (net.imglib2.view.MixedTransformView)3 Before (org.junit.Before)3 BinaryFunctionOp (net.imagej.ops.special.function.BinaryFunctionOp)2 UnaryFunctionOp (net.imagej.ops.special.function.UnaryFunctionOp)2 Interval (net.imglib2.Interval)2 ComplexFloatType (net.imglib2.type.numeric.complex.ComplexFloatType)2 ByteType (net.imglib2.type.numeric.integer.ByteType)2