Search in sources :

Example 1 with N5Writer

use of org.janelia.saalfeldlab.n5.N5Writer in project bigwarp by saalfeldlab.

the class ApplyBigwarpPlugin method runN5Export.

public static <S, T extends NativeType<T> & NumericType<T>> void runN5Export(final BigWarpData<S> data, final List<SourceAndConverter<S>> sources, final String fieldOfViewOption, final Interval outputInterval, final Interpolation interp, final double[] offset, final double[] resolution, final String unit, final ProgressWriter progressWriter, final WriteDestinationOptions writeOpts, final ExecutorService exec) {
    final int nd = BigWarp.detectNumDims(data.sources);
    // setup n5 parameters
    final String dataset = writeOpts.n5Dataset;
    final int[] blockSize = writeOpts.blockSize;
    final Compression compression = writeOpts.compression;
    if (dataset == null || dataset.isEmpty()) {
        return;
    }
    N5Writer n5;
    try {
        n5 = new N5Factory().openWriter(writeOpts.pathOrN5Root);
    } catch (IOException e1) {
        e1.printStackTrace();
        return;
    }
    // build metadata
    final String[] axes = nd == 2 ? new String[] { "y", "x" } : new String[] { "z", "y", "x" };
    final String[] units = nd == 2 ? new String[] { unit, unit } : new String[] { unit, unit, unit };
    final N5CosemMetadata metadata = new N5CosemMetadata("", new N5CosemMetadata.CosemTransform(axes, resolution, offset, units), null);
    N5CosemMetadataParser parser = new N5CosemMetadataParser();
    // setup physical to pixel transform
    final AffineTransform3D resolutionTransform = new AffineTransform3D();
    resolutionTransform.set(resolution[0], 0, 0);
    resolutionTransform.set(resolution[1], 1, 1);
    if (resolution.length > 2)
        resolutionTransform.set(resolution[2], 2, 2);
    final AffineTransform3D offsetTransform = new AffineTransform3D();
    offsetTransform.set(offset[0], 0, 3);
    offsetTransform.set(offset[1], 1, 3);
    if (resolution.length > 2)
        offsetTransform.set(offset[2], 2, 3);
    final AffineTransform3D pixelRenderToPhysical = new AffineTransform3D();
    pixelRenderToPhysical.concatenate(resolutionTransform);
    pixelRenderToPhysical.concatenate(offsetTransform);
    // render and write
    final int N = data.movingSourceIndices.length;
    for (int i = 0; i < N; i++) {
        final int movingSourceIndex = data.movingSourceIndices[i];
        @SuppressWarnings("unchecked") final RealRandomAccessible<T> raiRaw = (RealRandomAccessible<T>) sources.get(movingSourceIndex).getSpimSource().getInterpolatedSource(0, 0, interp);
        // to pixel space
        final AffineRandomAccessible<T, AffineGet> rai = RealViews.affine(raiRaw, pixelRenderToPhysical.inverse());
        final IntervalView<T> img = Views.interval(Views.raster(rai), Intervals.zeroMin(outputInterval));
        final String srcName = data.sources.get(data.movingSourceIndices[i]).getSpimSource().getName();
        String destDataset = dataset;
        if (N > 1)
            destDataset = dataset + String.format("/%s", srcName.replace(" ", "_"));
        RandomAccessibleInterval<T> imgToWrite;
        if (nd == 2)
            imgToWrite = Views.hyperSlice(img, 2, 0);
        else
            imgToWrite = img;
        try {
            N5Utils.save(imgToWrite, n5, destDataset, blockSize, compression, exec);
            if (parser != null && metadata != null)
                parser.writeMetadata(metadata, n5, destDataset);
        } catch (Exception e) {
            e.printStackTrace();
        }
        progressWriter.setProgress((i + 1) / ((double) N));
    }
    progressWriter.setProgress(1.0);
}
Also used : N5CosemMetadata(org.janelia.saalfeldlab.n5.metadata.N5CosemMetadata) Compression(org.janelia.saalfeldlab.n5.Compression) RawCompression(org.janelia.saalfeldlab.n5.RawCompression) GzipCompression(org.janelia.saalfeldlab.n5.GzipCompression) Lz4Compression(org.janelia.saalfeldlab.n5.Lz4Compression) BloscCompression(org.janelia.saalfeldlab.n5.blosc.BloscCompression) XzCompression(org.janelia.saalfeldlab.n5.XzCompression) N5Writer(org.janelia.saalfeldlab.n5.N5Writer) AffineGet(net.imglib2.realtransform.AffineGet) IOException(java.io.IOException) N5CosemMetadataParser(org.janelia.saalfeldlab.n5.metadata.N5CosemMetadataParser) IOException(java.io.IOException) RealRandomAccessible(net.imglib2.RealRandomAccessible) N5Factory(org.janelia.saalfeldlab.n5.ij.N5Factory) AffineTransform3D(net.imglib2.realtransform.AffineTransform3D)

