// Objects Color Sorting // This macro shows a mean to sort objects from their color (red // mean value > blue mean value +/- a tolerance in the example), // after an "MaxEntropy" auto thresholding. // Author : Gilles Carpentier // Faculte des Sciences et Technologies, // Universite Paris 12 Val de Marne, France. // Image sample: http://rsbweb.nih.gov/ij/macros/images/cd3-sample.tif // CD3 (endothelial cells) immunostaining of a coronal mouse brain section. // Image sample with courtesy of Maylis Delugin and Pr Michel Moenner // Inserm U920, Universite Bordeaux I, France var shrinkDistanceID,idfixe,ExclusionTolerance=20; macro "Objects Color Sorting" { // generate objects by segmentation on the blue channel requires("1.45e"); setBatchMode(true); setBackgroundColor(255, 255, 255); run("Select None"); run("Remove Overlay"); Dialog.create("Set Exclusion Tolerance"); Dialog.addNumber("Tolerance", ExclusionTolerance); Dialog.show(); ExclusionTolerance=Dialog.getNumber(); imageName=getTitle(); run("Duplicate...", "title=ObjectsTotal"); run("RGB Stack"); setSlice(3); run("Duplicate...", "title=Objects"); selectImage("Objects"); setAutoThreshold("MaxEntropy"); run("Convert to Mask"); segMask=getTitle(); run("Set Measurements...", "area bounding redirect=None decimal=2"); run("Analyze Particles...", "size=20-Infinity circularity=0.00-1.00 show=Masks display clear record"); rename ("KeptObjects"); run("Duplicate...", "title=ObjectsTotalAnalysed"); selectImage("ObjectsTotal"); close (); selectImage (imageName);run("Duplicate...", "title=tempRGB"); // object sorting based on analysis of object histogramms red and blue mean values. startTime=getTime(); for (i=0; i< nResults; i++) { selectImage("ObjectsTotalAnalysed");totalBin=getImageID(); setThreshold(255, 255, "none"); doWand(getResult("XStart",i), getResult("YStart",i)); selectImage("tempRGB");run("Restore Selection"); run("Add Selection...", "stroke=red width=1"); // show detected objects as overlay colorValues=objectsColorHistoValues(); if ((colorValues[0] <= colorValues[2]) && (sqrt(pow((colorValues[0] - colorValues[2]),2)) > ExclusionTolerance)){ selectImage("KeptObjects"); setThreshold(255, 255, "none"); doWand(getResult("XStart",i), getResult("YStart",i)); run("Clear"); run("Select None"); selectImage("tempRGB");run("Restore Selection"); run("Add Selection...", "stroke=blue width=1"); // show excluded objects as overlay } run("Select None"); } imageCalculator("Subtract create", "ObjectsTotalAnalysed","KeptObjects"); rename ("RemovedObjects"); selectImage("tempRGB"); Overlay.copy; close (); selectImage("Objects"); close(); selectImage(imageName); Overlay.paste; setBatchMode("exit and display"); blink (imageName); } macro "-" {} macro "Open Sample Image [o]" { run("URL...", "url=http://rsbweb.nih.gov/ij/macros/images/cd3-sample.tif"); } macro "Hide Overlay [h]" { run("Hide Overlay"); } macro "Show Overlay [s]" { run("Show Overlay"); } macro "Remove Overlay [r]" { run("Remove Overlay"); } // return the mean value of the r,g and b histograms of selected objects function objectsColorHistoValues () { // adapted from a code of Wayne Rasband colorStat=newArray(3); setRGBWeights(1, 0, 0); getStatistics(area, mean, min, max, std, hist); colorStat[0]=MeanStatHisto (hist,1,255); setRGBWeights(0, 1, 0); getStatistics(area, mean, min, max, std, hist); colorStat[1]=MeanStatHisto (hist,1,255); setRGBWeights(0, 0, 1); getStatistics(area, mean, min, max, std, hist); colorStat[2]=MeanStatHisto (hist,1,255); return colorStat; } // return the mean value of an histogram function MeanStatHisto (histo,mini,maxi) { volhisto=0;meanhisto=0;nbPixel=0; if ((maxi-mini) > 0 ) { for (i=mini; i<= maxi; i++) { volhisto=volhisto +(i* histo[i]); nbPixel=nbPixel+(histo[i]); } } if (nbPixel >0) meanhisto=volhisto/nbPixel; return meanhisto; } function blink (image) { selectImage (image); start = getTime; while (click()==0 && (getTime-start)<4000) { wait(500);click();run("Hide Overlay"); wait(500);run("Show Overlay"); } } // adapted from the GetCursorLocDemo macro available at the // http://rsb.info.nih.gov/ij/macros/GetCursorLocDemo.txt function click() { rightButton=4;leftButton=16; getCursorLoc(x, y, z, flags); if (flags&leftButton!=0) exit; if (flags&rightButton!=0) exit; return 0; }