Set threshold limits on 1st image, run set threshold on rest of images in the folder

Hello,

I have a macro to run a threshold analysis on images.
I want to be able to set the threshold on the first image, then have the same set threshold run on the rest of the images in the folder.

The macro I currently have requires the threshold to be set for each image within the folder. This is because the run threshold code is written within the ‘for’ loop statement.

I’m not sure how to move the run threshold statement so that the user defined threshold is only set once at the start, then applied to all.

The macro is below:

macro "%area by Color Threshold [f2]" {

//Variables
FileName_ext = getInfo("image.filename");
FileName_nonum = replace(FileName_ext, "_(.?)(.?)\\.(.*)", "_");

//Perform macro for specified number of images
for (e=1; e<No_squares+1; e++) {

	//Selects the image in the appropriate order
	selectWindow(FileName_nonum + e + ".tif");
	
	//Color threshold to select DAB-only particles
	run("Color Threshold...");
		selectWindow("Threshold Color");
		waitForUser("Please adjust threshold.", "Adjust threshold according to antibody used.\nHue: 0-30 (DAB)\nSaturation: 0-255 (Adjust according to background)\nBrightness: Mean thresholding method.\nClick Filtered and Select after setting threshold.");
        
    //Create a binary image of the DAB-thresholded particles
	run("Create Mask");
		selectWindow("Mask");
	
	//Using ImageJ Measure function to determine %area coverage of thresholded particles
	run("Set Measurements...", "area_fraction limit redirect=None decimal=3");
	run("Measure");
		close("Mask");

}
		
//Brings Results window forward and saves it as a tab delimited text file in the folder containing the image
selectWindow(FileName_ext);
save_folder = getDirectory("image");
selectWindow("Results");
	saveAs("results", save_folder + "%area.xls");
	run("Close");

//Closes all windows in ImageJ
run("Close All");

//Open the saved results file in Excel to view
}

Hello Will,

If I understand your question: you want to set the threshold values for the first image in the series, and then you want these threshold values to be applied to all the remaining images?

So what you need is to do is option 1. save the numbers you input for the threshold or 2. get the numbers from the threshold adjuster after you applied threshold.

But that is the hard way to do it. I think the easiest is to open the images in the folder as a stack and apply the threshold to the whole stack. Use (File >> Import >> Image Sequence…) with the macro recorder running to find a line that works for you.

In my jython script it looks something like this, it opens all the images in a directory as a stack.


	overlay_stack = IJ.run("Image Sequence...", "open="+Destination_Directory+
			       " number=300 starting=0 increment=1 scale=300 file=.tif")

I hope this helps, or maybe I misunderstood your issue.

Edit: Oh, and to format code on this forum use the code button.

Best,

Sverre