use of org.janelia.saalfeldlab.n5.N5Reader in project bigwarp by saalfeldlab.
the class BigWarpInit method loadN5Source.
public static Source<?> loadN5Source(final String n5Root, final String n5Dataset) {
final N5Reader n5;
try {
n5 = new N5Factory().openReader(n5Root);
} catch (IOException e) {
e.printStackTrace();
return null;
}
final N5MetadataParser<?>[] PARSERS = new N5MetadataParser[] { new ImagePlusLegacyMetadataParser(), new N5CosemMetadataParser(), new N5SingleScaleMetadataParser(), new CanonicalMetadataParser(), new N5GenericSingleScaleMetadataParser() };
final N5MetadataParser<?>[] GROUP_PARSERS = new N5MetadataParser[] { new N5CosemMultiScaleMetadata.CosemMultiScaleParser(), new N5ViewerMultiscaleMetadataParser(), new CanonicalMetadataParser() };
N5Metadata meta = null;
try {
final N5DatasetDiscoverer discoverer = new N5DatasetDiscoverer(n5, N5DatasetDiscoverer.fromParsers(PARSERS), N5DatasetDiscoverer.fromParsers(GROUP_PARSERS));
final N5TreeNode node = discoverer.discoverAndParseRecursive(n5Dataset);
meta = node.getMetadata();
} catch (IOException e) {
}
if (meta instanceof MultiscaleMetadata) {
return openAsSourceMulti(n5, (MultiscaleMetadata<?>) meta, true);
} else {
return openAsSource(n5, meta, true);
}
}
use of org.janelia.saalfeldlab.n5.N5Reader in project bigwarp by saalfeldlab.
the class BigWarpInit method openAsSource.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T extends N5Metadata> Source<?> openAsSource(final N5Reader n5, final T meta, final boolean isVolatile) {
final RandomAccessibleInterval imageRaw;
final RandomAccessibleInterval image;
try {
if (isVolatile)
imageRaw = to3d(N5Utils.openVolatile(n5, meta.getPath()));
else
imageRaw = to3d(N5Utils.open(n5, meta.getPath()));
if (meta instanceof N5ImagePlusMetadata && ((N5ImagePlusMetadata) meta).getType() == ImagePlus.COLOR_RGB && Util.getTypeFromInterval(imageRaw) instanceof UnsignedIntType) {
image = toColor(imageRaw);
} else
image = imageRaw;
if (meta instanceof SpatialMetadata) {
final String unit = ((SpatialMetadata) meta).unit();
final AffineTransform3D srcXfm = ((SpatialMetadata) meta).spatialTransform3d();
final FinalVoxelDimensions voxelDims = new FinalVoxelDimensions(unit, new double[] { srcXfm.get(0, 0), srcXfm.get(1, 1), srcXfm.get(2, 2) });
return new BwRandomAccessibleIntervalSource(image, Util.getTypeFromInterval(image), srcXfm, meta.getPath(), voxelDims);
} else
return new BwRandomAccessibleIntervalSource(image, Util.getTypeFromInterval(image), new AffineTransform3D(), meta.getPath());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
use of org.janelia.saalfeldlab.n5.N5Reader in project mars-fx by duderstadt-lab.
the class MarsBdvFrame method loadN5Source.
private Source<T> loadN5Source(MarsBdvSource source, MarsMetadata meta) throws IOException {
N5Reader reader;
if (n5Readers.containsKey(source.getPath())) {
reader = n5Readers.get(source.getPath());
} else {
reader = new N5Importer.N5ViewerReaderFun().apply(source.getPath());
n5Readers.put(source.getPath(), reader);
}
@SuppressWarnings("rawtypes") final RandomAccessibleInterval wholeImage = N5Utils.open(reader, source.getN5Dataset());
// wholeImage should be XYT or XYCT. If XYCT, we hyperSlice to get one
// channel.
// XYZCT should also be supported
int dims = wholeImage.numDimensions();
@SuppressWarnings("rawtypes") final RandomAccessibleInterval image = (dims > 3) ? Views.hyperSlice(wholeImage, wholeImage.numDimensions() - 2, source.getChannel()) : wholeImage;
int tSize = (int) image.dimension(image.numDimensions() - 1);
if (tSize > numTimePoints)
numTimePoints = tSize;
@SuppressWarnings("rawtypes") final RandomAccessibleInterval[] images = new RandomAccessibleInterval[1];
images[0] = image;
if (source.getSingleTimePointMode()) {
AffineTransform3D[] transforms = new AffineTransform3D[tSize];
// Drift should be corrected against them
for (int t = 0; t < tSize; t++) transforms[t] = source.getAffineTransform3D();
int singleTimePoint = source.getSingleTimePoint();
@SuppressWarnings("unchecked") final MarsSingleTimePointN5Source<T> n5Source = new MarsSingleTimePointN5Source<>((T) Util.getTypeFromInterval(image), source.getName(), images, transforms, singleTimePoint);
return n5Source;
} else {
AffineTransform3D[] transforms = new AffineTransform3D[tSize];
for (int t = 0; t < tSize; t++) {
if (source.getCorrectDrift()) {
double dX = meta.getPlane(0, 0, 0, t).getXDrift();
double dY = meta.getPlane(0, 0, 0, t).getYDrift();
transforms[t] = source.getAffineTransform3D(dX, dY);
} else
transforms[t] = source.getAffineTransform3D();
}
@SuppressWarnings("unchecked") final MarsN5Source<T> n5Source = new MarsN5Source<>((T) Util.getTypeFromInterval(image), source.getName(), images, transforms);
return n5Source;
}
}
use of org.janelia.saalfeldlab.n5.N5Reader in project mars-fx by duderstadt-lab.
the class MarsBdvFrame method loadN5VolatileSource.
private Source<T> loadN5VolatileSource(MarsBdvSource source, MarsMetadata meta) throws IOException {
N5Reader reader;
if (n5Readers.containsKey(source.getPath())) {
reader = n5Readers.get(source.getPath());
} else {
reader = new N5Importer.N5ViewerReaderFun().apply(source.getPath());
n5Readers.put(source.getPath(), reader);
}
@SuppressWarnings("rawtypes") final RandomAccessibleInterval wholeImage = N5Utils.openVolatile(reader, source.getN5Dataset());
// wholeImage should be XYT or XYCT. If XYCT, we hyperSlice to get one
// channel.
// XYZCT should also be supported
int dims = wholeImage.numDimensions();
@SuppressWarnings("rawtypes") final RandomAccessibleInterval image = (dims > 3) ? Views.hyperSlice(wholeImage, wholeImage.numDimensions() - 2, source.getChannel()) : wholeImage;
int tSize = (int) image.dimension(image.numDimensions() - 1);
if (tSize > numTimePoints)
numTimePoints = tSize;
@SuppressWarnings("rawtypes") final RandomAccessibleInterval[] images = new RandomAccessibleInterval[1];
images[0] = image;
if (source.getSingleTimePointMode()) {
AffineTransform3D[] transforms = new AffineTransform3D[tSize];
// Drift should be corrected against them
for (int t = 0; t < tSize; t++) transforms[t] = source.getAffineTransform3D();
int singleTimePoint = source.getSingleTimePoint();
@SuppressWarnings("unchecked") final MarsSingleTimePointN5Source<T> n5Source = new MarsSingleTimePointN5Source<>((T) Util.getTypeFromInterval(image), source.getName(), images, transforms, singleTimePoint);
return (Source<T>) n5Source.asVolatile(sharedQueue);
} else {
AffineTransform3D[] transforms = new AffineTransform3D[tSize];
for (int t = 0; t < tSize; t++) {
if (source.getCorrectDrift()) {
double dX = meta.getPlane(0, 0, 0, t).getXDrift();
double dY = meta.getPlane(0, 0, 0, t).getYDrift();
transforms[t] = source.getAffineTransform3D(dX, dY);
} else
transforms[t] = source.getAffineTransform3D();
}
@SuppressWarnings("unchecked") final MarsN5Source<T> n5Source = new MarsN5Source<>((T) Util.getTypeFromInterval(image), source.getName(), images, transforms);
return (Source<T>) n5Source.asVolatile(sharedQueue);
}
}
Aggregations