//Select folder
dir = getDirectory("Select the XY01\T0001 folder");
//First, we retrieve the list of all of the photos in the T___ folder
photolist = getFileList(dir);
Array.sort(photolist);
//Next we retrieve the list of all of the T___ folders within XY01
par1 = File.getParent(dir);
Tlist = getFileList(par1);
Array.sort(Tlist);
for (j=0; j<photolist.length; j++) {	
for (i=0; i<Tlist.length; i++) {	
open(par1 + "\\" + Tlist[i], photolist[j])
//Stacking images
run("Images to Stack", "method=[Copy (center)] name=Stack title=[] use");
//Crop on the ROI
rename("channel1");
setTool("rectangle");
waitForUser("dessiner contour zone");
run("Crop");
//Thresholding
run("Auto Threshold", "method=Default stack use_stack_histogram");
//Remove background and smooth edges
run("Despeckle", "stack");
run("Erode", "stack");
run("Erode", "stack");
//Smooth the outliers
run("Invert", "stack");
run("Remove Outliers...", "radius=10 threshold=50 which=Dark stack");
run("Invert", "stack");
run("Duplicate...", "duplicate");
//Select root1
selectWindow("channel1");
setTool("rectangle");
waitForUser("Crop on root1");
run("Crop");
//Select root2
selectWindow("channel1-1");
setTool("rectangle");
waitForUser("Crop on root2");
run("Crop");
// Acquisition of the “Fret Distance”
selectWindow("channel1");
run("Analyze Particles...", "size=500-Infinity pixel show=Outlines display stack");
selectWindow("channel1-1");
run("Analyze Particles...", "size=500-Infinity pixel show=Outlines display stack");
//Output
selectWindow("Results");
saveAs("Measurements");
//Closing
close("\\Others");
run("Clear Results");
run("Close All");

}
}

