Login  Register

Re: Threshold Colour - select/macro question

Posted by yorkh2 on Feb 19, 2015; 5:11pm
URL: http://imagej.273.s1.nabble.com/Threshold-Colour-select-macro-question-tp5000412p5011698.html

Hello,

 I am trying to run this as a macro to process a folder of images but the macro stops after only processing one.  This is the code I am using:

inputDir = getDirectory("Please select a directory for processing");
allFiles = getFileList(inputDir);


outputDir1 = getDirectory("Please select a directory for saving Color Threshold Images");
outputDir2 = getDirectory("Please select a directory for saving inverted 8bit Images");


setBatchMode(true);
for(i=0; i<allFiles.length; i++) {
        path = inputDir + allFiles[i];
        if(!endsWith(path, "/")) open(path);
        if(nImages>=1) {
                if(endsWith(path, "f")) {
                        t = getTitle();
                        s = lastIndexOf(t, '.');
                        t = substring(t, 0, s);
                        t = replace(t, " ", "_");
                       
                        t1 = t + "-ColorThreshold";
                        t2 = t + "-8bit";
                       
                        run("Duplicate...", " ");
run("Threshold Colour");
// Threshold Colour v1.13------
// Autogenerated macro, single images only!
// G. Landini 30/Aug/2011.
//

min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=63;
max[0]=206;
filter[0]="stop";
min[1]=18;
max[1]=255;
filter[1]="pass";
min[2]=11;
max[2]=255;
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  if (filter[i]=="stop")  run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
  selectWindow(""+i);
  close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
// Threshold Colour ------------
run("8-bit");
run("Create Selection");
selectWindow("SBM-2-AT8-LVL1-34-sl11-CurvesAdj.tif");
run("Restore Selection");
setBackgroundColor(255, 255, 255);
run("Make Inverse");
run("Clear", "slice");
run("Select None");
saveAs("Tiff", outputDir1 + t1 + ".tif");
run("8-bit");

saveAs("Tiff", outputDir2 + t2 + ".tif");
                       
                        run("Close");
                }
        }
}

Any suggestions would be very much appreciated!

Kind regards,

Tate