Login  Register

Analyze particles Exclude edge particles not working when automated

Posted by Mariam Shirdel on Feb 18, 2015; 12:57pm
URL: http://imagej.273.s1.nabble.com/Analyze-particles-Exclude-edge-particles-not-working-when-automated-tp5011662.html

Hi!
I'm trying to analyse images of particles similar to the image I have attached. When I do this manually in ImageJ exclude edge particles in Analyze Particles works fine. When I try to use a macro I've modified everything works except for exclude edge particles. I can use the command and Analyze Particles works and I get all of the results, it's just that exclude edge particles is ignored. I've tried to figure out what I'm doing wrong, but I really can't find it. Does anyone know what could be wrong?

The whole macro is:

requires("1.33s"); //display a message and aborts the macro if the ImageJ version is less than the one specified.

print("\\Clear");
run("Clear Results");

//Set the correct measurements
run("Set Measurements...", "area perimeter fit shape area_fraction redirect=None decimal=3");
setBatchMode(false);
rolling = 50; // the rolling ball background subtraction diameter
sigma = 1.0; // the sigma diameter for Gaussian blur
thresholdMethod = newArray("Default", "Huang", "Intermodes", "IsoData", "Li", "MaxEntropy", "Mean", "MinError(I)", "Minimum", "Moments", "Otsu", "Percentile", "RenyiEntropy", "Shanbhag", "Triangle", "Yen"); // the threshold method applied by Multithresholder
minCirc = 0.00; // the minimum circularity for a particle
maxCirc = 1.00; // the maximum circularity for a particle
excludeEdge = true;
APExclude = "";
watershed = false;

//Prompt for a folder that contains the images and text files from the ESEM
dir = getDirectory("Choose a Directory ");

//Get the correct values to set the scale
distance_pixels = getNumber("Distance in pixels?", 313);
known_um = getNumber("Known distance?", 200);
minArea = (known_um/distance_pixels)^2; // the minimum area to be considered as a particle in um, one pixel in um
maxArea = 10000; // the maximum area to be considered as a particle in um, equations only relevant for diameter of particle below 100 um

Dialog.create("Segmentation parameters");
Dialog.addNumber("Rolling ball diameter (0=don't subtract):", rolling); //1
Dialog.addNumber("Gaussian blur diameter (0=don't blur):", sigma); //2
Dialog.addChoice("Threshold method:", thresholdMethod); //3
//Dialog.addNumber("Minimum particle area (um):", minArea); //4
//Dialog.addString("Maximum particle area (um):", maxArea); //5
Dialog.addNumber("Minimum particle circularity:", minCirc); //6
Dialog.addNumber("Maximum particle circularity:", maxCirc); //7
Dialog.addCheckbox("Run watershed", watershed); //8
Dialog.addCheckbox("Exclude edge particles", excludeEdge); //9
Dialog.show();
rolling = Dialog.getNumber(); //1
sigma = Dialog.getNumber(); //2
thresholdMethod = Dialog.getChoice(); //3
//minArea = Dialog.getNumber(); //4
//maxArea = Dialog.getString(); //5
minCirc = Dialog.getNumber(); //6
maxCirc = Dialog.getNumber(); //7
watershed = Dialog.getCheckbox(); //8
excludeEdge = Dialog.getCheckbox(); //9

if (excludeEdge) //will be used later when Analyze Particle is run
                             APExclude = "exclude";

filenames = getFileList(dir);
for (i=0; i<filenames.length; i++){
                             imagefile = filenames[i];

                             //Open the image and set the scale for the image
                             open(dir + imagefile);
                             run("Set Scale...", "distance=distance_pixels known=known_um pixel=1 unit=um global");

                             //Subtract the background to minimize noise. 50 seems to work well.
                             if (rolling > 0)
                                                          run("Subtract Background...", "rolling=" + rolling); // + " light"

                             //Blur the image a bit.
                             if (sigma > 0)
                                                          run("Gaussian Blur...", "sigma=" + sigma);

                             //Use thresholding, IsoData (1) or Otsu (2) for CT, MaxEntropy (2) or RényiEntropy (1) for PC.
                             setAutoThreshold(thresholdMethod + " dark");
                             run("Threshold...");
                             setOption("BlackBackground", false);
                             run("Convert to Mask");

                             //Close the particles and fill the holes that might arise from closing.
                             run("Close-");
                             run("Fill Holes");

                             //Watershed gets a few more particles and segments particles that are supposed to be divided.
                             if (watershed)
                                                          run("Watershed");

                             //Analyze the particles in the image.
                             run("Analyze Particles...", "size=" + minArea + "-" + maxArea + " circularity=" + minCirc + "-" + maxCirc + " show=Outlines display " + APExclude + " clear summarize");

                             //Select the Results window
                             selectWindow("Results");
                             //Save the Results to a file both as .txt and .xls
                             //.txt
                             saveAs("text", dir + "Results_" + imagefile);
                             //.xls
                             index = lastIndexOf(imagefile, ".");
                             if (index!=-1)
                                                          imagefilenew = substring(imagefile, 0, index);
                             imagefilenew = imagefilenew + ".xls";
                             saveAs("Results", dir + "Results_" + imagefilenew);

                             //Select the particle outlines window
                             outlines = "Drawing of " + imagefile;
                             selectWindow(outlines);
                             //Save the outline drawing produced by the particle analysis
                             saveAs("tiff", dir + outlines);

                             //Copy the particle outlines window and add it to the original image file
                             close(imagefile);
                             open(dir + imagefile);
                             selectWindow(imagefile);
                             run("RGB Color"); //original image file is changed from 8-bit to RGB color
                             imageCalculator('AND', imagefile, outlines);
                             saveAs("tiff", dir + "Overlay_" + imagefile);

                             print("\\Clear");
                             run("Clear Results");
}

//Select the Summary window
selectWindow("Summary");
//Save the Summary to a txt file
saveAs("text", dir + "Summary");

//Create a file with all the information about how the image has been manipulated
criteriaPath = dir + "PSA_criteria.csv";
criteriaFile = File.open(criteriaPath);
print(criteriaFile, "rolling, sigma, thresholdMethod, minArea, maxArea, minCirc, maxCirc, excludeEdge, watershed, distance, known"); //writes to the open file criteriaFile
print(criteriaFile, rolling + ", " + sigma + ", " + thresholdMethod + ", " + minArea + ", " + maxArea + ", " + minCirc + ", " + maxCirc + ", " + excludeEdge + ", " + watershed + ", " + distance_pixels + ", " + known_um); //writes to the open file criteriaFile
File.close(criteriaFile);

//Close all open windows
close("\\Others");
close();
list = getList("window.titles");
for (j=0; j<list.length; j++){
     windowname = list[j];
     selectWindow(windowname);
     run("Close");
}

Best regards,
Mariam

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

PC3940_Compo_02.tif (1M) Download Attachment