Problems when using bfsave from Matlab

Hello,
I would like to save a dataset from Matlab to ome.tiff using bfsave function but it does not really work.

Problem 1
I have a 4D dataset (50 x 50 x 2 x 5, in YXCZT order), where I marked slice numbers and the corresponding color channels (“1R” text is burned into the 1st channel slice 1; “2R” - 2nd slice red channel; “1G” - 1st slice green…):
Slice 1:
image

Slice 2:
image

When I save and reopen dataset in Matlab or Fiji I am getting this result:
Slice 1:
image
Slice 2:
image

I was trying transpose the matrix or provide other dimension order but it does not really help.

Problem 2
Another issue is that a new dataset should be created

bfsave(I, outputPath) writes the input 5D matrix into a new file specified by outputPath.

but instead bfsave adds the dataset to an existing file, see the second part of the example below.

Dataset:
Stack_2C_5Z.mat (917 Bytes)

Matlab script:

% load a dataset (I) dimensions [50 x 50 x 2 x 5], as YXCZT
load('Stack_2C_5Z.mat');

% permute image from YXCZT to XYZCT
I2 = permute(I, [2 1 4 3 5]);

% create OME-XML metadata object from the image
metadata = createMinimalOMEXMLMetadata(I2, 'XYZCT');

% save it
bfsave(I2, 'Stack_2C_5Z.tiff', 'metadata', metadata);

% save it twice into a different file. This time the size of the output
% file is twice larger, so the dataset is added twice into the file
bfsave(I2, 'Stack_2C_5Z_b.tiff', 'metadata', metadata);
bfsave(I2, 'Stack_2C_5Z_b.tiff', 'metadata', metadata);

It was tested on 5.9.2 but I also tested bfsave from 6.0.1 with similar results.

Am I missing anything?

Hi @Ilya_Belevich, the bfsave command is used to write in the OME-TIFF format, so the metadata is being saved as part of the OME-XML. If you change the output file extension to .ome.tiff it should now be reade correctly. (The permute is also swapping the X and Y, Im not sure if that was intentional or not).

For the second point Im not quite sure I understand the issue, if you call the command twice with the same output file it will write to the same dataset, so I would expect the file size to be double. I might have misunderstood the issue with Problem 2.

Hi David,
thank you for reply!

If you change the output file extension to .ome.tiff it should now be reade correctly.

After renaming I can now open the file in Fiji using Bio-Formats Importer.
Using Bio-Formats Importer for multiple files look extremely tedious; is there any other way to open the image? I mean that drag-and-drop of an ome.tiff file to the Fiji toolbar produces a single channel stack of 10 images.

The permute is also swapping the X and Y, Im not sure if that was intentional or not

That was done, since Matlab stores matrices as [Y:X], but it looks that it is not needed.

if you call the command twice with the same output file it will write to the same dataset, so I would expect the file size to be double

I was expecting that calling the function again overwrites the file, which makes sense to me and also stated in the description of the function, but instead it just adds the dataset to the existing file. Also the first part of the dataset (which was created during the first call of the function) is not accessible.

Have you enabled SCIFIO in ImageJ2 with the Bioformats plugin?

1 Like

@markkitt, nice suggestion, thank you!
@dgault, do you have any ideas about doubling of files when saving them twice?

The doubling of the file size is the expected result, though there may be some confusion over the way the description of the function is worded. The Bio-Formats save or write functions will not delete or remove any existing data but instead append to the selected file.

@dgault, in this case append is not working properly, since the first part of the file becomes inaccessible after such appending.
IMO, logically, default behavior of bfsave should create a new file, however providing additional arguments should allow it to append/insert/replace or affect in any other way the destination file.

Appending in this case will mean that the metadata and dimensions wont match the with whats in the file which is why part of it will be inaccessible. The command when used as in this example, with the intention of creating a new file, should have a new file name passed to it:

bfsave(I2, 'myFirstFile.ome.tiff', 'metadata', metadata);
bfsave(I2, 'mySecondFile.ome.tiff', 'metadata', metadata);
1 Like