wiki:Matlab

Version 5 (modified by melissa, 9 years ago) (diff)

--

Reading files

This section assumes that you have installed the bfopen.m script and loci_tools.jar, as instructed  here.

The first thing to do is initialize a file:

  data = bfopen('/path/to/data/file');

'data' is an array whose structure is a bit complicated. It is an n-by-3 array, where n is the number of series in the dataset. The [s, 1] element (if s is the series index between 1 and n) is an m-by-2 array, where m is the number of planes in the series. The [s, 1, t, 1] element (where t is the image index between 1 and m) contains the pixel data for the t-th image in the s-th series. The [s, 1, t, 2] element contains the label for said image.

The [s, 2] element of 'data' contains metadata key/value pairs that apply to the s-th series. The [s, 3] element of 'data' contains color lookup tables for each image in the series.

Displaying images

If you want to display one of the images, you can do so as follows:

  series1 = data{1, 1};
  series1_plane1 = series1{1, 1};
  series1_label1 = series1{1, 2};
  series1_colorMaps = data{1, 3};
  figure('Name', series1_label1);
  if (isempty(series1_colorMaps{1}))
    colorMap(gray);
  else
    colorMap(series1_colorMaps{1});
  end
  imagesc(series1_plane1);

This will display the first image of the first series with its associated color map (if present). If you would prefer not to apply the color maps associated with each image, simply comment out the calls to 'colorMap'.

Retrieving metadata

To retrieve the metadata value for a specific key:

  metadata = data{1, 2};
  bitsPerPixel = metadata.get('Bits per pixel');

To print out all of the metadata key/value pairs for the first series:

  metadata = data{1, 2};
  for i=1:size(metadata)
    fprintf('%s = %s\n', metadata[1], metadata[2])
  end

Saving files

First, make sure that you have loci_tools.jar installed in your MATLAB work folder.

Now, here is the basic code for saving planes to a file:

  javaaddpath(fullfile(fileparts(mfilename('fullpath')), 'loci_tools.jar'));
  writer = loci.formats.ImageWriter();
  metadata = loci.formats.MetadataTools.createOMEXMLMetadata();
  metadata.createRoot();
  metadata.setImageID('Image:0', 0);
  metadata.setPixelsID('Pixels:0', 0);
  metadata.setPixelsBinDataBigEndian(java.lang.Boolean.TRUE, 0, 0);
  metadata.setPixelsDimensionOrder(ome.xml.model.enums.DimensionOrder.XYZCT, 0);
  metadata.setPixelsType(ome.xml.model.enums.PixelType.UINT8, 0);
  metadata.setPixelsSizeX(ome.xml.model.primitives.PositiveInteger(java.lang.Integer(64)), 0);
  metadata.setPixelsSizeY(ome.xml.model.primitives.PositiveInteger(java.lang.Integer(64)), 0);
  metadata.setPixelsSizeZ(ome.xml.model.primitives.PositiveInteger(java.lang.Integer(1)), 0);
  metadata.setPixelsSizeC(ome.xml.model.primitives.PositiveInteger(java.lang.Integer(1)), 0);
  metadata.setPixelsSizeT(ome.xml.model.primitives.PositiveInteger(java.lang.Integer(1)), 0);
  metadata.setChannelID('Channel:0:0', 0, 0);
  metadata.setChannelSamplesPerPixel(ome.xml.model.primitives.PositiveInteger(java.lang.Integer(1)), 0, 0);
  
  writer.setMetadataRetrieve(metadata);
  writer.setId("my-file.ome.tiff");
  writer.saveBytes(0, plane);
  writer.close();

This example will write a single plane to an OME-TIFF file. It assumes that there are 8 unsigned bits per pixel, and that the image is 64 pixels x 64 pixels. In your own code, you will need to adjust the dimensions and pixel type accordingly. Also, 'plane' is an array constructed like so:

  plane = zeros(1, 64 * 64, 'uint8');

In the near future, we will have a script for saving files similar to bfopen.m; see #396.