How to obtain an integer image?

Hi there!

How can I obtain an integer(gray) image? I have float, short, byte even RGB processors but no integer one. I even try the image and stack converters but it seems not working at all. Having ip0 as a FloatProcessor and ns as the number of images in the stack I do:

ImagePlus imp0 = new ImagePlus("Geometric mean.", ip0); if (ns==1) { new ImageConverter(imp0).convertToGray32();} else { new StackConverter(imp0).convertToGray32();} imp0.updateAndDraw(); imp0.show();

But the resulting image is still float, I even try to save it and mhd says it is float.

ImageJ 1.x does not support 32-bit integer signed (int32) or unsigned (uint32) pixel types. You need to use ImageJ2 data types for that. Here is an example Groovy script:

// @DatasetService dataset
// @OUTPUT Dataset image

import net.imagej.axis.Axes
import net.imagej.axis.AxisType

long[] dims = [512, 512]
name = "A 32-bit integer image"
AxisType[] axes = [Axes.X, Axes.Y]
bitsPerPixel = 32
signed = false
floating = false

// create the image!
image = dataset.create(dims, name, axes, bitsPerPixel, signed, floating)

// populate the pixels with a simple gradient
counter = 0L
for (pixel in image) {
	pixel.set(counter++)
}

Note that while you can create e.g. uint32 images using the ImageJ2 and ImgLib2 libraries, the legacy ImageJ 1.x user interface (which is what ImageJ and Fiji currently use by default) will still convert them under the hood to float32 for backwards compatibility.

You can see this (problematic) behavior if you change the gradient to:

counter = Integer.MAX_VALUE
for (pixel in image) {
	pixel.set(counter--)
}

And then inspect the pixel values. You’ll see that the displayed float32 values do not match what you should have with a uint32 image, due to type conversion rounding.

If you use SCIFIO, you can save ImageJ2 Dataset objects properly, so the correct type is preserved. However, the MHD/MHA saver plugin is unfortunately still written to use the ImageJ 1.x API, so it will not be able to save as uint32 regardless. We will need to rewrite that plugin as a SCIFIO plugin for that to work. Note that the LOCI ImageJ team does have plans to rewrite all the image readers and writers of fiji/IO to be built on SCIFIO… but it is unlikely we will have time in the near future. Help from the developer community to expedite the transition would be most welcome.

Bad news then! Is there a workaround with current IJ1 approach? I can open an integer image and when saving using MHD plugin it is saved as integer, so there shall be something I can do. (I was thinking hacking imageplus properties)

Glancing at the MetaImage_Writer and FileInfo source code, I would be quite surprised if there was an easy hack here. But I am not certain.

I think that is because the original FileInfo type field is preserved as GRAY32_UNSIGNED, even though ImageJ 1.x coerces the data into a float32. So while the type may be preserved, the data won’t be.

The most pragmatic workaround at the moment is probably to convert your images using a different tool than ImageJ. Or, for a talented coder, to port the MetaImage_Writer to SCIFIO, since it would not be very complicated to do so.