Search in sources :

Example 1 with Interval

use of org.hl7.elm.r1.Interval in project labkit-ui by juglab.

the class ParallelUtils method applyOperationOnCells.

/**
 * Divides the given image into cells of size cellDimensions and executes the
 * given the given operation on each chunk. Imglib2 {@link Parallelization} is
 * used to parallelize this process. The {@link ProgressWriter} can be used to
 * visualize the progress or cancel the operation.
 */
public static <T> void applyOperationOnCells(RandomAccessibleInterval<T> image, int[] cellDimensions, Consumer<RandomAccessibleInterval<T>> operation, ProgressWriter progressWriter) {
    List<Interval> cells = getCells(new CellGrid(Intervals.dimensionsAsLongArray(image), cellDimensions));
    AtomicInteger numStarted = new AtomicInteger(0);
    AtomicInteger numFinished = new AtomicInteger(0);
    int n = cells.size();
    Parallelization.getTaskExecutor().forEach(cells, cell -> {
        AtomicBoolean cancelled = new AtomicBoolean(false);
        if (cancelled.get())
            throw new CancellationException();
        try {
            progressWriter.out().println("Chunk " + numStarted.incrementAndGet() + " of " + n);
            operation.accept(Views.interval(image, cell));
            progressWriter.setProgress((double) numFinished.incrementAndGet() / n);
        } catch (CancellationException e) {
            cancelled.set(true);
            throw e;
        }
    });
    progressWriter.setProgress(1.0);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CancellationException(java.util.concurrent.CancellationException) CellGrid(net.imglib2.img.cell.CellGrid) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval) Interval(net.imglib2.Interval) FinalInterval(net.imglib2.FinalInterval)

Example 2 with Interval

use of org.hl7.elm.r1.Interval in project labkit-ui by juglab.

the class EllipsoidTest method testBoundingBox.

@Test
public void testBoundingBox() {
    Ellipsoid ellipsoid = new Ellipsoid(new double[] { 1, 1 }, new double[] { 0.5, 0.5 });
    Interval boundingBox = ellipsoid.boundingBox();
    assertEquals(Intervals.createMinMax(0, 0, 2, 2), boundingBox);
}
Also used : Interval(net.imglib2.Interval) Test(org.junit.Test)

Example 3 with Interval

use of org.hl7.elm.r1.Interval in project bigwarp by saalfeldlab.

the class BigWarp method exportAsImagePlus.

