Help macro matrix text

Hello Community,

I’m very new to imagej and I need your help. I got a stack of 100 images (2000x2000) in 32 bits with background = NaN. I need to create a .txt file of matrix (100x1) representing the histograms peaks of each image (there is one peak per image).
How can I process?

If you need the maximum value you can easily measure all stacks like this:

  1. Select the whole image (STRG+A)
  2. Add the selection to the ROI manager (Press ‘t’ or use Edit->Selection->Add to Manager)
  3. Use the Analyze->Set Mesurements to measure the 'Min&Max value
  4. Use the ROI Manager action More->Multi Measure to measure all slices of the selection

You can then save the table as *.csv to create your text file.

Is that what you’re trying to archive?

Of course you can also iterate over the stack and use the macro functions getStatistics or getHistogramm, see:

Here an example:

Thanks for your quick answer. What I want in my matrix is not he maximum or minimum gray values but the most represented interval of gray values in each histogram (peak of the histogram) that is the gray value interval where there is the maximum number of pixels. The problem is how to do it automatically by taking that value and adding it in a text file.
For now I have to open each image, analyze the histogram and manually report the maximum value which is time consuming.

Thanking you,


Here a quick macro which gives you the maximum count on a bin for each slice which you can then save as a text file (please adjust the number of bins to a reasonable value - here we use the default 256 bins).
Please import your images as a stack:

n = nSlices;
for (i = 1; i <= n; i++) {
    getHistogram(values, counts, 256);
    /*For each slice we calculate the max bin count!*/
    count = 0;
    index = 0;
    binStart = 0;
    for (u = 0; u < values.length-1; u++) {

        if (counts[u] > count) {
            count = counts[u];
            index = u;
            binStart = values[u];

    /*Here we print the result which could be saved as text!*/
    print(index + "  "+ binStart + "  " + count);


Please note that the bin index starts with 0 (0-255) in this macro like the ‘List’ command in the histogram GUI action!

Yeah! It worked. Two ideas, two good results!

Thank you community!