Skip to content

Instantly share code, notes, and snippets.

@sbesson
Last active December 22, 2015 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbesson/6529406 to your computer and use it in GitHub Desktop.
Save sbesson/6529406 to your computer and use it in GitHub Desktop.
Reading a multi-image file via Bio-Formatsr
% Create a reader from a nd2 file
filepath = fullfile(getenv('HOME'), 'bernhard', '962.nd2');
fprintf(1, 'Loading %s\n', filepath);
% Load the reader. In the case of very large files, the call to setId()
% in the bfGetReader() should be the limiting step
r = bfGetReader(filepath);
% Retrieve the metadata store for metadata reading
metadata = r.getMetadataStore();
% Read number of series
nSeries = r.getSeriesCount();
fprintf(1, 'Number of series: %g\n\n', nSeries);
%% Read metadata
% More information on the supported metadata fields can be found on the corresponding format page
% e.g. for nd2 http://www.openmicroscopy.org/site/support/bio-formats4/formats/nikon-nis-elements-nd2-metadata.html
for s = 1 : nSeries
% Switching to sth series
r.setSeries(s - 1);
% Listing all images in the series
fprintf(1, '\n\tImage metadata\n');
fprintf(1, '\n%s\n', char(metadata.getImageName(s-1)));
fprintf(1, '\tNumber of images: %g\n', r.getImageCount());
fprintf(1, '\tAcquisition date: %s\n', char(metadata.getImageAcquisitionDate(s-1)));
% Read pixels metadat
fprintf(1, '\n\tPixels metadata\n');
fprintf(1, '\tSize X: %g\n', metadata.getPixelsSizeX(s-1).getValue());
fprintf(1, '\tSize Y: %g\n', metadata.getPixelsSizeY(s-1).getValue());
fprintf(1, '\tSize Z: %g\n', metadata.getPixelsSizeZ(s-1).getValue());
fprintf(1, '\tSize C: %g\n', metadata.getPixelsSizeC(s-1).getValue());
fprintf(1, '\tSize T: %g\n', metadata.getPixelsSizeT(s-1).getValue());
fprintf(1, '\tPixels size X: %g microns\n', metadata.getPixelsPhysicalSizeX(s-1).getValue());
fprintf(1, '\tPixels size Y: %g microns\n', metadata.getPixelsPhysicalSizeY(s-1).getValue());
fprintf(1, '\tPixels size Z: %g microns\n', metadata.getPixelsPhysicalSizeZ(s-1).getValue());
% Read channel metadata
fprintf(1, '\n\tChannel metadata\n');
nChannels = metadata.getPixelsSizeC(s-1).getValue();
for c = 1 :nChannels
fprintf(1, '\t%s\n', char(metadata.getChannelID(s-1, c-1)));
fprintf(1, '\t%s\n', char(metadata.getChannelName(s-1, c-1)));
end
% Read metadata for first plane of the series
fprintf(1, '\n\t\tMetadata for first plane\n');
fprintf(1, '\t\tZ: %g\n', metadata.getPlanePositionX(s-1, 0).intValue);
fprintf(1, '\t\tC: %g\n', metadata.getPlanePositionX(s-1, 0).intValue);
fprintf(1, '\t\tT: %g\n', metadata.getPlanePositionX(s-1, 0).intValue);
fprintf(1, '\t\tPosition X: %g\n', metadata.getPlanePositionX(s-1, 0).intValue);
fprintf(1, '\t\tPosition X: %g\n', metadata.getPlanePositionY(s-1, 0).intValue);
fprintf(1, '\t\tPosition Y: %g\n', metadata.getPlanePositionZ(s-1, 0).intValue);
fprintf(1, '\t\tExposure time: %g\n', double(metadata.getPlaneExposureTime(s-1, 0)));
fprintf(1, '\t\tDeltaT: %g\n', metadata.getPlaneDeltaT(s-1, 0).doubleValue);
end
%% Read pixels data
% First set the desired series
s = 2; % Second series
r.setSeries(s - 1);
% Load plane using plane index
iPlane = 1; % First plane in the series
I = bfGetPlane(r, iPlane);
% Load plane using (z, c, t) coordinates
z = 2;
c = 1;
t = 10;
% Convert to linear index
% NB: this is a Java method taking 0-based index as input and returning 0-based index
iPlane = loci.formats.FormatTools.getIndex(r, z-1, c-1, z-1) + 1;
I = bfGetPlane(r, iPlane + 1);
% Read tile originated at (x, y) and of dimensions (w, h)
iPlane = 1; % First plane in the series
x = 100;
y = 100;
width = 200;
height = 200;
I = bfGetPlane(r, iPlane, x, y, w, h);
% Close the reader
r.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment