Skip to content

Instantly share code, notes, and snippets.

@Granga
Last active March 28, 2016 16:16
Show Gist options
  • Select an option

  • Save Granga/9bdd6a6a2fb9fbef459d to your computer and use it in GitHub Desktop.

Select an option

Save Granga/9bdd6a6a2fb9fbef459d to your computer and use it in GitHub Desktop.
var USERSELECTSDIR = 0;
var DEFAULTDIR = "C:\\Users\\Me\\Downloads\\100_Images\\";
var myDir = "";
var originalName = "";
var originalNameNoExtension = "";
var savedName = "";
var methods;
var resultsDir = myDir + "Results\\";
var savedNameNoExtension = "";
var wandX = 0;
var wandY = 0;
var z; //notused
var modifiers; //notused
if (USERSELECTSDIR != 0) {
myDir = getDirectory("Please select a directory");
}
if (USERSELECTSDIR == 0) {
myDir = DEFAULTDIR;
}
resultsDir = myDir + "Results\\";
list = getFileList(myDir);
for (ii = 0; ii < list.length; ii++) {
originalName = list[ii];
methods = getList("threshold.methods");
wandX = 0;
wandY = 0;
for (m = 0; m < methods.length; m++) {
close("*");
open(myDir + originalName);
originalNameNoExtension = File.nameWithoutExtension;
doAutoRgbThreshold(methods[m]);
setTool("wand");
if (wandX == 0) {
waitForUser;
getCursorLoc(wandX, wandY, z, modifiers);
}
doWand(wandX, wandY, 15, "4-connected");
doExtractOutline();
savedName = doSaveImageAsTiff(resultsDir, "outline-" + methods[m], originalNameNoExtension);
close("*");
open(myDir + originalName);
open(myDir + "Results\\" + savedName);
imageCalculator("Min create", originalName, savedName);
doSaveImageAsTiff(resultsDir, "overlayed", savedName);
close("*");
}
close("*");
}
function doAutoRgbThreshold(method) {
var min = newArray(3);
var max = newArray(3);
var filter = newArray(3);
var a = getTitle();
run("RGB Stack");
run("Convert Stack to Images");
selectWindow("Red");
rename("0");
selectWindow("Green");
rename("1");
selectWindow("Blue");
rename("2");
filter[0] = "pass";
filter[1] = "pass";
filter[2] = "pass";
for (x = 0; x < 3; x++) {
selectWindow("" + x);
setAutoThreshold(method);
getThreshold(min[x], max[x]);
setThreshold(max[x], 255);
run("Convert to Mask");
if (filter[x] == "stop") run("Invert");
}
imageCalculator("AND create", "0", "1");
imageCalculator("AND create", "Result of 0", "2");
for (j = 0; j < 3; j++) {
selectWindow("" + j);
close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
}
function doExtractOutline() {
setBackgroundColor(255, 255, 255);
run("Clear Outside");
setForegroundColor(0, 0, 0);
run("Fill", "slice");
run("Outline");
}
function doSaveImageAsTiff(dir, postfix, originalName) {
File.makeDirectory(dir);
saveAs("tiff", dir + originalName + "-" + postfix);
return originalName + "-" + postfix + ".tif";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment