run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel"); //sets the scaling unit of the image to units -> necessary for size limit of ROIs (assumption: images are 600dpi)
outputFolder = getDirectory("Choose output folder");
name = getTitle;
dotIndex = indexOf(name, ".");
filename = substring(name, 0, dotIndex);
path = outputFolder+filename;
run("Duplicate...", "title=original"); //generates a copy of the image for the color threshold
run("Duplicate...", "title=mask"); //generates a copy of the image for the ROI selection
run("8-bit"); //converts the ROI selection image to 8-bit
setAutoThreshold("Default dark"); //sets threshold for conversion to mask -> leaves will be black and background white
setThreshold(0, 200);
setOption("BlackBackground", false);
run("Convert to Mask"); //image is converted to mask
waitForUser("Fused leaves?", "1) Separate fused leaves by drawing a white line between them using the paintbrush tool \n2) You can check the success using the magic wand  \n3) Make sure nothing is selected  \n4) Klick OK");run("ROI Manager..."); //asks used to remove remaining unwanted areas manually
run("Set Measurements...", "area integrated add redirect=None decimal=3"); //defines measurement parameters
run("Analyze Particles...", "size=1000-Infinity circularity=0.00-1.00 show=Nothing display clear include add in_situ"); //searches for ROI with a size of 1000-infinity (may need adjustment when images are not 600dpi) and drwas ROI on the image
run("Flatten");
saveAs("Jpeg" , path+"-mask.jpg"); //saves the mask image
saveAs("results", path+"-mask-results.txt"); //saves the selected results like areas of each ROI
selectWindow("Results"); run("Close");
selectWindow("original"); //selects the duplicate of the original window
run("Duplicate...", "title=green"); //generates another duplicate and renames it to green
run("Threshold Colour"); //runs the color threshold function
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]=50; //lower limit for threshold is 50
max[0]=120; //upper limit for threshold is 120
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"); //selects the color threshold mask
rename(a); //renames the picture
run ("8-bit"); //converts the image to 8 bit
roiManager("Select All");
roiManager("Measure"); //measures the area and other selected features of the ROI
selectWindow("Results");
saveAs("results", path+"-green-results.txt"); //saves the results as a txt file
run("Close");
selectWindow("green");
saveAs("Jpeg" , path+"-green.jpg"); //saves the mask of the green area as an image
selectWindow("Threshold Colour");
run("Close");
selectWindow("ROI Manager");
run("Close");
close(); //closing of remaining windows
close(); 
close();
close();
close();