public void exportAsImagePlus(boolean virtual, String path) {
    if (ij == null)
        return;
    final GenericDialogPlus gd = new GenericDialogPlus("Apply Big Warp transform");
    gd.addMessage("Field of view and resolution:");
    gd.addChoice("Resolution", new String[] { ApplyBigwarpPlugin.TARGET, ApplyBigwarpPlugin.MOVING, ApplyBigwarpPlugin.SPECIFIED }, ApplyBigwarpPlugin.TARGET);
    gd.addChoice("Field of view", new String[] { ApplyBigwarpPlugin.TARGET, ApplyBigwarpPlugin.MOVING_WARPED, ApplyBigwarpPlugin.UNION_TARGET_MOVING, ApplyBigwarpPlugin.LANDMARK_POINTS, ApplyBigwarpPlugin.LANDMARK_POINT_CUBE_PIXEL, ApplyBigwarpPlugin.LANDMARK_POINT_CUBE_PHYSICAL, ApplyBigwarpPlugin.SPECIFIED_PIXEL, ApplyBigwarpPlugin.SPECIFIED_PHYSICAL }, ApplyBigwarpPlugin.TARGET);
    gd.addStringField("point filter", "");
    gd.addMessage("Resolution");
    gd.addNumericField("x", 1.0, 4);
    gd.addNumericField("y", 1.0, 4);
    gd.addNumericField("z", 1.0, 4);
    gd.addMessage("Offset");
    gd.addNumericField("x", 0.0, 4);
    gd.addNumericField("y", 0.0, 4);
    gd.addNumericField("z", 0.0, 4);
    gd.addMessage("Field of view");
    gd.addNumericField("x", -1, 0);
    gd.addNumericField("y", -1, 0);
    gd.addNumericField("z", -1, 0);
    gd.addMessage("Other Output options");
    gd.addChoice("Interpolation", new String[] { "Nearest Neighbor", "Linear" }, "Linear");
    gd.addMessage("Virtual: fast to display,\n" + "low memory requirements,\nbut slow to navigate");
    gd.addCheckbox("virtual?", false);
    int defaultCores = (int) Math.ceil(Runtime.getRuntime().availableProcessors() / 4);
    gd.addNumericField("threads", defaultCores, 0);
    gd.addMessage("Writing options (leave empty to opena new image window)");
    gd.addDirectoryOrFileField("File or n5 root", "");
    gd.addStringField("n5 dataset", "");
    gd.addStringField("n5 block size", "32");
    gd.addChoice("n5 compression", new String[] { N5Exporter.GZIP_COMPRESSION, N5Exporter.RAW_COMPRESSION, N5Exporter.LZ4_COMPRESSION, N5Exporter.XZ_COMPRESSION, N5Exporter.BLOSC_COMPRESSION }, N5Exporter.GZIP_COMPRESSION);
    gd.showDialog();
    if (gd.wasCanceled())
        return;
    final String resolutionOption = gd.getNextChoice();
    final String fieldOfViewOption = gd.getNextChoice();
    final String fieldOfViewPointFilter = gd.getNextString();
    final double[] resolutionSpec = new double[3];
    resolutionSpec[0] = gd.getNextNumber();
    resolutionSpec[1] = gd.getNextNumber();
    resolutionSpec[2] = gd.getNextNumber();
    final double[] offsetSpec = new double[3];
    offsetSpec[0] = gd.getNextNumber();
    offsetSpec[1] = gd.getNextNumber();
    offsetSpec[2] = gd.getNextNumber();
    final double[] fovSpec = new double[3];
    fovSpec[0] = gd.getNextNumber();
    fovSpec[1] = gd.getNextNumber();
    fovSpec[2] = gd.getNextNumber();
    final String interpType = gd.getNextChoice();
    final boolean isVirtual = gd.getNextBoolean();
    final int nThreads = (int) gd.getNextNumber();
    final String fileOrN5Root = gd.getNextString();
    final String n5Dataset = gd.getNextString();
    final String blockSizeString = gd.getNextString();
    final String compressionString = gd.getNextChoice();
    final int[] blockSize = ApplyBigwarpPlugin.parseBlockSize(blockSizeString, this.ndims);
    final Compression compression = ApplyBigwarpPlugin.getCompression(compressionString);
    final WriteDestinationOptions writeOpts = new ApplyBigwarpPlugin.WriteDestinationOptions(fileOrN5Root, n5Dataset, blockSize, compression);
    final Interpolation interp;
    if (interpType.equals("Nearest Neighbor"))
        interp = Interpolation.NEARESTNEIGHBOR;
    else
        interp = Interpolation.NLINEAR;
    double[] res = ApplyBigwarpPlugin.getResolution(this.data, resolutionOption, resolutionSpec);
    List<Interval> outputIntervalList = ApplyBigwarpPlugin.getPixelInterval(this.data, this.landmarkModel, this.currentTransform, fieldOfViewOption, fieldOfViewPointFilter, fovSpec, offsetSpec, res);
    final List<String> matchedPtNames = new ArrayList<>();
    if (outputIntervalList.size() > 1)
        ApplyBigwarpPlugin.fillMatchedPointNames(matchedPtNames, getLandmarkPanel().getTableModel(), fieldOfViewPointFilter);
    // landmark centers (because multiple images can be exported this way )
    if (matchedPtNames.size() > 0) {
        BigwarpLandmarkSelectionPanel<T> selection = new BigwarpLandmarkSelectionPanel<>(data, sources, fieldOfViewOption, outputIntervalList, matchedPtNames, interp, offsetSpec, res, isVirtual, nThreads, progressWriter);
    } else {
        if (writeOpts.n5Dataset != null && !writeOpts.n5Dataset.isEmpty()) {
            final String unit = ApplyBigwarpPlugin.getUnit(data, resolutionOption);
            // export async
            new Thread() {

                public void run() {
                    progressWriter.setProgress(0.01);
                    ApplyBigwarpPlugin.runN5Export(data, sources, fieldOfViewOption, outputIntervalList.get(0), interp, offsetSpec, res, unit, progressWriter, writeOpts, Executors.newFixedThreadPool(nThreads));
                    progressWriter.setProgress(1.00);
                }
            }.start();
        } else {
            // export
            final boolean show = (writeOpts.pathOrN5Root == null || writeOpts.pathOrN5Root.isEmpty());
            ApplyBigwarpPlugin.runExport(data, sources, fieldOfViewOption, outputIntervalList, matchedPtNames, interp, offsetSpec, res, isVirtual, nThreads, progressWriter, show, false, writeOpts);
        }
    }
}
Also used : BigwarpLandmarkSelectionPanel(bdv.gui.BigwarpLandmarkSelectionPanel) Compression(org.janelia.saalfeldlab.n5.Compression) GenericDialogPlus(fiji.util.gui.GenericDialogPlus) WriteDestinationOptions(bdv.ij.ApplyBigwarpPlugin.WriteDestinationOptions) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Point(java.awt.Point) RealPoint(net.imglib2.RealPoint) Interpolation(bdv.viewer.Interpolation) Interval(net.imglib2.Interval) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval)

