Processing object histogram to set threshold

Hi there
I’m got stuck in my macro-writing. I have a large number of images with similar objects that I sort by size. They have a typical size distribution:

Ideally I would like to sample a few images and look at all the objects found in those. Make an area histogram of all the objects, find the max (most frequent) and use that for automatic area threshold setting in my analysis of all the images.
Problems I have encountered:

  • Problem to collect the object numbers from several image analyses in one single results table from which I can do the histogram
  • Can’t figure out how to find the max (most frequent) in the histogram within ImageJ (no macro function corresponding to the “list” button in the histogram window)

Any suggestions??

Make a stack of the images, then analyse the stack. The results table should have all the object in the stack.
Then use the Distribution function to plot a histogram of the areas of your objects.

Yes that’s a good Idea – to put the sample images in a stack first – and to then do the analysis. I will try.

Then the problem to do and process the histogram in a macro. I usually work in a very basic fashion when I write macros. Start the macro recoder and then do what
I need to do and finally fill out with what is needed to make it a functional macro.

In this case you can generate the histogram by: run(“Distribution…”, “parameter=Area or=100 and=0-500”); but then is need to get the data out to make a table
inspection and find the max count. You get a nice table when you press the list button in the histogram plot, but the macro recorder does not register anything when I do it and I can’t find any command among the built in macro functions that would do the job,
so for table inspection/max count search I am stuck.

But thanks for the help with the first part / Erik Sörman


See this example

// load and segment stack
run("MRI Stack (528K)");
setAutoThreshold("Default dark no-reset");
setOption("BlackBackground", true);
run("Convert to Mask", "method=Default background=Dark calculate black");

// analyse stack
run("Analyze Particles...", "display clear include stack");
run("Distribution...", "parameter=Area or=200 and=1-30000");

// get and show the data from the histogram of area sizes

Plot.getValues(xpoints, ypoints);
for (i=0;  i<200; i++) {
 print(xpoints[i], ypoints[i]);


Tried to implement this series of ideas. It all works!

Except that I have to confirm that the operations and analyses should be done at the full stack at a number of steps in the macro execution. How can you avoid

Thanks for the help / Erik

(first time I use a forum of this type to get help – great first time experience!)


The code above does not stop anywhere, so it is not possible to know what is that you are referring to.

When writing code in the macro language, it is convenient to use the Macro Recorder so you get the syntax correctly when dealing with stacks as opposed to single images.

Right, a few stack or “stack” was needed here and there in the macro. Now it all works as I want it to. Mission completed. Thank you so much. / Erik