help! macro no longer works if setBatchMode = true
Posted by Crowell Elizabeth on Nov 16, 2010; 12:54pm
URL: http://imagej.273.s1.nabble.com/help-macro-no-longer-works-if-setBatchMode-true-tp3686407.html
Hello ImageJ community,
I have a macro that works perfectly when batch mode is set to false.
However, as soon as I set batch mode to true, the macro no longer
functions (attached). I have searched all the list archives, and I
cannot find a solution for this problem. I have tried debugging the
code to localize the problem:
- When setBatchMode=false, the variable length1 in the function GetROIs
is read correctly, and corresponds to the number of ROIs in the manager.
- When setBatchMode=true, the variable length1 is read only for the
first file processed, afterwards it is always read as equalling zero,
although I can see the ROI manager being filled.
- When setBatchMode=true, if I comment out the call to the function
"MeasureROIs", the variable length1 is properly read, and everything
works correctly (although of course I do not get my output, which comes
from MeasureROIs).
I cannot find what my error might be. I have reproduced the problem on
two different Macs. A simpler code ("ImageJTest.txt", attached) used on
the blobs sample image works fine in batch mode.
I am quite lost and cannot find what is causing the problem. Would
anyone have a moment to help me? I can post a sample image set for
testing if it is needed.
Thanks in advance,
Elizabeth
--
Elizabeth CROWELL
----------------------------------------------------------------------
Membrane Traffic and Cell Division Research Group
Institut Pasteur
28 rue du Dr Roux
75015 PARIS, France
Tel : 01.44.38.94.07
Fax : 01.45.68.89.54
----------------------------------------------------------------------
// "BatchMeasureCilia"
// There is an incomprehensible behavior of analyze particles in the function
// "MeasureROIs". When j = 0 (the first ROI selection in the manager), analyze
// particles measures all the ROIs in the manager and then the thresholded regions
// in the Cy3 image. This occurs even if the ROI manager is reset before running
// analyze particles! Even more strange, the ROIs are not measured when j > 0.
// The only way I found around the problem is to delete the unwanted measurements
// in the results table before saving it.
// The ROIManager list length is not read if run in Batch mode!
// This macro batch processes all the files in a folder and any
// subfolders in that folder. It opens all the FITC images and segments them
// after removing noise using a Gaussian blur subtraction. Then it
// opens the corresponding Cy3 image (with the same number), thresholds, and
// measures the intensity in each ROI.
// The DAPIROIs are saved in the same folder as the images. The results
// of the measurements are saved in a new folder called
// condition_ROImeasurements, one level up from the images. Each results
// file is named after the image used for measurement (FITC000X).
// If no ROIs were selected in an image (due to automatic thresholding
// problems, for example) the macro prints out the name of the image
// in the log and moves on to the next image.
// The current settings are for selecting Ninein staining imaged at 60X.
// The macro does not work if there are images in the source directory.
// The images must be organized into subfolders (for ex. series1,
// series2, etc) and cannot be present at the same level as the subfolders.
// Modified from "BatchMeasureROIs" macro by E. Crowell 9/11/2010.
requires("1.33s");
setBatchMode(true);
dir = getDirectory("Please choose a source directory.");
GetFiles(dir);
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")) {
if (endsWith(list[i], "FITCmax.tif")) {
path = dir+list[i];
print("GetFiles list "+path);
GetROIs(path);
}
}
}
}
}
function GetROIs(path) {
open(path);
print("GetROIs opened file "+path);
rename("filter1");
run("Duplicate...", "title=filter2");
// the sigma parameters might need to be adjusted here.
selectImage("filter1");
run("Gaussian Blur...", "sigma=5");
run("Variance...", "radius=3");
selectImage("filter2");
run("Gaussian Blur...", "sigma=25");
imageCalculator("Subtract create", "filter1","filter2");
rename("Difference");
selectImage("filter1");
close();
selectImage("filter2");
close();
selectImage("Difference");
setAutoThreshold("Default dark");
// run("OtsuThresholding 16Bit");
run("Convert to Mask", "calculate");
run("Fill Holes", "slice");
run("Watershed", "slice");
// the filter size will need to be adjusted here.
run("Analyze Particles...", "size=50-1000 circularity=0.50-1.00 show=Nothing add");
length1 = roiManager("count");
print("Result of GetROIs "+length1);
if (length1>0) {
roiManager("Save", substring(path,0,lastIndexOf(path, "."))+"_ROI.zip");
selectImage("Difference");
close();
MeasureROIs(path, length1);
roiManager("reset");
} else {
selectImage("Difference");
close();
print("No ROIs were selected in image "+path);
}
}
function MeasureROIs(path, length1) {
corresp = replace(path,"FITC","Cy3");
open(corresp);
print("Corresponding file path in MeasureROIs "+corresp);
rename("filter1");
run("Duplicate...", "title=filter2");
selectImage("filter1");
run("Gaussian Blur...", "sigma=2");
selectImage("filter2");
run("Gaussian Blur...", "sigma=25");
imageCalculator("Subtract create", "filter1","filter2");
rename("Diffimage");
selectImage("filter1");
close();
selectImage("filter2");
close();
parentdir = File.getParent(corresp);
dest = parentdir+"_ROImeasurements";
File.makeDirectory(dest);
number = substring(corresp,lastIndexOf(corresp,"/"),lastIndexOf(corresp,"Cy3max"));
print("Length of ROIManager list in MeasureROIs "+length1);
for(j=0;j<length1;j++){
roiManager("Select", j);
run("Enlarge...", "enlarge=70");
run("Duplicate...", "title=dup");
selectImage("dup");
setAutoThreshold("Default dark");
// run("OtsuThresholding 16Bit");
run("Analyze Particles...", "size=10-2000 circularity=0.00-1.00 show=[Nothing]");
if (j==0) {
IJ.deleteRows(0,length1 - 1);
}
saveAs("Measurements", dest+number+"_"+j);
run("Clear Results");
selectImage("dup");
close();
}
if (isOpen("Results")) {
selectWindow("Results");
run("Close");
}
if (isOpen("Diffimage")) {
selectImage("Diffimage");
close();
}
}
setBatchMode(true);
run("Blobs (25K)");
rename("filter1");
run("Duplicate...", "title=filter2");
// the sigma parameters might need to be adjusted here.
selectImage("filter1");
run("Gaussian Blur...", "sigma=5");
run("Variance...", "radius=3");
selectImage("filter2");
run("Gaussian Blur...", "sigma=25");
imageCalculator("Subtract create", "filter1","filter2");
rename("Difference");
selectImage("filter1");
close();
selectImage("filter2");
close();
selectImage("Difference");
run("OtsuThresholding 8Bit");
run("Convert to Mask");
run("Fill Holes");
run("Watershed");
run("Analyze Particles...", "size=0-10000 circularity=0.00-1.00 show=Nothing add");
length1 = roiManager("count");
print("length of ROIManager list "+length1);