Search in sources :

Example 1 with Dimensions

use of net.imglib2.Dimensions in project vcell by virtualcell.

the class DeconstructGeometryCommand method run.

@Override
public void run() {
    // Crop to get a z-stack over time (remove channel dimension)
    long maxX = fluorData.max(fluorData.dimensionIndex(Axes.X));
    long maxY = fluorData.max(fluorData.dimensionIndex(Axes.Y));
    long maxZ = fluorData.max(fluorData.dimensionIndex(Axes.Z));
    long maxTime = fluorData.max(fluorData.dimensionIndex(Axes.TIME));
    Img fluorImg = fluorData.getImgPlus().getImg();
    FinalInterval intervals = Intervals.createMinMax(0, 0, 0, 0, 0, maxX, maxY, maxZ, 0, maxTime);
    RandomAccessibleInterval fluorImgCropped = ops.transform().crop(fluorImg, intervals, true);
    // Calculate scale factors
    double[] scaleFactors = { 1, 1, 1, 1 };
    for (int i = 0; i < geomData.numDimensions(); i++) {
        scaleFactors[i] = geomData.dimension(i) / (double) fluorImgCropped.dimension(i);
    }
    // Scale the fluorescence dataset to match the geometry
    NLinearInterpolatorFactory interpolatorFactory = new NLinearInterpolatorFactory();
    RandomAccessibleInterval fluorScaled = ops.transform().scale(fluorImgCropped, scaleFactors, interpolatorFactory);
    // Crop out the first slice of each z-stack in time series
    intervals = Intervals.createMinMax(0, 0, 0, 0, fluorScaled.dimension(0) - 1, fluorScaled.dimension(1) - 1, 0, fluorScaled.dimension(3) - 1);
    IntervalView fluorXYT = (IntervalView) ops.transform().crop(fluorScaled, intervals, true);
    // Create a blank image of the same X-Y-Time dimensions
    long[] dimensions = { fluorXYT.dimension(0), fluorXYT.dimension(1), fluorXYT.dimension(2) };
    Img<DoubleType> result = ops.create().img(dimensions);
    // Calculate constant d in TIRF exponential decay function
    theta = theta * 2 * Math.PI / 360;
    double n1 = 1.52;
    double n2 = 1.38;
    double d = lambda * Math.pow((Math.pow(n1, 2) * Math.pow(Math.sin(theta), 2) - Math.pow(n2, 2)), -0.5) / (4 * Math.PI);
    // Iterate through each time point, using 3D geometry to generate 2D intensities
    Cursor<DoubleType> cursor = fluorXYT.localizingCursor();
    RandomAccess fluorRA = fluorScaled.randomAccess();
    RandomAccess<RealType<?>> geomRA = geomData.randomAccess();
    RandomAccess<DoubleType> resultRA = result.randomAccess();
    maxZ = geomData.dimension(2) - 1;
    while (cursor.hasNext()) {
        cursor.fwd();
        int[] positionXYZ = { cursor.getIntPosition(0), cursor.getIntPosition(1), (int) maxZ - 1 };
        int[] positionXYZT = { cursor.getIntPosition(0), cursor.getIntPosition(1), (int) maxZ - 1, cursor.getIntPosition(2) };
        resultRA.setPosition(cursor);
        geomRA.setPosition(positionXYZ);
        double sum = 0.0;
        while (positionXYZ[2] >= 0 && geomRA.get().getRealDouble() != 0.0) {
            fluorRA.setPosition(positionXYZT);
            geomRA.setPosition(positionXYZ);
            sum += geomRA.get().getRealDouble() * Math.exp(-zSpacing * positionXYZ[2] / d);
            positionXYZ[2]--;
        }
        resultRA.get().set(sum);
    }
    System.out.println("done");
    displayService.createDisplay(result);
}
Also used : Img(net.imglib2.img.Img) NLinearInterpolatorFactory(net.imglib2.interpolation.randomaccess.NLinearInterpolatorFactory) RandomAccess(net.imglib2.RandomAccess) RealType(net.imglib2.type.numeric.RealType) IntervalView(net.imglib2.view.IntervalView) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) DoubleType(net.imglib2.type.numeric.real.DoubleType) FinalInterval(net.imglib2.FinalInterval)

