BioFormats¶
Bio-Formats wrapper utilities.
- io.BioFormats.bfopen3(r, seriesNumber, sliceNo, options)¶
BFOPEN3 - Open microscopy images in MATLAB using Bio-Formats.
- Syntax:
result = io.BioFormats.bfopen3(r, seriesNumber) result = io.BioFormats.bfopen3(r, seriesNumber, sliceNo, options)
Modified from the original bfopen.m by Ilya Belevich. Returns the selected dataset from a Bio-Formats reader.
- Input Arguments:
r - handle to a dataset opened with:
r = loci.formats.ChannelFiller(); r = loci.formats.ChannelSeparator(r); r = loci.formats.gui.BufferedImageReader(r); r.setId(handles.filename);seriesNumber - series number to load, starting from
1sliceNo - (optional) desired slice number from the series
options - (optional) struct with fields (not yet fully tested):
.x1- starting x position.y1- starting y position.dx- width.dy- height
- Output Arguments:
result - struct with the selected series:
.img- image array [height, width, color, depth].ColorType-'grayscale','truecolor', or'indexed'.ColorMap- colormap for indexed images
Note
This method is ~1.5×-2.5× slower than Bio-Formats’s command line showinf tool, due to overhead from copying arrays.
Internet Explorer sometimes erroneously renames the Bio-Formats library to
loci_tools.zip- if this happens, rename it back toloci_tools.jar.- Thanks to all who offered suggestions and improvements:
Ville Rantanen
Brett Shoelson
Martin Offterdinger
Tony Collins
Cris Luengo
Arnon Lieber
Jimmy Fong
- NB: Internet Explorer sometimes erroneously renames the Bio-Formats library
to loci_tools.zip. If this happens, rename it back to loci_tools.jar.
- Updates:
05.09.2013 Ilya Belevich, added sliceNo to load only a single slice
- io.BioFormats.bfopen5(r, seriesNumber, sliceNo, options)¶
BFOPEN5 - Open microscopy images in MATLAB using Bio-Formats with Memoizer support.
- Syntax:
result = io.BioFormats.bfopen5(r, seriesNumber) result = io.BioFormats.bfopen5(r, seriesNumber, sliceNo, options)
Modified from the original bfopen.m by Ilya Belevich. Returns the selected dataset from a Bio-Formats Memoizer reader.
- Input Arguments:
r - handle to a Memoizer opened as:
r = loci.formats.Memoizer(bfGetReader(), 0); r.setId(filename); r.close(); % optionallyWhen the Memoizer class is used,
rwon’t be closed at the end of the function. Alternatively, pass afilenamestring to use withsetId.seriesNumber - series number to load, starting from
1sliceNo - (optional) desired slice number from the series
options - (optional) struct with fields:
.bioFormatsMemoizerMemoDir- directory to store Memoizer memo files.dimensionOrder- (char) output order of dimensions:'XYZCT'- default'XYCZT''XYTZC''XYZTC'
.x1- starting x position.y1- starting y position.z1- starting z position.dx- width.dy- height.dz- depth.waitbarHandle- (optional) handle to an existing waitbar;[]if none.waitbarUpdateFrequency- (optional) frequency to update the waitbar
- Output Arguments:
result - struct with the selected series:
.img- image array[height, width, depth, color, time]; dims re-projected peroptions.dimensionOrder.ColorType-'grayscale','multichannel', or'indexed'.ColorMap- colormap for indexed images
Note
Portions of this code were adapted from: http://www.mathworks.com/support/solutions/en/data/1-2WPAYR/
This method is ~1.5×-2.5× slower than Bio-Formats’s command line showinf tool, due to overhead from copying arrays.
Internet Explorer sometimes erroneously renames the Bio-Formats library to
loci_tools.zip- if this happens, rename it back toloci_tools.jar.- Thanks to all who offered suggestions and improvements:
Ville Rantanen
Brett Shoelson
Martin Offterdinger
Tony Collins
Cris Luengo
Arnon Lieber
Jimmy Fong
- NB: Internet Explorer sometimes erroneously renames the Bio-Formats library
to loci_tools.zip. If this happens, rename it back to loci_tools.jar.
- Updates:
30.01.2019 Ilya Belevich, adaptation for use with Memoizer
13.12.2023 Ilya Belevich, added cancel upon waitbar cancel click
07.01.2026 Ilya Belevich, switched to [Y X Z C T] outputs, renamed .ColorType truecolor->multichannel
- class io.BioFormats.Config¶
Bases:
handleCONFIG - process-wide selection of the active BioFormats / WSI reader backend.
Mirrors
io.zarr.Configfor the image-reading side. Holds one module-level setting shared by theio.BioFormatsfacade (io.BioFormats.Reader):library - which engine reads microscopy / whole-slide image files:
'mib'(default) - MIB’s bundled OME Bio-Formats Java reader (bfGetReader/bfGetPlane/loci.formats.Memoizer). Broadest coverage validated inside MIB; the historical path.'matlab'- MATLAB’s built-in WSI file readers (bioformatsinfo/bioformatsreadand, for classic WSI formats,openslideinfo/openslideread), which return lazy, tiled, pyramid-awareblockedImageobjects. No Java dependency.
Benchmark (2026-06-17, 50-series CZI): the two engines are a tie for a full read (~2.5 s, pixel-identical), so the choice is about coverage / robustness / dependencies, not speed.
models.MibModel.initializePreferencespushes this frompreferences.IO.BioFormats.Libraryat start-up; the Preferences dialog callssetLibraryagain whenever the user changes it (takes effect immediately).Examples
io.BioFormats.Config.setLibrary('matlab'); % use bioformatsread/openslideread tf = io.BioFormats.Config.isMatlab(); % true lib = io.BioFormats.Config.library(); % 'matlab' io.BioFormats.Config.setLibrary('mib'); % back to the Java readerSee also: io.zarr.Config, io.BioFormats.Reader
- Method Summary
- static isMatlab()¶
ISMATLAB - true when the MATLAB built-in WSI backend is active.
- static library(name)¶
LIBRARY - get (no args) or set (with a name) the active backend. Returns the canonical name
'mib'or'matlab'.
- static memoDir(dirPath)¶
MEMODIR - get (no args) or set (with a path) the Bio-Formats Memoizer directory used by the
'mib'backend (where.bfmemoreader caches are written). Defaultfullfile(tempdir, 'mibVirtual');models.MibModel.initializePreferencesoverrides it frompreferences.ExternalDirs.BioFormatsMemoizerMemoDirat start-up.
- static normalizeName(name)¶
NORMALIZENAME - map a string to a canonical backend name WITHOUT changing the process-wide setting (unlike
library(name)).
- static setLibrary(name)¶
SETLIBRARY - set the active backend (‘mib’|’matlab’, aliases accepted).
- static setMemoDir(dirPath)¶
SETMEMODIR - set the Bio-Formats Memoizer directory (mib backend).
- io.BioFormats.mibImage2ometiff(filename, imageS, options)¶
MIBIMAGE2OMETIFF - Save image in OME.TIF format - either as a single 5D file or a 2D sequence.
- Syntax:
[result, options] = io.BioFormats.mibImage2ometiff(filename, imageS) [result, options] = io.BioFormats.mibImage2ometiff(filename, imageS, options)- Input Arguments:
filename - full path for the output file (extension forced to
.ome.tiff)imageS - dataset [height, width, color_channels, z_slices, time]
options - (optional) struct with fields:
.pixSize- MIB pixel-size struct with fields.x,.y,.z,.t,.units,.tunits; default: all1, units'um', tunits's'.lutColors- [C×3] LUT colour matrix (unused in 2D imwrite path).ImageDescription- (char or cell-string) description embedded in the file (default:'').DatasetType-'image'(default) or'model'.Saving3d-'5D'(default): write all slices into one OME-TIFF viabfsave;'2D': write each z-slice as a separate.tiffile.overwrite-1= skip the “file exists” prompt (default:0).Compression-'none'(default),'lzw', or'packbits'(2D path only).showWaitbar-1= show progress bar (default);0= suppress.ParentFigure- handle to the MIB UIFigure; when provided the progress bar is shown as auiprogressdlgattached to that window; when absent the legacywaitbaris used.silent- [logical] (default:false); whentrueall interactive dialogs are suppressed.sequentialFn- controls 2D output naming:true(default whenNaN) - sequential names, e.g.image_01.ome.tifffalse- use original per-slice names from.SliceName; falls back to sequential when.SliceNameis absent or emptyNaN- decide at runtime (currently defaults totrue); normally set by the calling saver (OmeTiffSaver) based on the user’s dialog choice
.SliceName- cell array of per-slice source filenames (without path); used by thefalsebranch of.sequentialFn.cmap- colormap matrix for indexed images;NaN(default) means grayscale/RGB.Resolution- [xDPI yDPI] written into 2D.tiffiles; derived automatically frompixSizewhen absent.DimensionOrder- dimension order string passed tobfsave/createMinimalOMEXMLMetadata; default:'XYZCT'
- Output Arguments:
result -
1= success,0= failureoptions - the options struct as used (with all defaults filled in)
- class io.BioFormats.Reader¶
Bases:
handleREADER - backend-agnostic, on-demand region reader for microscopy / WSI files.
Facade over the BioFormats / WSI engine selected by
io.BioFormats.Config('mib'= bundled OME Bio-Formats Java reader,'matlab'= MATLAB built-inbioformatsread/openslideread). It exposes a small, reader-agnostic contract designed to matchio.loaders.Zarr3VirtualLoaderso that - in a later phase - the BigData image read seam (MibVirtualImage.getDataZarr→loaders{1}.readRegion(...)) can be backed by a WSI file directly, giving the same on-demand, level-aware read experience as a zarr3 dataset.- Contract
s = info()- dimensions, class, and resolution-level metadata.block = readRegion(level, Ylim, Xlim, Zlim, Clim, Tlim, dataClass)- read a sub-region at a 1-based resolutionlevel; returns MIB3-order[y, x, z, c, t].close()- release the underlying handle.
Phase A (2026-06-17): only the
'mib'(Java) backend is implemented, and only full resolution (level == 1); nothing in the app calls this class yet (no behaviour change). True pyramid-level reads (setResolution) and the'matlab'backend land in later phases (seedevelopment/bigdata/bigdata_implementation_plan.md).Examples
r = io.BioFormats.Reader('C:\data\slide.czi', 0); % series 0 s = r.info(); % dims + levels blk = r.readRegion(1, [1 512], [1 512], [1 1], [1 s.colors], [1 1], s.imgClass); r.close();See also: io.BioFormats.Config, io.loaders.Zarr3VirtualLoader, io.loaders.BioFormatsVirtualLoader
- Constructor Summary
- Reader(filename, seriesIndex, options)¶
READER - construct an on-demand reader; backend from io.BioFormats.Config.
- Input Arguments:
filename - [char] full path to the file
seriesIndex - (optional) [double] 0-based series index (default 0)
options - (optional) struct:
.library- [char] override the backend:'mib'(Java),'matlab'(bioformatsread), or'openslide'(openslideread). When omitted, the BioFormats engine comes from io.BioFormats.Config ('mib'``|’matlab’); ``'openslide'must be requested explicitly..memoDir- [char] Bio-Formats Memoizer directory (mib backend)
- Property Summary
- filename¶
- library¶
[double] 0-based series index.
- memoDir¶
[char] resolved backend (‘mib’ | ‘matlab’) at construction.
- seriesIndex¶
[char] full path to the microscopy / WSI file.
- Method Summary
- close()¶
CLOSE - release the underlying handle. Safe to call repeatedly.
- delete()¶
DELETE - destructor; closes the reader.
- info()¶
INFO - return dimensions + resolution-level metadata.
- Output Arguments:
s - struct with fields:
.height.width.depth.colors.time.imgClass- [char] e.g.'uint8'/'uint16'.numLevels- number of resolution (pyramid) levels.levelSizes- [numLevels x 2] per-level[height width]
- pyramidStruct(voxelSize)¶
PYRAMIDSTRUCT - build the MIB
pyramidmetadata struct for this image.Returns the fields
core.MibVirtualImage/MibBigDataImageneed so the existing pyramidal read path (getDataZarr/ futuregetDataPyramid) can drive this WSI reader exactly like a zarr3 dataset (level selection by magnification + level-local region reads).- Input Arguments:
voxelSize - (optional) [1x3] full-resolution
[y x z]voxel size (µm); default[1 1 1].
- Output Arguments:
pyramid - struct with fields:
.sourceType-'bioformats'(used by the read dispatch).levelNames- {1 x nLevels}'0'..'N-1'(resolution keys).levelImageSizes- [nLevels x 3] per-level[Y X Z].levelScaleFactors- [nLevels x 3] level0 ./ levelN[y x z].levelVoxelSizes- [1 x 3] full-resolution[y x z]voxel size.axisOrder-''(the reader returns native MIB3 [y x z c t]).chunkSizes/.shardSizes-[]
- readRegion(level, Ylim, Xlim, Zlim, Clim, Tlim, dataClass)¶
READREGION - read a sub-region; returns MIB3-order [y, x, z, c, t].
- Input Arguments:
level - [double] 1-based resolution level (1 = full resolution)
Ylim, Xlim, Zlim - [1x2] inclusive 1-based LEVEL-LOCAL ranges
Clim - [1x2] inclusive 1-based colour-channel range
Tlim - [1x2] inclusive 1-based time range
dataClass - [char] output class
- io.BioFormats.wavelength2rgb(wavelength)¶
WAVELENGTH2RGB - Convert wavelength into RGB value (0-255).
- Syntax:
rgb = io.BioFormats.wavelength2rgb(wavelength)
The code is adapted from http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm
- Input Arguments:
wavelength - a number containing wavelength
- Output Arguments:
rgb - an array containing, (red, green, blue) components of the color, range 0-255