Interactive colour threshold in macro

Dear all,

I am trying to measure area of a given color in images - but this color changes form one image to another. I would like to write a macro that :

  • opens the image,
  • opens the Color Threshold interactive window and allows me to set hue, saturation and brightness threshold (and letting me see the thresholded image to adjust it)
  • then applies this treshold and measures percentage area.

My issue is to get and set treshold values. I tried to record a macro using Color Threshold “macro” button, and then to add “wait for user” commands :

macro "test"
{
open('M1-1.JPG')
selectWindow("M1-1.JPG");
run("Clear Results");

//color treshold
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");
filter[0]="pass";
filter[1]="pass";
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
     waitForUser("Set mask","Threshold set?");
  getThreshold(lower,upper);
  setThreshold(lower,upper);
  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);
run("Close");


run("Analyze Particles...", "show=Nothing display clear");
run("Summarize");
run("Close");
} 

However the Colour treshold window does not appear and I cannot set the treshold manually. I read treads about interactive threshold setting but I am unable to adapt it to this case, were hue, saturation and brightness need to be set at the same time.

Many thanks,

Marypop

Marypop,

maybe something like the following example code will help:

run( "Set Measurements...", "area area_fraction redirect=None decimal=3" );
setOption( "BlackBackground", true );
run( "Lena (68K)" );
img = getImageID();
run( "Color Threshold..." );
waitForUser( "Set threshold and click OK." );
selectImage( img );
run( "Make Binary" );
run( "Measure" );
selectWindow( "Results" );

Paste the code to a new Macro text window and run it. (Internet connection required for image loading.)

HTH

Herbie

3 Likes

Exactly what I was looking for. Many thanks!

Hi all,

Sorry for this question, but the command “waitForUser” can be used to a macro for several images, so inside a loop?

thanks so much!:slight_smile:

Sure, just try it and you’ll see.

1 Like

ok, thanks. I was trying now :slight_smile: