Histogram "Count:" from many ROI

histogram
roi-manager
roi
Tags: #<Tag:0x00007fd543fe14e0> #<Tag:0x00007fd543fe12d8> #<Tag:0x00007fd543fe1008>

#1

Hi I was wondering, instead of selecting each ROI individually and hitting ctrl+h to get to the histogram and copying down the number for “Count:” (e.g. 1245) for 100 different ROIs, is there a way to grab these numbers automatically, either within imagej/fiji itself or must I write a script? I can take a stab at it, I would assume it’s getHistogram function looping for the ROIs but not sure of command to get the single “Count:” number.

Thanks for any help!


#2

Hi @lcsglvr,

If you just want the “Count” from the histogram, does this not mean that all you want is the number of pixels in the selection?

Isn’t this just the uncalibrated “area” measurement? Or it appears getRawStatistics(nPixels, mean, min, max, std, histogram) will do this for you with nPixels. I presume it works with a selection.

So you would want to do a script looping through each ROI using e.g.

run("Clear Results");
roiCount = roiManager("count"); 
for (i=0; i<roiCount; i++) {
    roiManager("select", i);
    getRawStatistics(nPixels, mean, min, max, std, histogram);
    setResult("Count", i, nPixels);
    updateResults;
}

I think that should work. Let me know how it goes.

Rob


#3

Thanks for the quick response, Rob! It is indeed the “Area” count. When I run the script, however, it does not report that “Area” value or the “Count:” value in the histogram – it reports some other value. It’s not a huge deal, because I can just Alt+A all the in ROImanager and copy/paste the list but was just curious!
Not sure what value it’s reporting.
The example of the ROI is 982 but it is reporting 11872


#4

Hi @lcsglvr,

Could you explain specifically what the value that you are looking for is? The “count” value in the histogram is the number of pixels in total for the selected ROI, as far as I’m aware.

If you have ROIs in the ROI manager, the script should run through each one and get that value, and add it to a new row in the results table (I’ve only tested on a single image with 5 ROIs, but it works).

I’m curious as to the solution that you’ve found. Does “Alt+A” select all ROIs? And then you’re copying the results table “Area” values? Just to make it clear for other users who find this topic.

Best,

Rob


#5

Hi Rob, I set a threshold for an 8bit microscope image and then I look at the histogram of several ROIs (each cell is an ROI). I then want to get the count of the pixels with that thresholded value,

Count: is the number of pixels that have that value

Anyway, I just ended up writing macro with this instead…

roiManager(“Show All”);
roiManager(“Show All without labels”);
roiManager(“Measure”);

it goes on then to autosave for me


#6

I see, there was no mention of thresholding anywhere, sorry! The number you would get from my script would be the area, in pixels, which is the same as the single “Count:” number found in the histogram. But that’s the count of all of the pixels in the selection, regardless of grayscale value.

Now i understand your problem, but i still can’t get a different value in the histogram window for “Count:” after thresholding. The histograms of my ROIs still show “Count:” as the total number of pixels in the selection. The same number as “Area” when i use ROI manager “measure”.

I wonder what is different between our two methods? I am stumped. Are you saving the “Area” result in your method?

Are you talking about the “count=” value shown when you hover over a value in the histogram? E.g. If thresholded 8-bit your image should be 0 or 255. The count @ 255 will be your value of interest. Though not sure how you’re getting that from the ROI manager “measure” function?

Best,

Rob


#7

I think it could be the calibration of the image… I think the count is always the count in pixels so the area in pixels instead the “area” coming from “measure” is calibrated if the image has a physical calibration it would be in that unit of measure sqaured while if the image is not calibrated it will end up in a pixel area that has to be the same of the one you could get using the pixels histogram count.

have a nice day,
Emanuele


#8

Sorry for not responding - was away.
As emartini states, it is scaled or calibrated (not sure of correct term!) from a micoroscope file (from associated metadata) that was imported. If you would like me to see the metadata associated with the microscopeimages, let me know how to access that in imagej/fiji