Example 4 with Interval

use of org.hl7.elm.r1.Interval in project bigwarp by saalfeldlab.

the class LandmarkGridGenerator method fillFromDialog.

public static boolean fillFromDialog(final BigWarp bw) {
    LandmarkTableModel ltm = bw.getLandmarkPanel().getTableModel();
    int nd = ltm.ndims;
    final GenericDialog gd = new GenericDialog("Generate landmark grid.");
    gd.addMessage("Field of view and resolution:");
    gd.addMessage("Number of points per dimension (default)");
    gd.addNumericField("nx", 5.0, 2);
    gd.addNumericField("ny", 5.0, 2);
    if (nd > 2)
        gd.addNumericField("nz", 5.0, 2);
    gd.addMessage("Spacing");
    gd.addNumericField("sx", -1.0, 4);
    gd.addNumericField("sy", -1.0, 4);
    if (nd > 2)
        gd.addNumericField("sz", -1.0, 4);
    gd.showDialog();
    if (gd.wasCanceled())
        return false;
    double nx = gd.getNextNumber();
    double ny = gd.getNextNumber();
    double nz = -1;
    if (nd > 2)
        nz = gd.getNextNumber();
    double sx = gd.getNextNumber();
    double sy = gd.getNextNumber();
    double sz = 1;
    if (nd > 2)
        sz = gd.getNextNumber();
    double[] res = ApplyBigwarpPlugin.getResolution(bw.getData(), ApplyBigwarpPlugin.TARGET, null);
    List<Interval> outputIntervalList = ApplyBigwarpPlugin.getPixelInterval(bw.getData(), bw.getLandmarkPanel().getTableModel(), null, ApplyBigwarpPlugin.TARGET, null, null, null, res);
    Interval pixelInterval = outputIntervalList.get(0);
    double[] max = new double[nd];
    for (int i = 0; i < nd; i++) {
        max[i] = res[i] * pixelInterval.dimension(i);
    }
    FinalRealInterval interval = new FinalRealInterval(new double[nd], max);
    LandmarkGridGenerator gen;
    if (sx > 0 || sy > 0 || sz > 0) {
        gen = new LandmarkGridGenerator(interval, new double[] { sx, sy, sz });
    } else {
        gen = new LandmarkGridGenerator(interval, new long[] { (long) nx, (long) ny, (long) nz });
    }
    double N = gen.approxNumberOfPoints();
    System.out.println("N : " + N);
    if (N > 1) {
        final GenericDialog warningDialog = new GenericDialog("Warning");
        warningDialog.addMessage("You are about to add approximately\n" + Math.round(N) + "\npoints.");
        warningDialog.addMessage("This could cause Bigwarp to be slow or crash.");
        warningDialog.addMessage("Proceed?");
        warningDialog.showDialog();
        if (warningDialog.wasCanceled())
            return false;
    }
    gen.fill(ltm);
    return true;
}
Also used : FinalRealInterval(net.imglib2.FinalRealInterval) GenericDialog(ij.gui.GenericDialog) FinalRealInterval(net.imglib2.FinalRealInterval) RealInterval(net.imglib2.RealInterval) Interval(net.imglib2.Interval)

