# Determine circle sizes in picture

I need to determine the size of the 16 circles. Therefore I tried to binarize the picture to get the 16 circles, but i failed. I used a combination of Binarize, EdgeDetect and FillingTransform. But that wasn't successful.

The best results were obtained by:

res = Table[EdgeDetect[image, i] // FillingTransform, {i, 0.5, 5, 0.1}]

I thought I can overlay some single binarized images from res to get what I want. But that's very cumbersome.

Well, I am sure that someone with more experience can do it better. Anyway, this is my approach.

I can see that the image can enhanced its contrast to make it darker, which delimitates the circles. Then, we can do some cleaning and can remove small elements. This gives:

newimg = MinDetect[ImageAdjust[img, 1.2], .02] //
MorphologicalTransform[#, "Clean"] & //
DeleteSmallComponents[#, 70] &

Now, we can retrieve properties of the resulting components in the image with restrictions, such as the area and the elongation of the best ellipse corresponding to the regions found:

circles = ComponentMeasurements[
newimg, {"Centroid", "EquivalentDiskRadius"}, #AdjacentBorderCount == 0 &&
150 < #Area < 3000 && -.8 < #Elongation < .8 &]

Then, we can see that the more approximate radius is about 18 pixels. We extract the data of the centroids, which will serve as the centres of the circles detected. Next, we create the circles having about 18 pixels for the radius, and, then, overlay the detected circles over the original image. The result is quite approximate to that requested:

markers = # -> 1 & /@ Floor@circles[[All, 2, 1]];
markersimg = ImageRotate@Image@SparseArray[markers, Reverse@Dimensions[ImageData@img]];
img2 = Dilation[markersimg, DiskMatrix[18]];
HighlightImage[img, img2]

Improvements would include detecting all the circles and a better location of the centres.

## Using Generalized Hough Transform (GHT) to detect circles

Hough Transform, based on the Radon Transform, was devised to detect lines in an image containing edges of objects. The alogrithm has been successively improved (the GHT algorithm) to detect different geometrical (regular and non regular) smooth shapes.

Here, a custom GHT was provided to detect circles in a image. As far I know, MMA has not implemented GHT to detect circles yet. I have modified it minimally to show the final detected circles in the original image. Starting from the image newimg obtained above:

circlesimg = Show[ImageApply[Plus, HoughCircleDetection[#, 18, 21, 10, .3]],
ImageSize -> ImageDimensions[#]] &[newimg];

I use this image and the data to obtain the circles to be detected:

Dilation[ImageRotate@
Image@SparseArray[# -> 1 & /@
#AdjacentBorderCount == 0 &&
150 < #Area < 3000 && -.8 < #Elongation < .8 &][[All, 2, 1]],
Reverse@Dimensions[ImageData@img]], DiskMatrix[19]]

And, finally, it seems from the data that 19 pixels is the most appropriate value for the radius of all the circles detected in the original image:

HighlightImage[img,
Dilation[ImageRotate@Image@SparseArray[# -> 1 & /@