Detecting and Counting Objects with Circular Features

This example shows how you can use imfindcircles together with removeoverlap function (can be found in the file exchange), for counting fungi spores, which has various elliptical like shapes.
imshow(image);
Fungus Spores
As you can see, the spores have different shapes and sizes, some overlap objects from other planes of the imaged sample. This is a common microscopy problem in biology.
First, try to find circles in the image using imfindcircles. For estimating the radius range of our objects we can use imdisline:
l= imdistline;
Using a radius range between 12 to 30 pixel and visualizing the results with viscircles:
[centers, radii] = imfindcircles(image,[12 30]);
close all;figure; imshow(image);
viscircles(centers, radii,'EdgeColor','b');
Spores after imfindcircles
Lets increase the Sensitivity factor (the default is 0.85) and use a low static Edge Gradient Threshold instead of the default graytreshold.
[centers, radii] = imfindcircles(image,[12 30],'Sensitivity',0.92,'Edge',0.03);
close all;figure; imshow(image);
viscircles(centers, radii,'EdgeColor','b');
Spores image after more sensitive imfindcircles
Now, it seems we detected more circles than spores, mostly because of overlapping circles. Usingremoveoverlap function we can remove the overlapping circles, or allow an overlap of circle pair up to some tolerance, e.g: 5 pixels overlap.
[centersNew,radiiNew]=RemoveOverLap(centers,radii,5,1);
close all;figure; imshow(image);
viscircles(centersNew, radiiNew,'EdgeColor','b');
Final detection of the spores
We got a relatively good detection for the number of spores, finally we can count the number of circles.
length(centersNew)
ans = 94
So, we counted 94 spores!