use of org.janelia.saalfeldlab.n5.Compression 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);
}
}
}
use of org.janelia.saalfeldlab.n5.Compression in project bigwarp by saalfeldlab.
the class ApplyBigwarpPlugin method run.
@Override
public void run(String arg) {
if (IJ.versionLessThan("1.40"))
return;
// Find any open images
final int[] ids = WindowManager.getIDList();
final int N = ids == null ? 0 : ids.length;
final String[] titles = new String[N + 1];
for (int i = 0; i < N; ++i) {
titles[i] = (WindowManager.getImage(ids[i])).getTitle();
}
titles[N] = "<None>";
final GenericDialogPlus gd = new GenericDialogPlus("Apply Big Warp transform");
gd.addMessage("File Selection:");
gd.addFileField("landmarks_image_file", "");
ImagePlus currimg = WindowManager.getCurrentImage();
String current = titles[N];
if (currimg != null) {
current = currimg.getTitle();
}
gd.addChoice("moving_image", titles, current);
if (titles.length > 1)
gd.addChoice("target_image", titles, current.equals(titles[0]) ? titles[1] : titles[0]);
else
gd.addChoice("target_image", titles, titles[0]);
gd.addMessage("\nN5/Zarr/HDF5/BDV-XML");
gd.addDirectoryOrFileField("Moving", "");
gd.addStringField("Moving_dataset", "");
gd.addDirectoryOrFileField("Target", "");
gd.addStringField("Target_dataset", "");
gd.addChoice("Transform type", new String[] { TransformTypeSelectDialog.TPS, TransformTypeSelectDialog.AFFINE, TransformTypeSelectDialog.SIMILARITY, TransformTypeSelectDialog.ROTATION, TransformTypeSelectDialog.TRANSLATION }, TransformTypeSelectDialog.TPS);
gd.addMessage("Field of view and resolution:");
gd.addChoice("Resolution", new String[] { TARGET, MOVING, SPECIFIED }, TARGET);
gd.addChoice("Field of view", new String[] { TARGET, MOVING_WARPED, LANDMARK_POINTS, SPECIFIED_PIXEL, SPECIFIED_PHYSICAL }, 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("Output options");
gd.addChoice("Interpolation", new String[] { "Nearest Neighbor", "Linear" }, "Linear");
gd.addCheckbox("virtual?", false);
gd.addNumericField("threads", 4, 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 landmarksPath = gd.getNextString();
ImagePlus movingIp = null;
ImagePlus targetIp = null;
final int mvgImgIdx = gd.getNextChoiceIndex();
final int tgtImgIdx = gd.getNextChoiceIndex();
movingIp = mvgImgIdx < N ? WindowManager.getImage(ids[mvgImgIdx]) : null;
targetIp = tgtImgIdx < N ? WindowManager.getImage(ids[tgtImgIdx]) : null;
final String mvgRoot = gd.getNextString();
final String mvgDataset = gd.getNextString();
final String tgtRoot = gd.getNextString();
final String tgtDataset = gd.getNextString();
final String transformTypeOption = gd.getNextChoice();
final String resOption = gd.getNextChoice();
final String fovOption = gd.getNextChoice();
final String fovPointFilter = gd.getNextString();
final double[] resolutions = new double[3];
resolutions[0] = gd.getNextNumber();
resolutions[1] = gd.getNextNumber();
resolutions[2] = gd.getNextNumber();
final double[] offset = new double[3];
offset[0] = gd.getNextNumber();
offset[1] = gd.getNextNumber();
offset[2] = gd.getNextNumber();
final double[] fov = new double[3];
fov[0] = gd.getNextNumber();
fov[1] = gd.getNextNumber();
fov[2] = gd.getNextNumber();
final String interpType = gd.getNextChoice();
boolean isVirtual = gd.getNextBoolean();
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();
// load the image data
BigWarpData<?> bigwarpdata = BigWarpInit.initData();
int id = 0;
if (movingIp != null) {
id += BigWarpInit.add(bigwarpdata, movingIp, id, 0, true);
} else if (mvgDataset != null && !mvgDataset.isEmpty()) {
BigWarpInit.addToData(bigwarpdata, true, id, mvgRoot, mvgDataset);
id++;
} else if (mvgRoot != null && !mvgRoot.isEmpty()) {
try {
ImagePlus movingFromFile = IJ.openImage(mvgRoot);
id += BigWarpInit.add(bigwarpdata, movingFromFile, id, 0, true);
} catch (Exception e) {
IJ.showMessage("could not read from file: " + mvgRoot);
return;
}
}
if (targetIp != null) {
id += BigWarpInit.add(bigwarpdata, targetIp, id, 0, false);
} else if (tgtDataset != null && !tgtDataset.isEmpty()) {
BigWarpInit.addToData(bigwarpdata, false, id, tgtRoot, tgtDataset);
id++;
} else if (tgtRoot != null && !tgtRoot.isEmpty()) {
try {
final ImagePlus targetFromFile = IJ.openImage(tgtRoot);
id += BigWarpInit.add(bigwarpdata, targetFromFile, id, 0, false);
} catch (Exception e) {
// we're allowed not to have a target image
}
}
bigwarpdata.wrapUp();
final int nd = BigWarp.detectNumDims(bigwarpdata.sources);
final int[] blockSize = ApplyBigwarpPlugin.parseBlockSize(blockSizeString, nd);
final Compression compression = ApplyBigwarpPlugin.getCompression(compressionString);
final WriteDestinationOptions writeOpts = new ApplyBigwarpPlugin.WriteDestinationOptions(fileOrN5Root, n5Dataset, blockSize, compression);
LandmarkTableModel ltm = new LandmarkTableModel(nd);
try {
ltm.load(new File(landmarksPath));
} catch (IOException e) {
e.printStackTrace();
return;
}
final Interpolation interp;
if (interpType.equals("Nearest Neighbor"))
interp = Interpolation.NEARESTNEIGHBOR;
else
interp = Interpolation.NLINEAR;
List<ImagePlus> warpedIpList = apply(bigwarpdata, ltm, transformTypeOption, fovOption, fovPointFilter, resOption, resolutions, fov, offset, interp, isVirtual, nThreads, false, writeOpts);
}
use of org.janelia.saalfeldlab.n5.Compression 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);
}
use of org.janelia.saalfeldlab.n5.Compression 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);
}
Aggregations