How do "Color Threshold" in Python? (imageJ)

Hi everyone. I am Daniel.

I am a new user of ImageJ, which is necessary for my data analysis.

I write Macro by Python in ImageJ. I want to do “Color Threshold” in Python Script. To do “Color Threshold” in Python Script, I run Macro Recorder and press the “Macro” button but, The result did not perform to Python Script because of syntax is completely different.

// Color Thresholder 2.1.0/1.53c
// 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]=0;
max[0]=255;
filter[0]="pass";
min[1]=0;
max[1]=255;
filter[1]="pass";
min[2]=133;
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-------------

“Color Threshold” means that

“Macro Recorder” means that I chose Bean Shell

I found some topics related to it, but I couldn’t understand it.

How can I do Color Threshold in Python?

I’d be really happy with your help.

Cheers

If there is any obscure point , please tell me.

If you update ImageJ to the latest version you can select Jython for the macro recorder, see:

But I never tested if this works with the Threshold Color.

WoW, This most amazing news in 2020.

But sadly, this recorder dose not work to Python3.

The result is that.

IJ.run(imp, "Color Threshold...", "");
// Color Thresholder 2.1.0/1.53g
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
call("ij.plugin.frame.ColorThresholder.RGBtoLab");
run("RGB Stack");
run("Convert Stack to Images");
selectWindow("Red");
rename("0");
selectWindow("Green");
rename("1");
selectWindow("Blue");
rename("2");
min[0]=130;
max[0]=255;
filter[0]="pass";
min[1]=0;
max[1]=255;
filter[1]="pass";
min[2]=0;
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-------------
IJ.run("script:Script.py.py", "");

But I decide to translate this code to Python.

Big Thanks for your help.

Well python scripting in Fiji (aka jython) is python 2 exclusively.
Then you have to find the function you need in the ImageJ API for instance.

Maybe there is such an API for the color thresholder too, I dont know.

Thanks for Reply.

I am now looking for that API and writing the code in Jython.