// Bright and Dark areas detection by autothresholding // Get in black the bright or dark areas obtained by auto-threshold whatever the weight // of dark and bright pixels into the image. // A demo image, courtasy of Damien Destouches, is available at // http://rsb.info.nih.gov/ij/macros/images/Cells2MG.tif // "Auto Threshold Dark" // Set to black the highest part of the histogram "volumes" delimited by the so called // lower and upper ImageJ threshold values. Volume is assumed to be the // sum of (pixel values * number of pixel). // "Auto Threshold Dark and binary Segment" make a Watershed onto the binary // image obtained by the "Auto Threshold Dark" macro. // "Auto Threshold Dark and 8-bit Segment" make a subsegmentation onto the binary // image obtained by the "Auto Threshold Dark" macro, using the "Find Maxima..." ImageJ function. // Author : Gilles Carpentier // Faculte des Sciences et Technologies, // Universite Paris 12 Val de Marne, France. var imageinit,mask1; macro "Auto Threshold Dark" { setBatchMode(true); AutoThresholdDark (); setBatchMode("exit and display"); } macro "Auto Threshold Dark and binary Segment" { setBatchMode(true); AutoThresholdDark (); Segment (imageinit,mask1,2); setBatchMode("exit and display"); } macro "Auto Threshold Dark and 8-bit Segment" { setBatchMode(true); AutoThresholdDark (); Segment (imageinit,mask1,8); setBatchMode("exit and display"); } function AutoThresholdDark () { setOption("BlackBackground", true); partofHisto=0;histo1=0; run("Select None");imageinit = getImageID(); depth=bitDepth; run("Duplicate...", "title=AutoThreshold and Segmeted");workimage = getImageID(); if (depth > 8) {run("8-bit");} run("Grays"); // get the auto threshold values setAutoThreshold(); getThreshold(lower, upper ); print("\\Clear"); resetThreshold(); // analyse of the histogram: getStatistics(area, mean, min, max, std, histogram); vol=newArray(3); vol[0]=volumeHisto (histogram,min,lower); vol[1]=volumeHisto (histogram,lower,upper); vol[2]=volumeHisto (histogram,upper,max); print("\"Volume\" 1 between min (" + min + ") and lower ("+ lower +") = "+ vol[0]); print("\"Volume\" 2 between lower (" + lower + ") and upper ("+ upper +") = "+ vol[1]); print("\"Volume\" 3 between upper (" + upper + ") and max ("+ max +") = "+ vol[2]); maxvol=maxOf(vol[1], vol[2]); for (a=1; a<3; a++) {if (maxvol == vol[a]) partofHisto = a;} if (partofHisto == 1) histo1=lower; if (partofHisto == 2) histo1=upper; setThreshold(histo1, max,"black & white"); print("Black (dark pixels) obtained by thresholding between " + histo1 +" and " + max); run("Convert to Mask"); run("Invert"); mask1 = getTitle; } function volumeHisto (histo,mini,maxi) { volhisto=0; if ((maxi-mini) > 0) { for (i=mini; i<= maxi; i++) { volhisto=volhisto +(i* histo[i]); } } return volhisto; } function Segment (ima1,mask1,depth) { if (depth == 8) { selectImage (ima1);run("Duplicate...", "title=temp");temp=getImageID(); run("Gaussian Blur...", "sigma=2"); run("Find Maxima...", "noise=4 output=[Segmented Particles] light "); masqueSeg=getImageID(); selectImage (temp); close (); imageCalculator("multiply",mask1,masqueSeg); selectImage (masqueSeg); close (); } else { selectWindow (mask1); run("Watershed"); } selectWindow (mask1); run("Duplicate...", "title=[Mask of "+ depth +" bit segmented-image]" );temp1=getImageID(); selectImage (mask1);run("Outline"); selectImage (ima1); run("Duplicate...", "title=["+ depth +" bit segmented-image]");segmentedIma=getImageID(); imageCalculator("add",segmentedIma,mask1); selectImage (mask1);close (); }