Delete particles depending on their size



Hi everyone, here is my problem

I want to measure the particles distribution on a picture that contains a lot of impurities, in order to measure the distrubution I am using the plugin “2D/3D spatial statistics” made by Thomas Boudier and Philippe Andrew.

I managed to understand more or less how this plugin works, but I have a problem, on my pictures there are a lot of impurities and I have to remove them, removing them manually costs a huge amount of time and I’m sure there is a way to remove them automatically with some coding.

In other words I would like to use a function that could change the color of all particles of a certain size by one other color, or at least invert these particles.

Thank you in advance for helping me, I’m a total beginner in automatization and I’m having a hard time.



Good day Theo,

although it was a good idea to post a sample image, there are various problems associated with it:

  1. The image appears to be a binary image
    (If your sample image was binary it is’t binary after JPG-compression.)
  2. Please post the original gray-level image (in TIFF or PNG-format)
    (You may also post images as Zip-archives.)
  3. Please don’t post JPG-compressed images
    (JPG-compression introduces artifacts! )
  4. Please indicate what you consider as “impurities”.




Hi Herbie, thank you for your answer !

Here is the original image before I treated it (it originally is a jpeg image, I converted it to png):


My goal is to measure the distribution between the holes and the matrix, unfortunately the contrast difference between the particles and the matrix is not always good, I’m losing several particles during the image treatment.

Because I want to keep as much particles as possible, I always end up with some small particles of matrix after doing the threshold, when I say impurities this is what I mean, the matrix that I couldnt remove.

I would like to remove these impurities. Since their global diameter is smaller than the particles I want to keep, I was thinking about a macro that invert the color of every particle of a certain size.

By the way if you know some plugins for particles analysis, I would be glad to try them !

Thank you.




if the original image is a JPG, I’d strongly recommend to redo the image acquisition using an uncompressed image format. JPG-compressed images are unsuited for scientific analyses!

It is still unclear from your sample image what you consider an acceptable particle. I see holes of different diameters and a few white particles on the surface.

Considering the holes, I recommend to make the surface more even, e.g. by polishing etc.

I assume that this is an image from an optical microscope and I recommend to use better illumiation to increase the contrast of the holes.

ImageJ’s “Analyze Particles…” allows one to define the desired range of sizes.

Have success


This is an excerpt of your sample image.
I have encircled what I consider being a hole. Do you see more than those?
I see little chance for a reasonable analysis with this rough surface and partially bad contrast of the holes.


No, in the example you showed there aren’t any other hole.

Thank you for the informations about jpg files, I will try next time to get another extraction format from my microscope.

I’m aware about the fact that this image has little chance of providing a reasonable analysis, unfortunately this is the best I managed to get with the means I have, polishing this material would result in making the sample totally unexploitable.

Even though the result will be different from reality, I still want to perform a distribution analysis.
This is another image of the same sample but with different zoom, the contrast between particles and matrix is better but I still encounter the “impurities” problem during my analysis :


Coming back to my original demand, do you know a way of changing the color of particles on a binary file regarding on their surface ?




Coming back to my original demand, do you know a way of changing the color of particles on a binary file regarding on their surface ?

As I’ve written:
“ImageJ’s “Analyze Particles…” allows one to define the desired range of sizes.”

However, I think this is the second step. First you have to get resonable binary images and I’m not sure you will succeed.




I knew this function but I did not think about using it for that purpose.

I managed to do what I want by using “Analyse Particles” with a certain minimum size, specifying I only want “bare outlines”.
Then saving the picture with bare outlines, opening it and using “fill holes” again.

So in the end I got rid of the small particles.
Thanks for your help.