Changing pixels above a certain value into NaN (ImageJ)

How do I change the pixels with values above 1.23 into NaN? Can the NaN also be reflected in the calibration curve? Thankssample.tif|attachment (226.8 KB)

Hello Lisa -

You can do it with a script (or macro).

Here are two jython (python) scripts: The first version (that I like
best and think is the most straightforward) loops over the pixels;
the second (maybe more ImageJ-idiomatic) creates and fills a

The loop version:

from ij import IJ

imp = IJ.openImage ('sample_orig.tif')

ip = imp.getProcessor()
pixels = ip.getPixels()
for  i in range (len (pixels)):
	if (pixels[i] > 1.23):
		pixels[i] = float ('nan')

imp.updateAndDraw() (imp, 'sample_nans_loop.tif')

The mask version:

from ij import IJ
from ij.process import ImageProcessor

imp = IJ.openImage ('sample_orig.tif')

ip = imp.getProcessor()
ip.setThreshold (1.23, float ('inf'), ImageProcessor.NO_LUT_UPDATE)
mask = ip.createMask()

ip.setValue (float ('nan'))
ip.fill (mask)

imp.updateAndDraw() (imp, 'sample_nans_mask.tif')

(To run the scripts you will have to put sample_orig.tif somewhere
where ImageJ will find it, or edit the path in the script.)

Both versions produce identical .tif files with nans, so I’ll only
upload the first version:

sample_nans_loop.tif (226.8 KB)

Purely for forum-display purposes, here is a .png version:


(Please note that the .png version is an 8-bit image, so it
doesn’t – and can’t – have nans.)

I don’t know how to do this (or whether it’s possible).

Thanks, mm

1 Like