// Process images macro by Christophe Leterrier // v1.0 12-11-2015 // // Takes a folder containing tif images // Process them with background subtraction, unsharp max, gamma, contrast enhancement macro "Process_Images" { //*************** Initialization *************** // Rolling ball diameter (in px) for the optionnal background subtraction // Default values for the Options Panel BG_DEF = true; BG_V_DEF = 105; UM_DEF = true; UM_V_DEF = 0.3; GA_DEF = false; GA_V_DEF = 0.8; GA_S_DEF = "-C=2"; EC_DEF = true; EC_V_DEF = 0.01; T8B_DEF = true; SC_DEF = false; SC_V_DEF = 0.5; SAVE_DEF = "In a 'Processed' folder next to the source folder"; SAVE_ARRAY = newArray("In the source folder", "In a 'Processed' subfolder of the source folder", "In a 'Processed' folder next to the source folder", "In a 'Processed' folder with custom location"); // Get the folder name INPUT_DIR = getDirectory("Select the input stacks directory"); print("\n\n\n*** Process_Images Log ***"); print(""); print("INPUT_DIR :"+INPUT_DIR); //*************** Dialog *************** // Creation of the dialog box Dialog.create("Process Images Options"); Dialog.addCheckbox("Background subtraction", BG_DEF); Dialog.addNumber("Rolling ball diameter", BG_V_DEF); Dialog.addCheckbox("Gamma", GA_DEF); Dialog.addNumber("Gamma value", GA_V_DEF); Dialog.addString("Gamma only for", GA_S_DEF); Dialog.addCheckbox("Scale", SC_DEF); Dialog.addNumber("Scale ratio", SC_V_DEF); Dialog.addCheckbox("Unsharp mask", UM_DEF); Dialog.addNumber("Unsharp ratio", UM_V_DEF); Dialog.addCheckbox("Enhance contrast", EC_DEF); Dialog.addNumber("Saturated pixels ratio", EC_V_DEF); Dialog.addCheckbox("Convert to 8-bit", T8B_DEF); Dialog.addChoice("Save Images", SAVE_ARRAY, SAVE_DEF); Dialog.show(); // Feeding variables from dialog choices BG = Dialog.getCheckbox(); BG_V = Dialog.getNumber(); GA = Dialog.getCheckbox(); GA_V = Dialog.getNumber(); GA_S = Dialog.getString(); SC = Dialog.getCheckbox(); SC_V = Dialog.getNumber(); UM = Dialog.getCheckbox(); UM_V = Dialog.getNumber(); EC = Dialog.getCheckbox(); EC_V = Dialog.getNumber(); T8B = Dialog.getCheckbox(); SAVE_TYPE = Dialog.getChoice(); // Get all file names ALL_NAMES = getFileList(INPUT_DIR); Array.sort(ALL_NAMES); ALL_EXT = newArray(ALL_NAMES.length); // Create extensions array for (i = 0; i < ALL_NAMES.length; i++) { // print(ALL_NAMES[i]); ALL_NAMES_PARTS = getFileExtension(ALL_NAMES[i]); ALL_EXT[i] = ALL_NAMES_PARTS[1]; } //*************** Preapre processing *************** setBatchMode(true); // Create the output folder OUTPUT_DIR = "Void"; if (SAVE_TYPE == "In the source folder") { OUTPUT_DIR = INPUT_DIR; } if (SAVE_TYPE == "In a 'Processed' subfolder of the source folder") { OUTPUT_DIR = INPUT_DIR + "Processed" + File.separator; if (File.isDirectory(OUTPUT_DIR) == false) { File.makeDirectory(OUTPUT_DIR); } } if (SAVE_TYPE == "In a 'Processed' folder next to the source folder") { INPUT_NAME = File.getName(INPUT_DIR); if (indexOf(INPUT_NAME, "Extracted") > 0) { ROOT_NAME = substring(INPUT_NAME, 0, lengthOf(INPUT_NAME)-10); } else { ROOT_NAME = INPUT_NAME; } OUTPUT_DIR = File.getParent(INPUT_DIR) + File.separator + ROOT_NAME + " Processed" + File.separator; if (File.isDirectory(OUTPUT_DIR) == false) { File.makeDirectory(OUTPUT_DIR); } } if (SAVE_TYPE == "In a 'Processed' folder with custom location") { OUTPUT_DIR = getDirectory("Choose the save folder"); INPUT_NAME = File.getName(INPUT_DIR); if (indexOf(INPUT_NAME, "Extracted") > 0) { ROOT_NAME = substring(INPUT_NAME, 0, lengthOf(INPUT_NAME)-10); } else { ROOT_NAME = INPUT_NAME; } OUTPUT_DIR = OUTPUT_DIR + File.separator + ROOT_NAME + " Processed" + File.separator; if (File.isDirectory(OUTPUT_DIR) == false) { File.makeDirectory(OUTPUT_DIR); } } OUTPUT_PARENT_DIR=File.getParent(OUTPUT_DIR); print("OUTPUT_DIR: "+OUTPUT_DIR); print("OUTPUT_PARENT_DIR: "+OUTPUT_PARENT_DIR); //*************** Processing *************** // Loop on all .tif extensions for (n=0; n -1) { run("Gamma...", "value=" + GA_V + " stack"); } // Scale if (SC == true) { ID1 = getImageID; LX = getWidth(); LY = getHeight(); run("Scale...", "x=" + SC_V + " y=" + SC_V + " width=" + round(LX * SC_V) + " height=" + round(LY * SC_V) + " interpolation=Bicubic average create"); ID2 = getImageID; selectImage(ID1); close(); selectImage(ID2); } // Unsharp mask if (UM == true) { run("Unsharp Mask...", "radius=1 mask=" + UM_V + " stack"); } // Enhance contrast if (EC == true) { run("Enhance Contrast...", "saturated=" + EC_V + " normalize process_all"); } // Convert to 8-bit if (T8B == true) { run("8-bit"); } // Create output file path and save the output image OUTPUT_PATH = OUTPUT_DIR + substring(FILE_NAME, 0, lengthOf(FILE_NAME) - 8) + substring(FILE_NAME, lengthOf(FILE_NAME) - 8, lengthOf(FILE_NAME)); save(OUTPUT_PATH); print("OUTPUT_PATH: "+OUTPUT_PATH); // Close output image if checked close(); }// end of IF loop on tif extensions }// end of FOR loop on all files setBatchMode("exit and display"); print(""); print("*** Process_Images end ***"); showStatus("Process Images finished"); } //*************** Functions *************** function getFileExtension(Name) { nameparts = split(Name, "."); shortname = nameparts[0]; if (nameparts.length > 2) { for (k = 1; k < nameparts.length - 1; k++) { shortname += "." + nameparts[k]; } } extname = "." + nameparts[nameparts.length - 1]; namearray = newArray(shortname, extname); return namearray; }