Example 5 with Interval

use of org.hl7.elm.r1.Interval in project bigwarp by saalfeldlab.

the class ApplyBigwarpPlugin method getPixelInterval.

public static Interval getPixelInterval(final Source<?> source, final LandmarkTableModel landmarks, final InvertibleRealTransform transform, final String fieldOfViewOption, final double[] outputResolution) {
    RandomAccessibleInterval<?> rai = source.getSource(0, 0);
    if (fieldOfViewOption.equals(TARGET)) {
        double[] inputres = resolutionFromSource(source);
        long[] max = new long[rai.numDimensions()];
        for (int d = 0; d < rai.numDimensions(); d++) {
            max[d] = (long) Math.ceil((inputres[d] * rai.dimension(d)) / outputResolution[d]);
        }
        return new FinalInterval(max);
    } else if (fieldOfViewOption.equals(MOVING_WARPED)) {
        double[] movingRes = resolutionFromSource(source);
        int ndims = transform.numSourceDimensions();
        AffineTransform movingPixelToPhysical = new AffineTransform(ndims);
        movingPixelToPhysical.set(movingRes[0], 0, 0);
        movingPixelToPhysical.set(movingRes[1], 1, 1);
        if (ndims > 2)
            movingPixelToPhysical.set(movingRes[2], 2, 2);
        AffineTransform outputResolution2Pixel = new AffineTransform(ndims);
        outputResolution2Pixel.set(outputResolution[0], 0, 0);
        outputResolution2Pixel.set(outputResolution[1], 1, 1);
        if (ndims > 2)
            outputResolution2Pixel.set(outputResolution[2], 2, 2);
        RealTransformSequence seq = new RealTransformSequence();
        seq.add(movingPixelToPhysical);
        seq.add(transform.inverse());
        seq.add(outputResolution2Pixel.inverse());
        FinalInterval interval = new FinalInterval(Intervals.minAsLongArray(rai), Intervals.maxAsLongArray(rai));
        return BigWarpExporter.estimateBounds(seq, interval);
    } else if (fieldOfViewOption.equals(UNION_TARGET_MOVING)) {
        final Interval movingWarpedInterval = getPixelInterval(source, landmarks, transform, MOVING_WARPED, outputResolution);
        final Interval targetInterval = getPixelInterval(source, landmarks, transform, TARGET, outputResolution);
        return Intervals.union(movingWarpedInterval, targetInterval);
    }
    return null;
}
Also used : FinalInterval(net.imglib2.FinalInterval) AffineTransform(net.imglib2.realtransform.AffineTransform) RealTransformSequence(net.imglib2.realtransform.RealTransformSequence) FinalInterval(net.imglib2.FinalInterval) Interval(net.imglib2.Interval) RandomAccessibleInterval(net.imglib2.RandomAccessibleInterval)

Aggregations

Interval (net.imglib2.Interval)59 RandomAccessibleInterval (net.imglib2.RandomAccessibleInterval)37 FinalInterval (net.imglib2.FinalInterval)34 ArrayList (java.util.ArrayList)19 List (java.util.List)16 Test (org.junit.Test)14 AffineTransform3D (net.imglib2.realtransform.AffineTransform3D)13 RealInterval (net.imglib2.RealInterval)8 HashMap (java.util.HashMap)7 Interval (org.opencds.cqf.cql.engine.runtime.Interval)7 Arrays (java.util.Arrays)6 Collectors (java.util.stream.Collectors)6 RealPoint (net.imglib2.RealPoint)6 FloatType (net.imglib2.type.numeric.real.FloatType)6 ImgPlus (net.imagej.ImgPlus)5 FinalDimensions (net.imglib2.FinalDimensions)5 IterableInterval (net.imglib2.IterableInterval)5 Dimensions (net.imglib2.Dimensions)4 Intervals (net.imglib2.util.Intervals)4 Labeling (sc.fiji.labkit.ui.labeling.Labeling)4