ImageJ(插件 Java):自动阈值方法不起作用

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

我尝试编写 Java ImageJ 插件,它应该:

我对阈值操作有疑问。我的部分代码如下所示:

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

我的目标是通过各种方法(例如"Default"、"Huang"、"Intermodes"、"IsoData"、"Li" 等)获得二值化图像。 我可以获得二值化图像的唯一方法是 运行:

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

但是,我只用一种方法得到了二值化图像。 如何通过 运行ning Java 代码(ImageJ 插件)做自动阈值?

您似乎错误地使用了 IJ.run 方法。第一个参数是包含 ImageJ 命令的字符串,第二个参数是包含此命令选项的字符串。来自文档:

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.

您也可以使用 GUI 录制宏,Plugins->Macros->Record...将录制模式设置为 Java,select你想要的方法和阈值。你会得到这样的东西:

// 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-----------------

Threshold dialog all are algorithms working on single channel (8-bit or 16-bit) images. In the Color Threshold 对话框中的自动阈值方法,它们专门应用于 24 位彩色图像的 亮度 通道。

要在 Java 中重现此内容,请使用以下代码:

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

(将图像转换为 8 位无非是使用 亮度 通道,丢弃色相和饱和度信息。除非您真的使用了其他滑块颜色阈值 对话框,您也可以在应用阈值之前将图像转换为 8 位。)