Skip to main content

image processing - How to count number of small dots in a picture


I have this picture of small particles in a polymer film. I want to count how many particles in the figure, so that I can have a rough estimation of the particle density. But the image quality is poor, I had a hard time to do it.


particle in film - grayscale, particle in film - color


I have tried several ways to do it, but failed. below is the code. The first method I tried is:


`SetDirectory["C:\\Users\\mayao\\documents"]
image = Import["Picture3.jpg"];
imag2 = Binarize[image, {0.0, 0.8}];
cells = SelectComponents[DeleteBorderComponents[imag2], "Count", -400];


circles = ComponentMeasurements[ImageMultiply[image,cells],"Centroid", "EquivalentDiskRadius"}][[All, 2]]; Show[image, Graphics[{Red, Thick, Circle @@ # & /@ circles}]]

Here is what I got: enter image description here


So it does not count all the particle. Plus, it sometimes take several particle as one.


I read another method from a thread here, the code is:


obl[transit_Image] := (SelectComponents[
MorphologicalComponents[
DeleteSmallComponents@
ChanVeseBinarize[#, "TargetColor" -> Black],
Method -> "ConvexHull"], {"Count", "SemiAxes"},

Abs[Times @@ #2 Pi - #1] < #1/100 &]) &@
transit; GraphicsGrid[{#, obl@# // Colorize,
ImageMultiply[#,
Image@Unitize@
obl@#]} & /@ (Import /@ ("C:\\Users\\mayao\\documents\\" <> # \
& /@ {"Picture1.jpg", "Picture2.jpg", "Picture3.jpg",
"Picture1.jpg"}))]

But it does not recognize the single particles:


enter image description here



Is there any other method to do this task? Thanks a lot for any suggestions.



Answer



get the binarize image


 img = Import["http://i.stack.imgur.com/0OJd7.jpg"];
binimg = LocalAdaptiveBinarize[img, 25];

enter image description here


The effect like this:


    big = ImageDemosaic[binimg // ColorConvert[#, "Grayscale"] &, 
"RGGB"] // MinDetect // SelectComponents[#, "Count", # > 100 &] &;

(array = WatershedComponents[GradientFilter[big, 2],
DistanceTransform[big] // MaxDetect]) // Colorize

enter image description here


Then your number is


array // Max


1650




Update===========================================================


Use the Closing to optimize the binarize image.


binimg = Closing[LocalAdaptiveBinarize[img, 25], 3]

enter image description here


Then we get the array and verify the effect.


(array = WatershedComponents[GradientFilter[binimg, 2], 
DistanceTransform[binimg // ColorNegate] //
MaxDetect]) // Colorize


enter image description here


Or you can like this:


Show[img, 
Graphics[{Red,
Point[(array /. 1164 -> 0 //
ComponentMeasurements[#, "Centroid"] &)[[All, 2]]]}]]

enter image description here


So the number of your component is:


array // Max



1340



Comments