Calculating Raw Integrated Density of Thresholded Image -- Odd Results

I am very new at using macros for image analysis. I would love any help troubleshooting my current problem. I have written a macro to calculate the sum of the pixel values in each of multiple ROIs in a single image. (I believe this is called the raw integrated density.) I attempted to make the macro only sum pixels above a certain threshold (50). However, I got strange results. With a threshold of 50, I got multiple ROIs with a value of “NaN,” which doesn’t make sense if RawIntDen is a sum. With a threshold of 0, I got no NaN values, but when I removed any threshold language entirely, I got values that in many cases were smaller than the 0 threshold values, which again makes no sense if RawIntDen is a sum! Am I misreading what RawIntDen is? Please help me fix my macro. Thanks!

//duplicate image and change to 8 bit
run("Duplicate...", "title=Duplicate.tif");

//Threshold image
setThreshold(50, 255);

//Select all ROIs in the Manager
selectWindow("ROI Manager");
for(i=0; i<count;i++) { 
        array[i] = i; 
roiManager("Select", array);

//Calculate Raw Integrated Density for all ROIs
run("Set Measurements...", "integrated limit redirect=None decimal=0");

ok… just a quick check - but are you running ‘Measure’ on the original image? be sure you select that guy again and aren’t running it on your mask…

so somewhere in the beginning - before you duplicate:

id = getImageID(); // get original image id

and then to measure on your original dataset (something like this):

selectImage(id); // activate original image
roiManager("Show All with labels"); // overlay ROIs
roiManager("Measure"); // measure on original image

Just a thought - though I haven’t run/tested your code at the moment…


1 Like

Thanks for the reply. I believe I am running measure on the duplicate of the original image. I duplicated the image to turn it into an 8-bit without worrying about changing my original data. Thank you for the code line "id = getImageID(); I’d been trying for awhile to do something like that and hadn’t figured out how. However, I believe the error in my RawIntDen calculation lies elsewhere, and may have to do with how ImageJ calculates RawIntDen, which is what I’d really like to discover.

I wasn’t able to reproduce the behavior your describe with the macro you posted. I tried on the Blobs 8-bit sample image and also on a 32-bit image that itself contained NaN pixels. My results never contained NaN in the RawIntDen column.

Can you provide an example image that reproduces the issue?

Which ImageJ version are you running?


I am trying to share the ROI set with you all, but I only get an error message that “new users can’t upload attachments.” Trying to figure this out.

Test image: download

ROI Image: download

Did those images load? The ROI Image can be used to create an ROI Set using the below macro; the other image is the one I use the quantification macro on.

run("Duplicate...", "title=Duplicate.tif");
setThreshold(8, 255);
run("Make Binary");
run("Options...", "iterations=2 count=1 pad edm=Overwrite do=Nothing");
run("Options...", "iterations=9 count=1 pad edm=Overwrite do=Nothing");
run("Make Binary");
run("Create Selection");
run("Add to Manager");
roiManager("Select", 0);
roiManager("Select", 0);

Oh sorry, the ImageJ version I’m using is 1.51j8.


Using your two macros on the two images you provide, I get measurement values of NaN for exactly one of the regions, ROI number 25.
Checking on the thresholded image after activating this ROI, I can see that there are no thresholded pixels within that region, leading to the NaN measurement.


Oh, NaN just means there were no thresholded pixels?!?

I feel dumb.

Ok, I think I can figure the rest of this out from here.

Thanks for your help!