Login  Register

Re: How to read current density calibration from an image in a macro

Posted by Rasband, Wayne (NIH/NIMH) [E] on Oct 21, 2015; 12:42am
URL: http://imagej.273.s1.nabble.com/How-to-read-current-density-calibration-from-an-image-in-a-macro-tp5014685p5014691.html

> On Oct 20, 2015, at 6:55 AM, Stein Rørvik <[hidden email]> wrote:
>
> Hello,
>
> I have some 16 bit CT images (similar to the CT image available in the open samples menu) for which I need to read the current density calibration from within a macro. I cannot figure out how to do that in a reliable way. The density values are calibrated from minus a few thousands to plus a few thousands across the 0-65535 range.
>
> There is already a "calibrate(pixelValue)" macro available; what I need is a function like "uncalibrate(densityValue)" or "getRawPixelValue(densityValue)" to get the corresponding pixel value.
>
> I wrote these lines as a workaround, but this approach does not work if the image is not straight line calibrated.
>
> calibOffset = calibrate(0);
> calibSlope = calibrate(1)-calibrate(0);
> rawValue = (calibValue-calibOffset)/calibSlope;

16-bit CT images normally use straight line calibration so the above approach should work. For images that are not straight line calibrated, call the Calibration.getRawValue() method in a script, for example:

  value = -440;
  img = IJ.getImage();
  cal = img.getCalibration()
  rawValue = cal.getRawValue(value);
  print(rawValue);

You can use the eval() function to run this code in a macro, for example:

  value = -440;
  code = "IJ.getImage().getCalibration().getRawValue("+value+")";
  rawValue = eval("js", code);
  print(rawValue);

-wayne

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html