ImageJ (plugin Java): auto threshold method doesn't work

2.5k views Asked by At

I try to write Java Plugin to ImageJ, which should:

  • Load image (24-bit).
  • Do some preprocessing operations.
  • Threshold the image in numerous of methods.
  • Do some other operations.

I have problem with threshold operation. Part of my code looks like this:

Opener opener = new Opener();
ImagePlus imp = opener.openImage(source);
// Preprocessing
IJ.run("Threshold..." , method);
// Other operations e.g. "open", "outline" etc.
IJ.saveAs(destination);

My goal is to get binarized image in various methods (e.g. "Default", "Huang", "Intermodes", "IsoData", "Li" etc.). Only way which I can get binarized image is to run:

IJ.run(imp, "8-bit", "");
IJ.run(imp, "Make Binary", "");

however, I get an image binarized by only one method. How to do automatic threshold by running Java code (ImageJ plugin)?

2

There are 2 answers

2
Jan Eglinger On BEST ANSWER

The auto-threshold methods in the Threshold dialog all are algorithms working on single channel (8-bit or 16-bit) images. In the Color Threshold dialog, they are applied exclusively to the Brightness channel of your 24-bit color image.

To reproduce this in Java, use the following code:

IJ.run(imp, "HSB Stack", "");
imp.setSlice(3);
IJ.setAutoThreshold(imp, "Triangle dark");
Prefs.blackBackground = true;
IJ.run(imp, "Convert to Mask", "only");

(Converting your image to 8-bit is nothing else than using the Brightness channel, discarding the Hue and Saturation information. Unless you really make use of the other sliders in the Color Threshold dialog, you can just as well convert the image to 8-bit before applying the threshold.)

7
Ortomala Lokni On

You seem to use the IJ.run method the wrong way. The first parameter is a string containing an ImageJ command and the second parameter is a string containing the options of this command. From the documentation:

public static void run(java.lang.String command, java.lang.String options)

Runs an ImageJ command, with options that are passed to the GenericDialog and OpenDialog classes. Does not return until the command has finished executing. To generate run() calls, start the recorder (Plugins/Macro/Record) and run commands from the ImageJ menu bar.

You can also use the GUI to record a macro, Plugins->Macros->Record..., set the record mode to Java, and select the methods and threshold values you want. You will obtain something like this:

// Color Thresholder 1.49i
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=139;
max[0]=254;
filter[0]="pass";
min[1]=48;
max[1]=110;
filter[1]="pass";
min[2]=189;
max[2]=255;
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  if (filter[i]=="stop")  run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
  selectWindow(""+i);
  close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
// Colour Thresholding-----------------