Hello list members,
I am trying to measure the intensity in thresholded regions of each slice of a stack. My macro works perfectly when batchmode=false. When batchmode=true, it seems to measure the same slice over and over again. In any case, the results are identical for every slice. This is the part of the code that seems to be causing the problem. for (i=0; i<=nSlices; i++) { if (nSlices>1) run("Set Slice...", "slice=" +i); setAutoThreshold("Minimum dark"); run("Create Selection"); run("Measure"); run("Select None"); } The complete code is below. Thank you in advance for any hints! Elizabeth Crowell // "BatchMeasureQuies" // // This macro batch processes all the files in a folder and any // subfolders in that folder. It opens all the DAPI images and selects the // nuclei. Then it // opens the corresponding FITC (or Cy3 or Cy5) image (with the same number) and // measures the intensity in each ROI. // The results are saved in the same folder as the images. // The current settings are for selecting mouse cell nuclei imaged at 10X. // Modified from "BatchMeasureNuclei" by E. Crowell 18/02/2013. requires("1.33s"); dir = getDirectory("Please choose a source directory."); setBatchMode(false); run("Colors...", "foreground=black background=black selection=yellow"); print("Number of nuclei significantly positive for Cy5 staining at 1s threshold and 2s threshold:"); GetFiles(dir); if (isOpen("Log")) { selectWindow("Log"); saveAs("Text", dir+"AnalysisResults.txt"); } function GetFiles(dir) { list = getFileList(dir); for (i=0; i<list.length; i++) { if (endsWith(list[i], "/")) GetFiles(""+dir+list[i]); else { if (endsWith(list[i], ".tif")) { for (i=0; i<list.length; i++) { if (endsWith(list[i], "DAPI.tif")) { path = dir+list[i]; GetROIs(path); } } } } } } function GetROIs(path) { open(path); rename("orig"); run("Duplicate...", "title=dup duplicate"); selectImage("dup"); run("Gaussian Blur...", "sigma=60 stack"); imageCalculator("Subtract create stack", "orig", "dup"); rename("DAPIimg"); selectImage("orig"); close(); selectImage("dup"); close(); run("8-bit"); run("Gaussian Blur...", "sigma=2 stack"); // The remove outliers step below does not work with small Axiovision images. // run("Remove Outliers...", "radius=10 threshold=0 which=Bright"); run("Square Root", "stack"); setAutoThreshold("Triangle dark"); // Do not use "calculate for each image," it detects lots of noise. run("Convert to Mask", " "); run("Watershed", "stack"); run("Analyze Particles...", "size=164-880 circularity=0.85-0.99 show=Nothing summarize add stack"); dlength = roiManager("count"); if (dlength>0) { // If at least 1 cell was detected, process the image. selectWindow("Summary of DAPIimg"); save(substring(path,0,lastIndexOf(path,"."))+"_cellcount.txt"); selectWindow("Summary of DAPIimg"); run("Close"); roiManager("Save", substring(path,0,lastIndexOf(path,"."))+"ROIs.zip"); selectImage("DAPIimg"); close(); run("Clear Results"); MeasureROIs(path); } else { selectImage("DAPIimg"); close(); } } function MeasureROIs(path) { corresp = replace(path,"_DAPI","_Cy5"); print("Processing image "+corresp); open(corresp); MeasImage = getImageID(); length = roiManager("count"); //Measure the ROIs and save. if (length>0) { for(j=0;j<length;j++){ roiManager("Select", j); roiManager("Measure"); } saveAs("Measurements", substring(corresp,0,lastIndexOf(corresp,"."))+"_meas.txt"); run("Clear Results"); MeasureBG(corresp); } } function MeasureBG(corresp) { // Fill all the nuclei. length = roiManager("count"); for(j=0;j<length;j++){ roiManager("Select", j); run("Fill", "slice"); } roiManager("reset"); run("Select None"); // Now measure the background signal outside the nuclei. for (i=0; i<=nSlices; i++) { if (nSlices>1) run("Set Slice...", "slice=" +i); setAutoThreshold("Minimum dark"); run("Create Selection"); run("Measure"); run("Select None"); } saveAs("Measurements", substring(corresp,0,lastIndexOf(corresp,"."))+"_BGmeas.txt"); MeasImage = getImageID(); selectImage(MeasImage); close(); // Compare the maximum intensity values in nuclei and in background. // Results table is open. Get the data and calculate statistics. Verified the code and it works. // The mean of the "Max" column: for (a=0; a<nResults(); a++) { sum=sum+getResult("Max",a); } mean=sum/nResults; // The sample standard deviation of the "Max" column: // Interestingly, the ^ symbol does not work for powers in ImageJ macro language. variance=0; for (a=0; a<nResults(); a++) { variance=variance+((getResult("Max",a)-mean)*(getResult("Max",a)-mean)); } sd=sqrt((1/(nResults-1))*variance); thres1=mean+sd; thres2=mean+2*sd; print("1s threshold: "+thres1); print("2s threshold: "+thres2); // Now close the old results table. run("Clear Results"); // Now open the Cy5 measurements and count how many nuclei have a max intensity greater than the thresholds. name=substring(corresp,0,lastIndexOf(corresp,"."))+"_meas.txt"; run("Results... ", "open=name"); print("Total number of nuclei detected: "+nResults); pos2s=0; pos1s=0; for (b=0; b<nResults(); b++) { mm = getResult("Max",b); if (mm > thres2) { pos2s++; pos1s++; } else { if (mm > thres1) { pos1s++; } } } print(corresp+" 1s "+pos1s); print(corresp+" 2s "+pos2s); run("Clear Results"); } -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |