Difficulty writing macros for image segmentation

Hi all, I’m new to ImageJ and coding, also not entirely sure of etiquette for posting, so please correct me if I’ve done this wrong.

Essentially I’m trying to segment out part of an image and then measure it’s area within a 6x6mm circle. The image in questing is a heat map of thickness. I have managed to do it manually (see steps) but then when I tried to create a macros to do the remaining images, it has not worked. I’ll post the images separately

  • Open image
  • Adjust colour threshold (hue 50-200, sat 100-255, brightness 0-255) - mask to black
  • Convert to 8-bit image
  • Adjust threshold 1-255
  • Create circle 0,0,180,180
  • Set measurements to include area fraction and limit to threshold
  • Set scale 180 pixels = 6 mm
  • Measure
  • Save results of measurement

Macros code created:

open("/Desktop/256.png");
run("Color Threshold...");
// 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]=50;
max[0]=200;
filter[0]="pass";
min[1]=100;
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-------------
selectWindow("256.png");
run("8-bit");
setAutoThreshold("Default no-reset");
//run("Threshold...");
//setThreshold(1, 255);
setOption("BlackBackground", false);
run("Convert to Mask");
run("Close");
//setTool("oval");
makeOval(0, 0, 180, 180);
run("Set Measurements...", "area mean min area_fraction limit redirect=None decimal=3");
run("Set Scale...", "distance=180 known=6 unit=mm");
run("Measure");
saveAs("Results", "/Desktop/256results.csv");

Many thanks in advance.
256 final output image

Hi Edward, welcome to the forum.
This macro assumes a given image name at least in the following line:

selectWindow("256.png");

So it is likely to fail when trying to select image “256.png” with another image.
You could replace this by selectWindow(a);

The run("Close"); command before makeOval also probably prevents your macro to finish.

Jerome.

1 Like

Hi Jerome

Many thanks for your help. I did as suggested and it runs through. The only remaining issue is that the colour thresholding doesn’t seem to be following the command. Seems to be defaulting to 0,255 for all the parameters

:thinking:

Ed

Hi, it seems to be working here. I added an Invert and a print statement to check the used threshold values, and they seem to be used when running the macro in debug mode (try to select “Debug Macro” from the editor Debug menu and execute your macro step by step using Ctrl-E to see if/when it fails.)

3 Likes

You, Sir, are a genius!

Thanks so much for your help - a great introduction!

Ed

1 Like