I ended up getting the measurements from ROImanager and saving as CSV file the results… then just copying them to a master xls file I am using for all files: in master xls file I’m have the “Thresholded” value and then associated histogram counts/ROImeasurements of the selected ROI circles (cells)

Here is the script: It’s probably a messy way but gets the job done for me right now. Of course I would welcome and streamlined/nicer way : ) if want.

// written by: Lucas R Glover, DPhil; originally written October 2017

//this section just defining variables
imgdir = getDirectory("image"); //"
OrigImage=getTitle();  //returns a string with the image title 
titlexls = OrigImage + ".csv";
x=100; y=100; 

run("Clear Results"); //clear results from prior images
run("Set Scale...", "distance=0 unit=pixel global");
run("Set Measurements...", "area limit redirect=None decimal=3");

run("8-bit"); //converting image to 8bit
  
x=1300; y=950; //dialogue box pops up to remind you to drag in your ROI set 
  call("ij.gui.WaitForUserDialog.setNextLocation",x,y); 
  waitForUser("please drag/load your ROI.zip, if have one set!"     +x+","+y)

run("Threshold..."); //runs the Threshold program so you can set an appropriate threshold
  call("ij.gui.WaitForUserDialog.setNextLocation",x,y); 
  waitForUser("done setting threshold?"      +x+","+y)
  
 
//this section then measures the red dots/area of each ROI from thresholding and save it into a csv file 
//in the image's directory with its title
selectWindow("ROI Manager");
run("Select All");
roiManager("Show All");
roiManager("Show All without labels");
selectWindow("ROI Manager");
run("Select All");
roiManager("Measure");
saveAs("Results", imgdir + OrigImage + '.csv');


title = "save ROIs?";
  width=512; height=512;
  Dialog.create("New Image");
  Dialog.addCheckbox("save ROIs?", true);
  Dialog.show();
  saveROIs = Dialog.getCheckbox();
  if (saveROIs==true) 
   roiManager("Save", imgdir + 'RoiSet.zip');

//clean up spare memory
call("java.lang.System.gc");
call("java.lang.System.gc");
call("java.lang.System.gc");
call("java.lang.System.gc");

#9

PS- in latest Fiji update, when I got to the Threshold dialog box (ctrl+shift+T), it doesn’t have the “Set” button anymore so my image now becomes black-and-white dots etc and gives different values. I went back to older version so I could get back the “Set” button… Not sure if this was meant to change in newest version, got deleted somehow, or I am just thick and not able to adapt to new ways… but I think it also gave me different values. Anyway, just letting whomever know


#10

Good day Lucas R. Glover,

please try to stay informed about changes in new/current versions of ImageJ here:
https://imagej.nih.gov/ij/notes.html

1.51r 15 October 2017
Paragraph 2:

Thanks to Philippe Carl, replaced the min/max threshold value labels in the “Threshold” dialog by editable text fields and removed the “Set” button. New values are validated once the user hits the enter key. Also added mouse wheel and arrow key support to the min and max sliders. The up/down keys increase/decrease the threshold values when the cursor is within one of the text fields.

HTH

Herbie


#11

Cheers @lcsglvr,

Limiting to the threshold was the difference, I didn’t know this was an option. Thanks for clearing it up.

As an aside, I would think about setting the threshold of your image using an automated method (algorithm in Image -> Adjust -> Auto Threshold) or by limits set for all images, as best practise. And if you do that, you will be able to automate all of your code! E.g. you can load ROIs using roiManager("open", roiDir + roiFileName); where roiFileName is the name of the .zip file (assuming you save them related to the image name, you can make this variable from your OrigImage variable + whatever string you add). And the threshold can be scripted also, so then you’re running hands-free and without subjective choice of threshold!

Best,

Rob


#12

Fantastic. Thanks for clearing up for me. Yes, should have kept up.
Best, Lucas


#13

Ah! Good to hear. Now if I could just draw the circles automated… The thresholding can be tricky because slightly depends on how good the original microscope slide was. So sometimes the Auto picks up a lot of noise (it’s there, of course, I just want to threshold it out manually).