// CustomTabStatFromResults // // This macro set demonstrates how to create, modify, open // and save custom results tables. It also shows one way to // exploit values obtained by "Analyse Particles..." and // "Measure" ImageJ menus into the so-called Results table: // It makes some basic statistics (summ, mean) like // "Summary" ImageJ window and presents them into an ImageJ // custom table. It contains saving functions as Excel type // (.xls), a tab last line ereaser function and it opens // saved files as Excel type (.xls) as custom table. // // "Get Demo Results" runs a demo macro which returns some // measurements into the ImageJ Results table to test the macro. // // "Get stats from image analysis Results Table" returns from an // ImageJ Results table: label, total area of objects, mean area // of objects and mean perimeters of objects (lable is available // in Results table if the "Display Label" option is set in the // "Analyse/Setmeasurements... ImageJ submenu) // // "Get stats from image analysis Results Table" makes stats // from any Results table, and adds it into a custom table. // // "Remove last table line" ereases the last line of a // table made or opened by the macro set. Can be undo // by "Undo remove last table line [z]" // // "Save a "Stat Results Table" table window as Excel type file" // saves an opened or builted tab by the macro set, as a ".xls" file. // (if the analysed image is recorded onto the HD the tab file will // be saved into the repertory containing the image) // "Open a "Stat Results Table" table file saved as Excel type file" // opens any file from ImageJ saved tab as ".xls". // // Author : Gilles Carpentier // Faculte des Sciences et Technologies, // Universite Paris 12 Val de Marne, France. // tab variables var windowTabName="Stat Results Table",nameOfStatTab="["+windowTabName+"]",label="",undoErease=""; // stat variables var nbPerim=0,TheTotalArea=0,meanPerim=0,meanObject=0; var xx = requires138x(); // check version at install time function requires138x() {requires("1.38x"); return 0;} macro "Get Demo Results" { // from DrawRandomDots author: Wayne Rasband // http://rsb.info.nih.gov/ij/macros/DrawRandomDots.txt setForegroundColor(0, 0, 0); windowTabName="Stat Results Table";nameOfStatTab="["+windowTabName+"]"; setBatchMode(true); for (a=0; a<10; a++) { imageName= "Random Dot-"+(a+1); newImage("im", "8-bit White", 256, 256, 1); rename (imageName); dotSize = 25; width = getWidth(); height = getHeight(); for (i=0; i<25; i++) { x = random()*width-dotSize/2; y = random()*height-dotSize/2; makeOval(x, y, dotSize, dotSize); run("Fill"); } run("Select None"); run("Make Binary"); run("Watershed"); run("Set Measurements...", "area perimeter display redirect=None decimal=2"); run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing display clear include record "); close (); makeStatFromResults (); if (a==0) TabWindow (0); else TabWindow (1); } } macro "Get stats from image analysis Results Table" { if (! isOpen("Results")) {exit ("No Results table")} undoErease = ""; windowTabName="Stat Results Table";nameOfStatTab="["+windowTabName+"]"; printChoice = "Add to the current table"; if (isOpen(windowTabName)) { Dialog.create("Print table user choice:"); Dialog.addMessage("Print the stats into a new tab or add them to the current \"Stat from Results\" table?"); Dialog.addChoice("Choice:", newArray("Add to the current table","New table"),printChoice); Dialog.show(); printChoice=Dialog.getChoice(); if (printChoice == "Add to the current table") {addLine=1;} else {addLine=0;} } else {addLine=0;} makeStatFromResults (); TabWindow (addLine); } macro "-" {} macro "Remove last table line" {rmLastLine ();} macro "Undo remove last table line [z]" {undormLastLine ();} macro "-" {} macro "Save a \"Stat Results Table\" table window as Excel type file" { if (! isOpen(windowTabName)) {exit ("No Stat Results Table")} saveChoicePath="Ask for the file destination"; imageFolder =getDirectory("image"); if (imageFolder != "") { Dialog.create("Save user choice:"); Dialog.addMessage("Save the tab file into the same repertory of the active image\nor ask the path throught a dialog box?"); Dialog.addChoice("Choice:", newArray("Save to the image path","Ask for the file destination"),saveChoicePath); Dialog.show(); saveChoicePath=Dialog.getChoice(); } // save the tab window asking the path to the user if (saveChoicePath=="Ask for the file destination") { imageFolder =""; saveTab (imageFolder); } // save the tab window to the path to the active image if (saveChoicePath=="Save to the image path") { saveTab (imageFolder); } } macro "Open a \"Stat Results Table\" table file saved as Excel type file" { requires("1.39d"); path = File.openDialog("Select a File"); name = File.getName(path); if (endsWith(name, ".xls")) {name=substring(name, 0, indexOf(name, ".xls"));} else {exit ("This file doesn't seam to be an Excel type file.");} openTab (path,name); } // --------------- tab functions ---------------// // function making stats from ImageJ Results Table values function makeStatFromResults () { sumObject=0;sumPerim=0;TheTotalArea=0;meanObject=0;meanPerim=0; // extraction from data from the Result Table: if (nResults() > 0 && isOpen("Results")) { if (columnLabelList ("Label") >=0) {label=getResultLabel (0);} else {label="unknown";} if (columnLabelList ("Area") >= 0) {sumObject=sumColumnTab ("Area"); } else {exit ("No \"Area\" measurements in this Result Table");} if (columnLabelList ("Perim.") >= 0) {sumPerim=sumColumnTab ("Perim."); } else {exit ("No \"Perim\.\" measurements in this Result Table");} } else {exit ("No result table");} // Stat calculations: TheTotalArea = sumObject; if (nResults() != 0) meanObject = TheTotalArea/nResults(); if (nResults() != 0) meanPerim=sumPerim/nResults(); } // function getting the sum of the value from the column name contained in kind function sumColumnTab (kind) { sum=0; if (columnLabelList (kind) >=0) { for (a=0; a 0 && isOpen("Results")) { selectWindow("Results"); results = getInfo(); lines = split(results, "\n"); headings = lines[0]; titlesofcolumns = split(headings, ",\t"); for (a=0; a 0) { print(nameOfStatTab, "\\Clear"); resteLines=""; for (i=1; i < (linesInTab.length -1); i++) { resteLines=resteLines+linesInTab[i] +"\n"; } if (linesInTab.length > 2) print (nameOfStatTab,resteLines); if (linesInTab.length > 1) undoErease=linesInTab[linesInTab.length-1]; } } } // function restoring the last ereased line in the table function undormLastLine () { if (undoErease != "") print(nameOfStatTab,undoErease); undoErease=""; } // saving a tab as excel type file function saveTab (path) { if (isOpen(windowTabName)) { if (path == "") { selectWindow (windowTabName); run("Input/Output...", "jpeg=75 gif=-1 file=.xls"); saveAs("Text"); } if (path != "") { selectWindow(windowTabName); saveAs("Text", path+windowTabName+".xls"); } } } function openTab (path,name) { undoErease="";windowTabName=name; lines=split(File.openAsString(path), "\n"); if (lines.length < 2) { exit ("This file doesn't seam to contain data");} headings = lines[0]; titlesOfColumns = split(headings, ",\t"); nameOfStatTab="["+windowTabName+"]"; if (isOpen(windowTabName)) {selectWindow(windowTabName) ;run("Close");} firstLine=""; for (i=0; i < (titlesOfColumns.length ); i++) { firstLine=firstLine+ titlesOfColumns [i]; if ( i < (titlesOfColumns.length )-1) {firstLine=firstLine+ "\t";} } toPrint=""; for (i=1; i < (lines.length ); i++) { toPrint=toPrint+lines[i]+"\n"; } run("New... ", "name="+nameOfStatTab+" type=Table"); print(nameOfStatTab, "\\Headings:"+firstLine+""); print(nameOfStatTab,toPrint); }