Example 2 with N5Writer

use of org.janelia.saalfeldlab.n5.N5Writer in project bigwarp by saalfeldlab.

the class BigWarpToDeformationFieldPlugIn method writeN5.

public static void writeN5(final String n5BasePath, final String n5Dataset, final LandmarkTableModel ltm, final long[] dims, final double[] spacing, final int[] spatialBlockSize, final Compression compression, final int nThreads) throws IOException {
    final ThinPlateR2LogRSplineKernelTransform tpsRaw = ltm.getTransform();
    final AffineGet affine = toAffine(tpsRaw);
    /*
		 * "remove the affine" from the total transform
		 * by concatenating the inverse of the affine to be removed
		 */
    final ThinplateSplineTransform tpsTotal = new ThinplateSplineTransform(tpsRaw);
    final RealTransformSequence seq = new RealTransformSequence();
    seq.add(tpsTotal);
    seq.add(affine.inverse());
    AffineGet pixelToPhysical = null;
    if (spacing.length == 2)
        pixelToPhysical = new Scale2D(spacing);
    else if (spacing.length == 3)
        pixelToPhysical = new Scale3D(spacing);
    FloatImagePlus<FloatType> dfieldRaw = convertToDeformationField(dims, seq, pixelToPhysical, nThreads);
    // this works for both 2d and 3d, it turn out
    RandomAccessibleInterval<FloatType> dfield = Views.permute(Views.permute(dfieldRaw, 0, 2), 1, 2);
    int[] blockSize = new int[spatialBlockSize.length + 1];
    blockSize[0] = spatialBlockSize.length;
    for (int i = 0; i < spatialBlockSize.length; i++) {
        blockSize[i + 1] = spatialBlockSize[i];
    }
    final N5Writer n5 = new N5Factory().openWriter(n5BasePath);
    N5DisplacementField.save(n5, n5Dataset, affine, dfield, spacing, blockSize, compression);
    N5DisplacementField.saveAffine(affine, n5, n5Dataset);
}
Also used : Scale2D(net.imglib2.realtransform.Scale2D) Scale3D(net.imglib2.realtransform.Scale3D) N5Writer(org.janelia.saalfeldlab.n5.N5Writer) AffineGet(net.imglib2.realtransform.AffineGet) ThinplateSplineTransform(net.imglib2.realtransform.ThinplateSplineTransform) RealTransformSequence(net.imglib2.realtransform.RealTransformSequence) RealPoint(net.imglib2.RealPoint) FloatType(net.imglib2.type.numeric.real.FloatType) ThinPlateR2LogRSplineKernelTransform(jitk.spline.ThinPlateR2LogRSplineKernelTransform) N5Factory(org.janelia.saalfeldlab.n5.ij.N5Factory)

Aggregations

AffineGet (net.imglib2.realtransform.AffineGet)2 N5Writer (org.janelia.saalfeldlab.n5.N5Writer)2 N5Factory (org.janelia.saalfeldlab.n5.ij.N5Factory)2 IOException (java.io.IOException)1 ThinPlateR2LogRSplineKernelTransform (jitk.spline.ThinPlateR2LogRSplineKernelTransform)1 RealPoint (net.imglib2.RealPoint)1 RealRandomAccessible (net.imglib2.RealRandomAccessible)1 AffineTransform3D (net.imglib2.realtransform.AffineTransform3D)1 RealTransformSequence (net.imglib2.realtransform.RealTransformSequence)1 Scale2D (net.imglib2.realtransform.Scale2D)1 Scale3D (net.imglib2.realtransform.Scale3D)1 ThinplateSplineTransform (net.imglib2.realtransform.ThinplateSplineTransform)1 FloatType (net.imglib2.type.numeric.real.FloatType)1 Compression (org.janelia.saalfeldlab.n5.Compression)1 GzipCompression (org.janelia.saalfeldlab.n5.GzipCompression)1 Lz4Compression (org.janelia.saalfeldlab.n5.Lz4Compression)1 RawCompression (org.janelia.saalfeldlab.n5.RawCompression)1 XzCompression (org.janelia.saalfeldlab.n5.XzCompression)1 BloscCompression (org.janelia.saalfeldlab.n5.blosc.BloscCompression)1 N5CosemMetadata (org.janelia.saalfeldlab.n5.metadata.N5CosemMetadata)1