//ImageJ setup run("Set Measurements...", "area centroid display redirect=None decimal=3"); run("Input/Output...", "jpeg=85 gif=-1 file=.csv use_file copy_column copy_row save_column save_row"); // Ask user for source and destination directories dir = getDirectory("Source Directory"); dest1 = getDirectory("Destination Directory RN threshold"); dest2 = getDirectory("Destination Directory absolute RN threshold"); dest3 = getDirectory("Destination Directory excel file"); rname= "Untitled"; Dialog.create("Name of results file"); Dialog.addString("Results name:", rname); Dialog.show(); rname= Dialog.getString(); list = getFileList(dir); setBatchMode(true); //images that macro has difficulties with will be listed in dialogue box at end of macro problems = " "; //Go through files in source directory in turn for (i=0; i80% of total area as fail if (i==0) { run("Select All"); run("Measure"); imagesize = getResult("Area"); fail = 0.8*imagesize; size = sqrt(imagesize); samecellthreshold = 0.05*size; run("Clear Results"); } // RN threshold 'maxima less x' x=55 run("Find Maxima...", "noise=55 output=[Maxima Within Tolerance]"); run("Close-"); rename(replace(getTitle,"Maxima","RN")); run("Analyze Particles...", "size=3000-Infinity pixel circularity=0.00-1.00 show=Outlines display exclude"); a = 1; sbreak = 0; if(nResults>=1){ while (startsWith(getResultLabel(nResults-a), name) && endsWith(getResultLabel(nResults-a), "RN") && sbreak != 1){ setResult("ImageNo.", nResults-a, nResults-a+1); if (nResults > a){ a = a+1; } else { sbreak = sbreak + 1; } } } saveAs("Tiff", dest1+name); close(); close(); //If no RN ROI captured for image name, add row with zeros if (nResults==0) { setResult("Label", nResults, name + " * no RN"); updateResults; setResult("Area", nResults-1, 0); setResult("X", nResults-1, 0); setResult("Y", nResults-1, 0); setResult("ImageNo.", nResults-1, 0); problems = problems + "\n" + name; updateResults(); } if (nResults>0) { if(!startsWith(getResultLabel(nResults-1), name)){ setResult("Label", nResults, name + " * no RN"); updateResults; setResult("Area", nResults-1, 0); setResult("X", nResults-1, 0); setResult("Y", nResults-1, 0); setResult("ImageNo.", nResults-1, 0); problems = problems + "\n" + name; updateResults; } } setAutoThreshold("Default dark"); //RN threshold absolute pixel intensity threshold=220 setThreshold(220.0000, 255.0000); setOption("BlackBackground", false); run("Convert to Mask"); run("Close-"); rename(name + " absolute"); run("Analyze Particles...", "size=3000-Infinity pixel circularity=0.00-1.00 show=Outlines display exclude"); //setResult("ImageNo.", nResults-1, nResults); b = 1; ubreak = 0; while (startsWith(getResultLabel(nResults-b), name) && endsWith(getResultLabel(nResults-b), "absolute")&& ubreak != 1){ setResult("ImageNo.", nResults-b, nResults-b+1); if (nResults > b){ b = b+1; } else { ubreak = ubreak + 1; } } saveAs("Tiff", dest2+name); close(); //If no absolute RN ROI captured lastrow = getResultLabel(nResults-1); if (!endsWith(lastrow, "absolute")) { setResult("Absolute RN Area", nResults-1, "* no abs RN"); if (!endsWith(getResultLabel(nResults-1), "no RN")){ problems = problems + "\n" + name; } updateResults(); } //Count number of RN and absolute RN measurements up = 1; absRNs = 0; RNs = 0; vbreak = 0; while (startsWith(getResultLabel(nResults-up), name) && vbreak !=1){ if (endsWith(getResultLabel(nResults-up), "absolute")){ absRNs = absRNs +1; if (nResults >up){ up = up +1; } } else { if (endsWith(getResultLabel(nResults-up), "RN")){ RNs = RNs +1; if (nResults>up){ up = up +1; } if (up == nResults){ vbreak = vbreak+1; } } else { problems = problems + "\n" + name; vbreak = vbreak + 1; } } } //Sample absolute RN areas, if >= 80% of total area, delete absolute RN area rows and add fail to all RN rows l=1; imagefail = 0; wbreak = 0; while (startsWith(getResultLabel(nResults-l), name) && endsWith(getResultLabel(nResults-l), "absolute") && wbreak!=1){ query = getResult("Area", nResults-l); if (query>=fail){ imagefail = imagefail+1; wbreak = wbreak + 1; } if (nResults>l){ l = l+1; } else { if (l == nResults){ wbreak = wbreak+1; } } } if (imagefail == 1){ m=1; xbreak = 0; if (RNs >1){ problems = problems + "\n" + name; } while (startsWith(getResultLabel(nResults-m), name) && xbreak!=1){ if (endsWith(getResultLabel(nResults-m), "absolute")){ IJ.deleteRows(nResults-m, nResults-m); } if (endsWith(getResultLabel(nResults-m), "RN")){ setResult("Absolute RN Area",nResults-m, "fail"); updateResults; } if (nResults>m){ m = m+1; } else { if (m == nResults){ xbreak = xbreak+1; } } updateResults(); } // Move correct absolute RN to correlating RN measurement for images with no 'fails' } else { ybreak = 0; if(endsWith(getResultLabel(nResults-absRNs-RNs), "no RN")){ ybreak = 1; } if (endsWith(getResult("Absolute RN Area",nResults-1), "no abs RN")) { ybreak = 1; } lastrow = getResultLabel(nResults-1); b = RNs; while (startsWith(lastrow, name) && endsWith(lastrow, "absolute") && ybreak!=1) { absRN = getResult("Area", nResults-absRNs); absRNX = getResult("X", nResults-absRNs); absRNY = getResult("Y", nResults-absRNs); absRNNo = getResult("ImageNo.", nResults-absRNs); // Start from top RN RNX = getResult("X", nResults-absRNs-b); RNY = getResult("Y", nResults-absRNs-b); queryx = abs(RNX - absRNX); queryy = abs(RNY - absRNY); // If query absRN and query RN are in the same position, join up results if (queryx <= samecellthreshold && queryy <= samecellthreshold) { setResult("Absolute RN Area",nResults-absRNs-b, absRN); setResult("Absolute RN X",nResults-absRNs-b, absRNX); setResult("Absolute RN Y",nResults-absRNs-b, absRNY); setResult("Abolute RN ImageNo.",nResults-absRNs-b, absRNNo); IJ.deleteRows(nResults-absRNs, nResults-absRNs); if (absRNs ==1 && RNs ==1){ ybreak = 1; } if (absRNs >1 && RNs >1){ absRNs = absRNs-1; RNs = RNs-1; b = RNs; } if (absRNs >1 && RNs ==1){ problems = problems + "\n" + name; ybreak = 1; } if (RNs >1 && absRNs ==1){ problems = problems + "\n" + name; ybreak = 1; } lastrow = getResultLabel(nResults-1); } if (queryx >= samecellthreshold || queryy >= samecellthreshold){ if (b>1) { b=b-1; } if (b==1) { problems = problems + "\n" + name; ybreak = 1; } } } } // Clean up selectWindow(name + " absolute"); close(); } saveAs("Results", dest3+"Results "+rname+".csv"); selectWindow("Results"); run("Close"); showMessage("Images needing manual curation", "Image files needing manual curation: " + "\n" + problems);