Get histogram 32 bit Image

Dear Forum,
maybe I didn’t search well enough, but I am not founding a way to get the histogram of a 32 bit image in a Java plugin.
Thank you very much,

What do you mean by that? How does it differs from using the ImageJ histogram tool?

Hi @iarganda,
I mean I would like to have an array with the distribution of the pixels intensities for a 32 bit image.

Something like the histogram or histogram16 fields of ImageStatistics class.

I would prefer that (instead of the ImageJ histogram tool) since I would like to use the distribution to implement an automatic way to define a threshold.

I hope I made myself clear,
thank you
Emanuele Martini

I see! That’s probably available in ImageJ2, isn’t it, @ctrueden? @hinerm?

1 Like

Why not to use the autothreshold method then?

private Roi getThreshold(ImagePlus imp, String Method) {
    ImageProcessor ip2 = imp.getProcessor().duplicate();
    ImagePlus imp2 = new ImagePlus("Thresholded " + Method, ip2);
    boolean darkBackground = Method.contains("dark");
    if (!darkBackground) {
    ThresholdToSelection ts = new ThresholdToSelection();
    Roi roi = ts.convert(ip2);

where method is selected from this dialog

//  Dialog to handle the method and the background
        GenericDialog gd = new GenericDialog("Planar Uniformity.");
        gd.addChoice("Select threshold method:", mMethodStr, "Default");
        gd.addCheckbox("Dark Background", true);
        if (gd.wasCanceled()) {
        String method = gd.getNextChoice();
        boolean darkb = gd.getNextBoolean();
        if (darkb) {
            method += " dark";
        // end Dialog

I strongly recommend using parameters instead of a GenericDialog for harvesting input whenever possible. The code is simpler, more robust, works headlessly and can be applied to both plugins and scripts.

For example, writing an IJ1 Macro in the script editor, you can accomplish this task with two lines:

// @String(label="Select threshold method", choices={"Default", "My Cool Threshold 23"}) method
// @Boolean(label="Dark Background") dark

You can use Ops to quickly get a Histogram e.g., in a python script:

# @ImgPlus img
# @OpService ops

out = ops.image().histogram(img)


This code is equivalent for a Java plugin (using @Parameter-annotated ImgPlus and OpService fields). It should work with any input image type (e.g. I just tested with a random 32-bit image)


Thank you very much
I will try it

You can use the getStatistics() method to get a 256 bin histogram of a 32-bit image. Here is a JavaScript example:

img = IJ.createImage("tmp","32-bit random",500,500,1);
stats = img.getStatistics();
histo = stats.histogram;
for (i=0; i<histo.length; i++)
   IJ.log(i+" "+histo[i]);

I tried that but it raises

-------------------------------------------------------------[218,8] error: illegal start of type[218,49] error: ';' expected
2 errors 

What do I need to use that solution?

It also says String cannot be converted to Annotation

Here is a complete example of one way you could structure a Java command using Ops to make a histogram with a user-selected bin count:

import net.imagej.ImgPlus;
import net.imagej.ops.OpService;
import net.imglib2.Cursor;
import net.imglib2.histogram.Histogram1d;

import org.scijava.command.Command;
import org.scijava.command.ContextCommand;
import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

@Plugin(type = Command.class, menuPath = "Plugins > Examples > Ops Histogram")
public class UseHistogram extends ContextCommand {

    private ImgPlus img;

    private OpService ops;

    private LogService logService;

    @Parameter(label = "How many bins?")
    private int bins = 256;

    public void run() {
        Histogram1d histogram = ops.image().histogram(img, bins);
        Cursor cursor = histogram.cursor();"Number of bins: " + histogram.getBinCount());"Number of values: " + histogram.valueCount());

        int i = 1;
        while (cursor.hasNext()) {
  "Bin " + i++ + " counts: " +;


It requires a dependency on imagej-ops. To run in Fiji you should have the Java-8 update site enabled.


ok thank you all!
have a nice week
Emanuele Martini