Macro writing - help with segmentation of DAB staining at different intensities

macro
Tags: #<Tag:0x00007fb882cf5700>

#1

Dear all,

I am new to macro writing and am hoping someone can help. I need a macro that will batch process all images in a folder so that an area is measured at 4 different saturation parameters. Basically:

  1. Image>adjust>colour threshold: Hue 0-100, Saturation 0-30, Brightness default > analyse particles (size 0-infinity, circularity 0-1, outlines; summarize)

  2. for the same image, repeat 3 times with different saturation parameters, i.e. 30-60, 60-90, and 90-255 with analyze particles for each parameter to measure the area with low, medium, moderate, and high intensity.

I have used macro recording, but I don’t know how to batch process from a folder, and unedited it runs analyse particles 4 times with the same parameters.

I have pasted below what I have so far, can anyone help please?

B.w.

Katrin

run("Set Measurements...", "area mean limit display redirect=None decimal=3");
run("Color Threshold...");
// Color Thresholder 2.0.0-rc-43/1.51k
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=0;
max[0]=100;
filter[0]="pass";
min[1]=0;
max[1]=30;
filter[1]="pass";
run("Analyze Particles...", "summarize");
run("Color Threshold...");
// Color Thresholder 2.0.0-rc-43/1.51k
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=0;
max[0]=100;
filter[0]="pass";
min[1]=30;
max[1]=60;
filter[1]="pass";
run("Analyze Particles...", "summarize");
run("Color Threshold...");
// Color Thresholder 2.0.0-rc-43/1.51k
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=0;
max[0]=100;
filter[0]="pass";
min[1]=60;
max[1]=90;
filter[1]="pass";
run("Analyze Particles...", "summarize");
run("Color Threshold...");
// Color Thresholder 2.0.0-rc-43/1.51k
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=0;
max[0]=100;
filter[0]="pass";
min[1]=90;
max[1]=255;
filter[1]="pass";
run("Analyze Particles...", "summarize");

#2

My Fiji version (2.0.0-rc-59/1.51k) won’t run this code properly – but I suspect that after each analysis step, you might need to close the thresholded image and re-select the original image.

After you get it working for one image you can try to use the Process > Batch > Macro command – the easiest way to run a macro on a whole folder of images.

Worst case, you could write 3 macros, one for each set of parameters, and run each one on the whole folder using the Batch Process command.

Hope this helps.


#3

Hi @Katrin -

@tswayne gave some great advice … If you really want it all in a single macro code - you’d have to run some sort of loop where you incrementally increase your min[1] and max[1] values by 30 ( though when you hit your 4th time in the loop, you’d want to set your max[1]=255 ). You can read more about loops and whatnot here.

Also for batch processing… what @tswayne proposed is the easiest method - but you can use the following script (just inserting your own macro code where indicated):

// @File(label = "Input directory", style = "directory") input
// @File(label = "Output directory", style = "directory") output
// @String(label = "File suffix", value = ".tif") suffix

processFolder(input); // calls the function below

// function to scan folders/subfolders/files to find files with correct suffix
// you can change the suffix above according to the type of image file you want to process
function processFolder(input) {
	list = getFileList(input);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + list[i]))
			processFolder("" + input + list[i]); 
		if(endsWith(list[i], suffix))
			yourMacroCode(input, output, list[i]); // calls your macro code below
	}
}

// this function runs your inserted macro code (you can rename the function)
function yourMacroCode(input, output, file) {
        // ADD YOUR MACRO CODE HERE TO DO PRE-PROCESSING
               // including thresholding, ROI designation, etc.
        // INSERT YOUR MACRO CODE HERE TO DO THE MITOCHONDRIAL MORPHOLOGY
	// Leave these print statements until things work, then remove them.
	print("Processing: " + input + file);
	print("Saving to: " + output);
}

Hope this also helps.

eta :slight_smile:


Macro problems: paste results table to excel file; run loop that closes image, opens next image, and repeats macro commands
#4

Dear @tswayne and @etarena,

thank you very much for your help and suggestions! I now have 4 separate macros for the different saturation parameters that I run with Process>Batch>Macro. It works ok, but I would like Brightness in Threshold Color to remain default for each image. I am currently running the macro with a worked out average parameter for Brightness, which is only second best! Do you know how I could do that?

    run("Set Measurements...", "area mean limit display redirect=None decimal=3");
    run("Color Threshold...");
    // Color Thresholder 2.0.0-rc-43/1.51k
    // 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]=100;
    filter[0]="pass";
    min[1]=0;
    max[1]=30;
    filter[1]="pass";
    min[2]=0;
    max[2]=194;
    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("Analyze Particles...", "summarize");

B.w.

Katrin


#5

Hi @Katrin,

It sounds like you want the brightness to be auto-thresholded using the Default method, for each image, but use specific values for hue and saturation thresholds – is that right?

If so, you will need to change the assignment of min[2] and max[2].
One way to do this is below. Hope it helps!

run("Set Measurements...", "area mean limit display redirect=None decimal=3");
run("Color Threshold...");
// Color Thresholder 2.0.0-rc-43/1.51k
// 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]=100;
filter[0]="pass";
min[1]=0;
max[1]=30;
filter[1]="pass";

// optional specific assignment of Brightness threshold
// min[2]=0; 
// max[2]=194;

// automatic selection of Brightness threshold for each image
selectWindow("2");
resetThreshold();
setAutoThreshold("Default");  // use "Default dark" if image has a dark background
getThreshold(min[2],max[2]); // gets the values produced by the automatic algorithm
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("Analyze Particles...", "summarize");

#6

Automatic brightness is exactly what I wanted, and your code works perfectly! Thank you very much!

Katrin