Unwanted holes filling in particle analysis


I am doing particle analysis following a color tresholding and creation of the binary file.

run(“Analyze Particles…”, “size=600-Infinity show=[Overlay Masks] display clear in_situ”)

However holes seem to be filled, and I don’t want them to.
Binary :
<img src="//discourse-cloud-file-uploads.s3.dualstack.us-west-2.amazonaws.com/business4/uploads/imagej/original/2X/5/5af08ab461c4220d300c6efbafcca454441523d8.png" width=“621” height=“477”).>

Mask :

Yet I did not chose the “include holes” option; why is this happenning ? I found a similar issue here : http://imagej.1557.x6.nabble.com/analyze-particles-exclude-holes-td5002812.html. One of the issues highlighted there are the difference between white and black background, but I don’t see how I can apply it here (I am using white background).

The objective is to measure the total area (black in the binary).

For context, here is my macro :

macro "Soil cover batch [b]" {
run("Clear Results");
dir1 = getDirectory("Choose Source Directory ");
dir2  = File.getParent(dir1);
dirname1 = File.getName(dir1)
dirName = File.getName(dir2)
list = getFileList(dir1);
run("Clear Results"); 
row = 0; 
for (i=0; i<list.length; i++) { 
if (endsWith(list[i], "live.JPG")){
showProgress(i, list.length); 
setOption( "BlackBackground", false);
img = getImageID();
run( "Color Threshold..." ); 
waitForUser( "Set threshold and click OK." );
run( "Make Binary" );
waitForUser( "Remove some particles ?" );
run("Analyze Particles...", "size=600-Infinity circularity=0.01-1.0 show=[Overlay Masks] display clear in_situ");
run( "Measure" );
waitForUser( "Check" );
setResult("Label", row, list[i]); 
row = row +1;

 setOption("ShowRowNumbers", false); 
 path = 'path/to/results"; 
 saveAs("Results", path);


Since the goal is the total area, I suggest trying Edit -> Create Selection to generate a single ROI for the whole thresholded region. Then you should get your result in one go, just by measuring that ROI.

As far as I recall, Analyze Particles listens to the ‘include holes’ option for any measurements that it makes, but the ROIs that it generates are polygons made by tracing external boundaries - and not (as you might expect) more complicated shapes that can support having holes inside.

Therefore using Analyze Particles to create final measurements should be fine. It’s also fine to use it to create binary images using a size and/or circularity threshold. However using it to generate ROIs for later use is only appropriate whenever:

  • you do want to fill in holes, or
  • there aren’t any holes in the regions you’re measuring anyway

Therefore if you need to exclude small regions and then measure the total area, you might want to create a mask with Analyze Particles first, and then use Create Selection to make a ROI from the resulting binary image.

Hope this helps,


In addition to the answer by @petebankhead, please note that Analyze Particles always fills holes when used with the Show: Outlines or Show: Overlay Masks mode. If you want to work with just the thresholded area, use Show: Masks which will respect the Include holes option.

(As far as I know, this ImageJ1.x behavior is by design, but @Wayne might want to comment on this.)

1 Like

Thanks, this solves the issue of holes indeed. But is there any way I can pre-visualize which areas will be selected after the size filter? Alternatively, when I do the particle analysis, can I see in “real time” how the input threshold modifies the selection?

I suspect this may be reinventing the wheel, but I don’t know of another command to do it. Anyhow, you could try this Groovy script in Fiji:

It should create a new image from your binary image, in which each pixel has the area of the particle that it is part of.

Then you can simply threshold this image to exclude small particles (or, indeed, large ones).

Well, if you are just after the total area of the black part of the image, running the particle analyzer for that purpose is very inefficient.
Instead, just compute the image histogram and see how many counts are in the intensity[0] bin.

1 Like