Skip to content

Instantly share code, notes, and snippets.

@volker-baecker
Last active November 17, 2017 16:27
Show Gist options
  • Select an option

  • Save volker-baecker/627885ea40320e047f8b88f2888673af to your computer and use it in GitHub Desktop.

Select an option

Save volker-baecker/627885ea40320e047f8b88f2888673af to your computer and use it in GitHub Desktop.
Use image scaling and binary watershed to separate the nuclei in the image and create an indexed mask.
/**
* Separate Nuclei
*
* Use image scaling and binary watershed to separate the nuclei in the image
*
* written 2013 by Volker Baecker (INSERM) at Montpellier RIO Imaging (www.mri.cnrs.fr)
*
* This has been created from
* http://dev.mri.cnrs.fr/projects/imagej-macros/wiki/Separate_Nuclei_Tool
*
* This version does not measure anything but creates an indexed mask of the separated
* nuclei in order to allow benchmarking.
*
* Use something similar to the following to run this macro from the command-line:
* java -Xmx6000m -cp jars/ij-1.51n.jar ij.ImageJ -headless --console -macro separate_nuclei.ijm "input=/home/baecker/Downloads/nuclei-hl60/HL60_LowNoise_C50_3D_TIFF, output=/home/baecker/Downloads/nuclei-hl60/out, scale_factor=3"
*/
setBatchMode(true);
// Path to input image and output image (label mask)
inputDir = "/data/in/";
outputDir = "/data/out/";
// Functional parameters
var SCALE_FACTOR = 5;
arg = getArgument();
parts = split(arg, ",");
for(i=0; i<parts.length; i++) {
nameAndValue = split(parts[i], "=");
if (indexOf(nameAndValue[0], "input")>-1) inputDir=nameAndValue[1];
if (indexOf(nameAndValue[0], "output")>-1) outputDir=nameAndValue[1];
if (indexOf(nameAndValue[0], "scale_factor")>-1) SCALE_FACTOR=nameAndValue[1];
}
images = getFileList(inputDir);
run("Options...", "iterations=1 count=1 black");
for(i=0; i<images.length; i++) {
showProgress(i, images.length);
image = images[i];
if (!endsWith(image, ".tif")) continue;
roiManager("reset");
open(inputDir + "/" + image);
run("Select None");
processImage();
run("Remove Overlay");
createIndexedMaskFromRois();
save(outputDir + "/" + image);
run("Close All");
}
setBatchMode(false);
run("Quit");
function processImage() {
run("Scale...", "x="+(1.0/SCALE_FACTOR)+" y="+(1.0/SCALE_FACTOR)+" interpolation=Bilinear create title=small_tmp");
setAutoThreshold("Huang dark");
run("Convert to Mask");
run("Fill Holes");
run("Watershed");
run("Scale...", "x="+SCALE_FACTOR+" y="+SCALE_FACTOR+" interpolation=Bilinear create title=big_tmp");
setAutoThreshold("Huang dark");
roiManager("Reset");
run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing exclude add");
selectWindow("small_tmp");
close();
selectWindow("big_tmp");
close();
roiManager("Show All");
}
function createIndexedMaskFromRois() {
setForegroundColor(0);
run("Select All");
run("Fill", "slice");
run("Select None");
count = roiManager("count");
for(i=0; i<count; i++) {
roiManager("select", i);
run("Add...", "value="+(i+1));
}
roiManager("deselect");
run("Select None");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment