[ome-devel] Problems reading/writing calibrations in metadata

Jean Ollion smogollion at yahoo.fr
Mon Jun 29 17:38:55 BST 2015


Hello,
I have trouble reading and writing calibration in metadata. Here is a
minimal example, written as a JUnit test.

@Rule
    public TemporaryFolder testFolder = new TemporaryFolder();
@org.junit.Test
    public void testIOImageCalibration() {
        String id = testFolder.newFolder("imageTest").getAbsolutePath() +
File.separator + "imageTest.tif";
        int w = 512, h = 512, c = 1, z = 1;
        int pixelType = FormatTools.UINT16;
        byte[] img = new byte[w * h * c *
FormatTools.getBytesPerPixel(pixelType)];
        for (int i = 0; i < img.length; i++) {
            img[i] = (byte) (256 * Math.random());
        }
        double calX = 0.1d;
        double calZ = 0.23d;
        try {
            ServiceFactory factory = new ServiceFactory();
            OMEXMLService service =
factory.getInstance(OMEXMLService.class);
            IMetadata meta = service.createOMEXMLMetadata();
            MetadataTools.populateMetadata(meta, 0, null, false, "XYZCT",
FormatTools.getPixelTypeString(pixelType), w, h, z, c, 1, c);
            meta.setPixelsPhysicalSizeX(new Length(calX, UNITS.MICROM), 0);
            meta.setPixelsPhysicalSizeY(new Length(calX, UNITS.MICROM), 0);
            meta.setPixelsPhysicalSizeZ(new Length(calZ, UNITS.MICROM), 0);

            IFormatWriter writer = new loci.formats.ImageWriter();
            writer.setMetadataRetrieve(meta);
            writer.setId(id);
            writer.saveBytes(0, img);
            writer.close();
        } catch (Exception e) {
            fail("problem writing image to disk");
        }

        ImageProcessorReader r = new ImageProcessorReader(new
ChannelSeparator(LociPrefs.makeImageReader()));
        IMetadata meta = null;
        try {
            ServiceFactory factory = new ServiceFactory();
            OMEXMLService service =
factory.getInstance(OMEXMLService.class);
            try {
                meta = service.createOMEXMLMetadata();
                r.setMetadataStore(meta);
            } catch (ServiceException ex) {
                fail("problem setting metadata");
            }
        } catch (DependencyException ex) {
            fail("problem setting metadata");
        }
        try {
            r.setId(id);
            r.setSeries(0);
            Length lx = meta.getPixelsPhysicalSizeX(0);
            Length lz = meta.getPixelsPhysicalSizeZ(0);
            Assert.assertTrue("testing calibration X is not null?",
lx!=null);
            Assert.assertTrue("testing calibration Z is not null?",
lz!=null);
            Assert.assertEquals("testing calibration X retrieve", calX,
lx.value());
            Assert.assertEquals("testing calibration Z retrieve", calZ,
lz.value());
            r.close();
        } catch (FormatException exc) {
            fail("error reading image");
        } catch (IOException exc) {
            fail("error reading image");
        }
    }

It appears the objects "lx" and "lz" are null.
Could you point me the error?

I also have a slide question: here I use the ImageProcessorReader for
simplicity, is there a reader that outputs directly one primitive array per
plane, in order not to use imageJ's library?
Thank you,
Best,
Jean
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openmicroscopy.org.uk/pipermail/ome-devel/attachments/20150629/0bb926ac/attachment-0001.html>


More information about the ome-devel mailing list