We're Hiring!

Saving 5D image as Tif and OME-Tif

Historical discussions about the Bio-Formats library. Please look for and ask new questions at https://forum.image.sc/tags/bio-formats
Please note:
Historical discussions about the Bio-Formats library. Please look for and ask new questions at https://forum.image.sc/tags/bio-formats

If you are having trouble with image files, there is information about reporting bugs in the Bio-Formats documentation. Please send us the data and let us know what version of Bio-Formats you are using. For issues with your code, please provide a link to a public repository, ideally GitHub.

Saving 5D image as Tif and OME-Tif

Postby bimoelle » Thu Mar 12, 2015 3:13 pm

Hi,
we would like to save 5D-images in TIF- and OME-TIF format, respectively, using Bioformats, however, did not succeed for now. It seems that either the meta data is not written correctly to the image files or the image data itself is probably not formatted in the right way. When opening saved images, e.g., with ImageJ directly or by using the Bioformats importer, the dimensions are mixed up. For example, assume that we save an image with dimensions x-size = 200, y-size = 100, c-size = 3, z-size = 10 and t-size = 5, when loading the image again the dimensions are x-size = 200, y-size = 100, c-size = 1, z-size = 1, and t-size = 150. We tried to save the image using IFormatWriter and MetadataStore objects, as pointed out at http://www.openmicroscopy.org/site/supp ... xport.html, however, seem to miss something fundamental. Is there any sample code that shows how 5D data can be saved to file in Tif and OME-Tif format using Bioformats and particularly, how the image data has to be formed and how meta data can be specified correctly?
Thanks and best regards,

Birgit
bimoelle
 
Posts: 4
Joined: Thu Mar 12, 2015 12:47 pm

Re: Saving 5D image as Tif and OME-Tif

Postby rleigh » Fri Mar 13, 2015 5:59 pm

Dear Birgit,

Are you reading an image with Bio-Formats prior to writing it back out as an OME-TIFF or are you creating the image from scratch e.g. after/during original acquisition?

For the former, I would suggest looking at the "bfconvert" command which handles reading and writing. The source is components/bio-formats-tools/src/loci/formats/tools/ImageConverter.java In this situation, the MetadataStore is initialised with the correct metadata (including image dimensions) during reading, and so should be then usable when writing.

For the latter, the MetadataStore (OMEXMLMetadataStore) will need filling from scratch with the correct image dimensions. This can be done entirely with the methods from the MetadataStore interface, but it's simpler to create a CoreMetadata object and then call MetadataTools.populateMetadata which will do all this for you. After that you can set any other metadata you desire. The dimensionality is set in the CoreMetadata object (size[XYZTC]).

Any of the readers in /components/formats-bsd/src/loci/formats/in should be useful as examples of doing this, though they generally use populatePixels rather than populateMetadata. They do all set up the data in CoreMetadata though.

Once you have the data set in the MetadataStore, it should then work exactly as in the documentation link you provided.

If you would like to share the code you're working on, we can probably provide some additional suggestions after taking a look at it.


Kind regards,
Roger
User avatar
rleigh
 
Posts: 217
Joined: Tue Mar 13, 2012 11:45 am

Re: Saving 5D image as Tif and OME-Tif

Postby bimoelle » Mon Mar 16, 2015 10:08 am

Hi Roger,
we are trying to create and save images from scratch. We already used an OMEXMLMetadataStore object and the MetadataTools.populateMetadata() method. Here is how we tried to save the image data:

Code: Select all
   public static void main(String[] args) throws Exception {

      // dimensions of desired demo image
      int x = 30, y = 20, c = 6, z = 10, t = 5;
      int pixelType = FormatTools.UINT8;
      
      // maybe this is the problem: how to organize the image data?
      byte[] img =
            new byte[x * y * FormatTools.getBytesPerPixel(pixelType)];
      for (int i=0; i<img.length; i++)
         img[i] = (byte) (256 * Math.random());
      
      // create metadata object with minimum required metadata fields
      ServiceFactory factory = new ServiceFactory();
      OMEXMLService service = factory.getInstance(OMEXMLService.class);
      IMetadata meta = service.createOMEXMLMetadata();
      MetadataTools.populateMetadata(meta, 0, null, false, "XYZCT",
            FormatTools.getPixelTypeString(pixelType), x, y, z, c, t, 1);

      // write tif image
      IFormatWriter writer =
            new ImageWriter().getWriter("/tmp/imageDemo.tif");
      writer.setMetadataRetrieve(meta);
      writer.setId("/tmp/imageDemo.tif");
      writer.setWriteSequentially(true);
      
      // We are not sure over which dimensions we have to loop...
      for (int j=0;j<t*c*z;++j) {
         writer.saveBytes(j, img);
      }
      writer.close();
   }


Note that we just use random data here and have all image planes contain the same contents. When we open the image in ImageJ (without Bioformats) ImageJ reads it as a stack with t=1, c=1 and 300 z-slices (the units can be ignored for the moment):

Width: 30.00 cm (30)
Height: 20.00 cm (20)
Depth: 300.00 cm (300)
Resolution: 1 pixels per cm
Voxel size: 1x1x1 cm
ID: -2
Coordinate origin: 0,0,0
Bits per pixel: 8 (grayscale LUT)
Display range: 0-255

Opening it with the Bioformats Importer yields the following meta data:

<Pixels BigEndian="true" DimensionOrder="XYCZT" ID="Pixels:0" Interleaved="false" SignificantBits="8" SizeC="1" SizeT="300" SizeX="30" SizeY="20" SizeZ="1" Type="uint8">

So, obviously the Tif file contains either wrong meta data, or ImageJ and Bioformats cannot match the meta data in the file to the data actually available. In addition, interpreting the metadata seems to be done different by ImageJ and Bioformats, however, we would expect this to be unified.
Thanks for your help and best regards,

Birgit
bimoelle
 
Posts: 4
Joined: Thu Mar 12, 2015 12:47 pm

Re: Saving 5D image as Tif and OME-Tif

Postby mlinkert » Mon Mar 16, 2015 3:49 pm

Hi Birgit,

That is expected behavior when saving files with just the '.tif' (or '.tiff') extension; if you use a file name with the '.ome.tif' or '.ome.tiff' extension, then the dimension information should be correctly preserved. The extra '.ome' is what tells Bio-Formats to write an OME-TIFF, otherwise it will write a "plain" TIFF without any metadata beyond the image width and height.

If changing the extension doesn't work, please let us know.

Regards,
-Melissa
User avatar
mlinkert
Team Member
 
Posts: 353
Joined: Fri May 29, 2009 2:12 pm
Location: Southwest Wisconsin

Re: Saving 5D image as Tif and OME-Tif

Postby bimoelle » Mon Mar 16, 2015 5:36 pm

Hi Melissa,
the example indeed works if we use ome.tif and then open the image with Bioformats again. So this obviously means that our data is organized correctly and the metadata is also correct. However, I am bit confused concerning the expected behavior of Bio-Formats in case of writing tifs since on the webpage of Bio-Formats it is stated that OME-TIFF "...provides all the compatibility of TIFF while...". It is obvious that a lot of metadata that the OME-TIFF format supports cannot be properly represented in plain TIF-format. However, ImageJ demonstrates that it should be possible to at least save 5D data correctly with a minimal amount of metadata. Is there any specific reason why Bio-Formats does not support writing valid tif-files? This is really a pity since this renders using Bio-Formats impossible if saved files should, e.g., be further processed with other programs missing support for OME-TIF.
Kind regards,

Birgit
bimoelle
 
Posts: 4
Joined: Thu Mar 12, 2015 12:47 pm

Re: Saving 5D image as Tif and OME-Tif

Postby mlinkert » Tue Mar 17, 2015 10:34 am

Hi Birgit,

The TIFF standard does not support 5D data. ImageJ has defined its own metadata for recording the dimensions, which is simpler than OME-XML but is not part of the TIFF standard and will not be read by software that does not support ImageJ metadata. Bio-Formats does write valid TIFF files if the .tif/.tiff extensions are used, but does not save ImageJ-style metadata.

Note that you can write an OME-TIFF dataset to multiple files (see for example http://www.openmicroscopy.org/site/supp ... iple-files), which may be a useful compromise between recording the OME-XML metadata and making it easier for other software to process the files.

Regards,
-Melissa
User avatar
mlinkert
Team Member
 
Posts: 353
Joined: Fri May 29, 2009 2:12 pm
Location: Southwest Wisconsin

Re: Saving 5D image as Tif and OME-Tif

Postby bimoelle » Tue Mar 17, 2015 11:06 am

Hi Melissa,
thanks for the clarification. I am not sure, if writing multiple files would be an option for us, we will need to check that. I was not aware of the fact that the TIFF standard does not support 5D data and ImageJ writes its own TIF metadata format. We will have to check the different options for us now, and probably it will turn out that OME-TIFF is still the best option. Thanks for your support and quick replies,
best regards,

Birgit
bimoelle
 
Posts: 4
Joined: Thu Mar 12, 2015 12:47 pm


Return to User Discussion [Legacy]

Who is online

Users browsing this forum: No registered users and 1 guest