YUV Color Thresholding / Segmentation - Macro Issues

Hi All,

I’m new to using macros in ImageJ, and programming in any shape or form is not a skill I possess. Therefore any help is much needed and very much appreciated

I have devised a multi-step process for analysing my images, which works manually. However, when I try to record this process using the macro recorder in ImageJ and then run the macro on an image, the outcome is far from that desired (see below for images and macro code).

I think the issue lies in how the macro converts the RGB image to YUV

Also, when I try to run it, the macro generates the following message: “Clear Outside. This command requires a selection”. Then the macro stops.

I need to automate the process, as I have around 1000 images to analyse (each image takes around 10 minutes to analyse manually). So I would like to use the macro for batch processing.

The process is a sequence of 6 color thresholds / segmentations, that need to be applied sequentially to the original image (.jpg in RGB format). Following each threshold, the image is duplicated and the subsequent threshold is applied to the duplicated image

1 - YUV: Y: min: 0, max: 255; U: min: 0, max: 255; V: min: 0, max: 175

2 - YUV: Y: min: 0, max: 255; U: min: 129, max: 255; V: min: 0, max: 255

3 - YUV: Y: min: 0, max: 255; U: min: 0, max: 255; V: min: 168, max: 255

4 - HSB: H: min: 0, max: 255; S: min: 0, max: 255; B: min: 0, max: 140

5 - YUV: Y: min: 0, max: 255; U: min: 0, max: 255; V: min: 160, max: 255

6 - YUV: Y: min: 0, max: 140; U: min: 0, max: 255; V: min: 0, max: 255

Once the last threshold has been applied (threshold 6), I then duplicate the image and using the macro I would like the name of this image to be saved as the original filename with “-TotalCellsBW” appended to the end. I then convert this image to an 8-bit black and white image, and save that.

Is this even possible? I feel like it should be

Many thanks for your help

If you require any further info, please let me know

The macro:

run("Duplicate...", "title=[Original Test Image-1.JPG]");
run("Color Threshold...");
// Color Thresholder 1.48v
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
call("ij.plugin.frame.ColorThresholder.RGBtoYUV");
run("RGB Stack");
run("Convert Stack to Images");
selectWindow("Red");
rename("0");
selectWindow("Green");
rename("1");
selectWindow("Blue");
rename("2");
min[0]=0;
max[0]=255;
filter[0]="pass";
min[1]=0;
max[1]=255;
filter[1]="pass";
min[2]=0;
max[2]=175;
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-------------
setBackgroundColor(255, 255, 255);
run("Clear Outside");
run("Duplicate...", "title=[Original Test Image-2.JPG]");
// Color Thresholder 1.48v
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
call("ij.plugin.frame.ColorThresholder.RGBtoYUV");
run("RGB Stack");
run("Convert Stack to Images");
selectWindow("Red");
rename("0");
selectWindow("Green");
rename("1");
selectWindow("Blue");
rename("2");
min[0]=0;
max[0]=255;
filter[0]="pass";
min[1]=129;
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-------------
run("Clear", "slice");
run("Duplicate...", "title=[Original Test Image-3.JPG]");
// Color Thresholder 1.48v
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
call("ij.plugin.frame.ColorThresholder.RGBtoYUV");
run("RGB Stack");
run("Convert Stack to Images");
selectWindow("Red");
rename("0");
selectWindow("Green");
rename("1");
selectWindow("Blue");
rename("2");
min[0]=0;
max[0]=255;
filter[0]="pass";
min[1]=0;
max[1]=255;
filter[1]="pass";
min[2]=168;
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("Clear", "slice");
run("Duplicate...", "title=[Original Test Image-4.JPG]");
// Color Thresholder 1.48v
// 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]=0;
max[2]=140;
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("Clear", "slice");
run("Duplicate...", "title=[Original Test Image-5.JPG]");
// Color Thresholder 1.48v
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
call("ij.plugin.frame.ColorThresholder.RGBtoYUV");
run("RGB Stack");
run("Convert Stack to Images");
selectWindow("Red");
rename("0");
selectWindow("Green");
rename("1");
selectWindow("Blue");
rename("2");
min[0]=0;
max[0]=255;
filter[0]="pass";
min[1]=0;
max[1]=255;
filter[1]="pass";
min[2]=160;
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("Clear", "slice");
run("Duplicate...", "title=[Original Test Image-6.JPG]");
// Color Thresholder 1.48v
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
call("ij.plugin.frame.ColorThresholder.RGBtoYUV");
run("RGB Stack");
run("Convert Stack to Images");
selectWindow("Red");
rename("0");
selectWindow("Green");
rename("1");
selectWindow("Blue");
rename("2");
min[0]=0;
max[0]=140;
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-------------
run("Clear", "slice");
selectWindow("Original Test Image-6.JPG");
run("Duplicate...", "title=[Original Test Image-TotalCellsBW.JPG]");
run("8-bit");
setThreshold(255, 255);
run("Convert to Mask");
saveAs("Tiff", "C:\\Users\\Username1\\Documents\\MACRO TEST\\Original Test Image - Cells BLACK - No Cells WHITE.tif");

Original Image:

Image After Manual Processing (Desired):

Macro Output Image (Undesired):

1 Like

Solved. Thanks to G. Landini’s Threshold_Colour plugin (from: http://www.mecourse.com/landinig/software/software.html).

Macro works more or less as should, but only able to handle around 5-7 images at a time until the allocated memory (6078 MB) becomes insufficient. I think this is partly due to this plugin’s windows not closing once the threshold / segmentation has been applied (with each new image processed that’s another 6 windows opened). I’ve tried to insert code to close them once the threshold has been applied but unfortunately the code seems to close all active windows and then prematurely abort the macro.

For all intents and purposes it works, although my code is probably far from efficient