How to get histogram count of a stack tif by marco?

Dear all,

I tried to analysis histogram result of a tif file and save it to txt or xls.

When I do histogram manually, I got a figure as

and list the result as, the value “0” has “102976919” count.

When I run the histogram by macro, the left line of the list became “bin start”

Also if I run the the “histogramlister” macro (https://imagej.nih.gov/ij/macros/HistogramLister.txt), I got different count number. The value “0” has “118589” count. It only counted for the first stack.

So how can I just copy the total count number (102976919) for the tiff stacks from the list and save it?
And why the line became “bin start” if I run histogram by marco?

@zhenglili

Let’s see if I can help at all… So to start - I tested the Histogramlister macro - it seems to only count the first slice in a stack… so that is why you have that discrepancy in counts. So do not fret for that. You’d have to modify that macro code to move through your entire image stack…

I tried something:

getDimensions(width, height, channels, slices, frames);

// move through each slice of the image
for (i = 1; i < slices+1; i++) {
setSlice(i);

// This macro generates a 256 bin histogram and
// displays the counts in the "Results" window.

  nBins = 256;
  row = 0;
  getHistogram(values, counts, nBins);
  for (j=0; j<nBins; j++) {
      setResult("Value", row, values[j]);
      setResult("Count #" + i, row, counts[j]);
      row++;
   }
   updateResults();
}

But this just gives you a table with each individual slice’s Counts printed out. I just need to figure out a way to add them across… but should work in theory. I’m looking into List Functions to see if I can re-write this another way.

This is not ideal - obviously - but might lead you or someone else to getting you all the way there…

eta :slight_smile:

this was another example i found that did the same thing - but a bit more clean:

  run("Clear Results"); 
  setOption("ShowRowNumbers", false); 
  for (slice=1; slice<=nSlices; slice++) { 
     setSlice(slice); 
     getRawStatistics(n, mean, min, max, std, hist); 
     for (i=0; i<hist.length; i++) { 
        setResult("Value", i, i); 
        setResult("Count"+slice, i, hist[i]); 
     } 
  } 

Thank you so much.

I do have the marco. It gives a table of each slice. Do you have any idea about getting the total account (all stacks) for each value, just like the manual histogram?

Hey @zhenglili

Thanks to @ctrueden’s help… I was able to come up with a solution for you:

run("Clear Results"); 
setOption("ShowRowNumbers", false); 
for (slice=1; slice<=nSlices; slice++) { 
  setSlice(slice); 
  getRawStatistics(n, mean, min, max, std, hist); 
  for (i=0; i<hist.length; i++) { 
    setResult("Value", i, i); 
    setResult("Count"+slice, i, hist[i]); 
  } 
} 

// new we can add up the counts row-by-row...
resultRows = nResults;
for (k=0; k< resultRows; k++) {
  tot = 0;
  for (l=1; l <= nSlices; l++) {
    ntot = parseInt(getResult("Count"+l, k));
    tot = tot + ntot;
  }
  print("Value " + k + " = " + tot);
}

Hope this helps! I learned a lot doing it… so thank you! :slight_smile:

eta :slight_smile:

1 Like

Thank you so much. It works great.

2 Likes