How to get calibration info in ImageJ2


I know that in ImageJ1, you can get calibration info through an ImagePlus using the function ImagePlus.getCalibration(), and you can get even more information using ImagePlus.getProperties(). How would one go about doing this with ImageJ2 classes? Interestingly, .getProperties() from a ImgPlus, or a Dataset returns an empty HashMap. Basically I just want to access the unit length of the pixels of an image.

Thank you


Hi @Rody,

Short answer: use Dataset.averageScale( i )

Longer answer: for better or worse, there are several ways to get at that information. Check out the code below which shows three different ways to do it. Part of why it’s this way is that ij2 is very general - say you wanted one of your axis to be logarithmic.


#@ Dataset data

import net.imagej.axis.Axes;

println( data.averageScale( 0 ));
println( data.averageScale( 1 ));
println( data.averageScale( 2 ));
println( ' ' );

println( data.axis( 0 ).calibratedValue(1.0));
println( data.axis( 1 ).calibratedValue(1.0));
println( data.axis( 2 ).calibratedValue(1.0));

println( data.axis( Axes.X ).get().calibratedValue( 1.0 ));
println( data.axis( Axes.Y ).get().calibratedValue( 1.0 ));
println( data.axis( Axes.Z ).get().calibratedValue( 1.0 ));
println( ' ' );

Thanks John!
I’m guessing there’s no way to get the String name of the unit of length of the pixels? (e.g “microns”). I just wanted to check if calibration information was present in the image, but I suppose one could check this by checking if the pixel width and height are equal to one.

1 Like

Hi @Rody,

That’s possible, I was just lazy and left it out, try:
data.axis( i ).unit()

To learn about everything you can get, check out the doc for Dataset and CalibratedAxis.



That’s awesome, thanks again @bogovicj.