Macro Language Help

imagej
macro
thresholding

#1

I’m not very familiar with coding, but I need some help with setting up my macro so that I can analyze a large amount of images. I am trying to find the area of a filtered image by using the color threshold. I am able to put the setting of the HSB I want to get filtered out into macro, but I cannot set it so that it selects the filtered area and not the entire image. I usually can do it manually by operating this list of command: color threshold> set HSB setting> select> measure. It seems to me that I am missing the command for the “select” part. Here is my macro so far:

run("Color Threshold...");
// Color Thresholder 1.51j
// 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]=59;
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-------------
run("Close");
run("Measure");

Does anyone know where and what I need to add?


#2

Welcome @314 ,
Recently almost the same question was answered. Have a look there. Most likely, prior to executing the Measure… command, you need to add something like

run("Set Measurements...", "area limit redirect=None decimal=9");

where ‘limit’ is the essential word. This limits the measurement to the area that is currently selected (in your words: ‘selects the filtered area’). That gives this (slightly altered) macro code below; probably you want to re-load your original image and measure it with a redirect, or mask out the original image data before measuring as now the macro only measures the surface area.

//run("Color Threshold…");
// Color Thresholder 1.51j
// Autogenerated macro, single images only!
run("Cardio (768K, RGB DICOM)");

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]=72;
max[0]=186;
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-------------
setThreshold(1,255);
run("Set Measurements...", "area limit redirect=None decimal=9");
run("Measure");

#3

Thanks for the help @eljonco. I ran the extra lines of code in the batch macro for my images, then tested one image out manually by hand and got the same area. Interestingly though, I did not get the same mean, integrated density, nor raw density from the macro measurement and the manual measurement. I’m guessing this is because I made my image into black and white to find the area in the macro? Is there a way around this?


#4

Yes, redirect the measurement to the original image, or use the black and white as a mask to mask out the area in the original image, or create a ROI in the roiManager and use the ROI to select the area in the original image.
For finding the area, first duplicate the original image, then do the above steps to create the black and white and convert it to a ROI, add that to the roiManager and close the black and white image. The roi will still be available in the Manager, and your original image is not touched. Selecting a roi while your original image is in focus (frontmost window), will select that roi area in your original image. Then measure, which will give you the measurement based on original values.