Example 2 with Dimensions

use of net.imglib2.Dimensions in project vcell by virtualcell.

the class VCellResultService method importCsv.

public Dataset importCsv(File directory) throws FileNotFoundException {
    File[] files = directory.listFiles();
    // TODO: Better handling
    if (files == null)
        return null;
    ArrayList<ArrayList<Float>> timeSeries = new ArrayList<>(files.length);
    Scanner scanner;
    int dataSize = 0;
    for (File file : files) {
        scanner = new Scanner(file);
        scanner.useDelimiter("[,\n]");
        while (scanner.hasNext() && !scanner.hasNextDouble()) {
            scanner.next();
        }
        if (!scanner.hasNextDouble()) {
            scanner.close();
            return null;
        }
        ArrayList<Float> data = new ArrayList<>();
        while (scanner.hasNextDouble()) {
            data.add(scanner.nextFloat());
        }
        scanner.close();
        timeSeries.add(data);
        dataSize = data.size();
    }
    int[] dimensions = { dataSize, timeSeries.size() };
    Img<FloatType> img = new ArrayImgFactory<FloatType>().create(dimensions, new FloatType());
    Cursor<FloatType> cursor = img.localizingCursor();
    while (cursor.hasNext()) {
        cursor.next();
        int xPos = cursor.getIntPosition(0);
        int tPos = cursor.getIntPosition(1);
        Float val = timeSeries.get(tPos).get(xPos);
        cursor.get().set(val);
    }
    Dataset dataset = datasetService.create(img);
    // Drop single dimensions
    @SuppressWarnings("unchecked") ImgPlus<FloatType> imgPlus = (ImgPlus<FloatType>) dataset.getImgPlus();
    FinalInterval interval = Intervals.createMinMax(0, 0, imgPlus.dimension(0) - 1, imgPlus.dimension(1) - 1);
    ImgPlus<FloatType> cropped = ops.transform().crop(imgPlus, interval, true);
    dataset.setImgPlus(cropped);
    System.out.println(dataset.numDimensions());
    dataset.setName(directory.getName());
    return dataset;
}
Also used : Scanner(java.util.Scanner) ImgPlus(net.imagej.ImgPlus) Dataset(net.imagej.Dataset) ArrayList(java.util.ArrayList) FloatType(net.imglib2.type.numeric.real.FloatType) FinalInterval(net.imglib2.FinalInterval) File(java.io.File)

Example 3 with Dimensions

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

the class DefaultCreateKernelGabor method calculate.

@Override
public RandomAccessibleInterval<T> calculate(final double[] sigmas, final double[] period) {
    // both input arrays must be of the same length
    if (sigmas.length != period.length)
        throw new IllegalArgumentException("Params length mismatch: The number " + "of sigmas must match the dimensionality of the period vector.");
    // NB: sigma==0 indicates no filtering along its axis
    for (final double s : sigmas) if (s < 0.0)
        throw new IllegalArgumentException("Input sigma must be non-negative.");
    // the size and center of the output image
    final long[] dims = new long[sigmas.length];
    final long[] centre = new long[sigmas.length];
    for (int d = 0; d < dims.length; d++) {
        dims[d] = Math.max(3, (2 * (int) (3 * sigmas[d] + 0.5) + 1));
        centre[d] = (int) (dims[d] / 2);
    }
    // prepare the output image
    final RandomAccessibleInterval<T> out = createImgOp.calculate(new FinalInterval(dims));
    // calculate the squared length of the period vector
    double perLengthSq = 0.0;
    for (int d = 0; d < period.length; d++) perLengthSq += period[d] * period[d];
    // fill the output image
    final Cursor<T> cursor = Views.iterable(out).cursor();
    while (cursor.hasNext()) {
        cursor.fwd();
        // obtain the current coordinate (use dims to store it)
        cursor.localize(dims);
        // to calculate current Gabor kernel value
        double GaussExp = 0.0;
        double freqPart = 0.0;
        // but produce no Gaussian envelope for axes for which sigma==0
        // no blocking by default
        double blockingExp = 1.0;
        // sweep over all dimensions to determine voxel value
        for (int d = 0; d < dims.length; d++) {
            final double dx = dims[d] - centre[d];
            if (sigmas[d] > 0.)
                // normal case: cummulate exp's argument
                GaussExp += (dx * dx) / (sigmas[d] * sigmas[d]);
            else if (dx != 0.)
                // sigmas[d] == 0 && we are off the blocking axis
                blockingExp = 0.f;
            // cummulates scalar product...
            freqPart += dx * period[d];
        }
        GaussExp = Math.exp(-0.5 * GaussExp) * blockingExp;
        freqPart = 6.28318 * freqPart / perLengthSq;
        // compose the real value finally
        cursor.get().setReal(GaussExp * Math.cos(freqPart));
        // TODO NB: is it faster to determine type or calculate the math (possible uselessly)
        if (!(typeVar instanceof RealType<?>))
            // set then the imaginary part of the kernel too
            cursor.get().setImaginary(GaussExp * Math.sin(freqPart));
    }
    return out;
}
Also used : FinalInterval(net.imglib2.FinalInterval) RealType(net.imglib2.type.numeric.RealType)

Example 4 with Dimensions

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

the class Convolve method run.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void run() {
    // TODO: get the dimension indices from the image dependent on the
    // selected
    // axes -> OR: just map the kernel dimension labels to the image
    // dimension
    // labels
    final int[] axisIndices = new int[] { 0, 1 };
    // number of indicies must be conform with the dimensionality of axes
    if (axes.length != axisIndices.length) {
        throw new IllegalArgumentException("The number of selected dimension doesn't conforms with the kernel size.");
    }
    if (asFloat) {
        try {
            out = (ImgPlus) in.factory().imgFactory(new FloatType()).create(in, new FloatType());
        } catch (final IncompatibleTypeException e) {
            throw new IllegalArgumentException(e);
        }
    } else {
        out = (ImgPlus<O>) in.factory().create(in, in.firstElement().createVariable());
    }
    final Op op = ops.op(Ops.Filter.Convolve.class, out, in, kernel);
    if (in.numDimensions() > kernel.numDimensions()) {
        if (op instanceof UnaryComputerOp) {
            // if the selected convolve op is a function and the kernel dimensions
            // doesn't match the input image dimensions, than we can still convolve
            // each slice individually
            ops.run(Ops.Slice.class, out, in, op, axisIndices);
        } else {
            throw new IllegalArgumentException("The input image has more dimensions than the kernel!");
        }
    } else if (in.numDimensions() == kernel.numDimensions()) {
        // no 'slicing' necessary
        ops.run(op, out, in, kernel);
    }
}
Also used : Op(net.imagej.ops.Op) UnaryComputerOp(net.imagej.ops.special.computer.UnaryComputerOp) UnaryComputerOp(net.imagej.ops.special.computer.UnaryComputerOp) Ops(net.imagej.ops.Ops) IncompatibleTypeException(net.imglib2.exception.IncompatibleTypeException) FloatType(net.imglib2.type.numeric.real.FloatType) ItemIO(org.scijava.ItemIO)

Example 5 with Dimensions

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

the class AbstractThin method createOutput.

@Override
public RandomAccessibleInterval<BitType> createOutput(final RandomAccessibleInterval<BitType> input) {
    final long[] dims = new long[input.numDimensions()];
    input.dimensions(dims);
    final FinalDimensions dimensions = new FinalDimensions(dims);
    return ops().create().img(dimensions, new BitType());
}
Also used : FinalDimensions(net.imglib2.FinalDimensions) BitType(net.imglib2.type.logic.BitType)

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