Skip to content

Instantly share code, notes, and snippets.

@joelcarlson
Created July 31, 2015 10:54
Show Gist options
  • Save joelcarlson/d2a6e7473bc2c536e3ec to your computer and use it in GitHub Desktop.
Save joelcarlson/d2a6e7473bc2c536e3ec to your computer and use it in GitHub Desktop.
FIJI (ImageJ) macro to segment and calculate shape and first order statistics of cell nuclei in H&E stained histological images. Based on http://earlybreast.becklab.org/code . Make sure in the output directory you have a folder called "Image" for the thresholded images to save to, or comment out the first saveAs call. Several parameters are empi…
input = getDirectory("Input directory");
output = getDirectory("Output directory");
Dialog.create("File type");
Dialog.addString("File suffix: ", ".tiff", 5);
Dialog.show();
suffix = Dialog.getString();
processFolder(input);
//Loop over directory structure
function processFolder(input) {
list = getFileList(input);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + list[i]))
processFolder("" + input + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
//Process file in directory structure
function processFile(input, output, file) {
print("Processing: " + input + file);
open(input + file);
name = getTitle;
//ImageJ cannot redirect the calculated parameters to the same image being edited, so it must be duplicated
// and the parameters calculated on the previous image
run("Duplicate...", " ");
//Add whatever parameters you wish
run("Set Measurements...", "area mean standard modal min perimeter shape feret's integrated median skewness kurtosis redirect=[" + name +"] decimal=3");
//sigma = 1.5 is empirical, set your own based on experience
run("Gaussian Blur...", "sigma=1.5");
// Color Thresholder 2.0.0-rc-34/1.50a
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("RGB Stack");
run("Convert Stack to Images");
selectWindow("Red");
rename("0");
selectWindow("Green");
rename("1");
selectWindow("Blue");
rename("2");
min[0]=0;
max[0]=165;
filter[0]="pass";
min[1]=0;
max[1]=153;
filter[1]="pass";
min[2]=0;
max[2]=184;
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);
// Colour Thresholding-------------
//Binarize and watershed to allow overlay and calculation
run("Make Binary");
run("Watershed");
print("Saving Image to: " + output + "/Images/" + file);
saveAs("Tiff", output + "/Images/" + file);
//size=120-infinity is empirical, set your own if you wish.
run("Analyze Particles...", "size=120-Infinity show=[Overlay Masks] exclude clear");
name = getTitle;
index = lastIndexOf(name, ".");
if (index!=-1) name = substring(name, 0, index);
name = name + ".csv";
print("Saving to: " + output + name);
saveAs("results" , output + name );
close();
close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment