Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have hundreds of images similar to this image:


I'm looking to count the number of ellipses as well as their major and minor diameters. The ellipses are touching, hence,

img = Import["http://i.imgur.com/oNrJq0j.png"]

doesn't return fillable ellipses so using something like


returns useless information.

I'm very new to this type of image analysis using Mathematica so any help would be greatly appreciated.

share|improve this question
A Flying saucers invasion detector! –  belisarius Oct 5 '13 at 0:51
Hmm... At first glance I thought all the ellipses had the same "shape", that is, orientation and aspect ratio, so if you affine-tranformed the image in the right way they would all turn into circles and detecting them would become much easier. But it turns out that this is not the case: i.stack.imgur.com/Zl5M2.png –  Rahul Narain Oct 5 '13 at 1:00
Check this out –  Rojo Oct 5 '13 at 2:15

1 Answer 1

up vote 20 down vote accepted

One standard way to detect circular shapes is to binarize the image and apply a distance transform: The maxima locations of the distance transform are the centers of the circles.

To make this work on your ellipses, I first have to stretch them to be (roughly) circular, as @Rahul Narain suggested in a comment:

img = ColorConvert[Import["http://i.imgur.com/oNrJq0j.png"], 
{w, h} = ImageDimensions[img];
ir = ImageResize[img, {w/5, h*5}]
stretched = ImageRotate[ImageResize[img, {w/5, h*5}], 90 \[Degree]]

enter image description here

(The rotation is just for display. If I don't rotate the image by 90°, this post will be very long. It has no influence on the ellipse detection.)

Calculating the distance transform and finding the maxima is easy:

distTransform = DistanceTransform[Binarize[stretched]];    
maxima = MaxDetect[distTransform, 2];

Here's a display of the result so far:

   {"Stretched", "Binarized", "Distance Transform", 
    "Distance Transform Maxima"},
   Image[#, ImageSize -> All] & /@ {
     HighlightImage[stretched, maxima]}

enter image description here

Now I can use ComponentMeasuements to find connected maxima locations, their orientation, shape and the max. value in the distance transform image:

components = 
    distTransform}, {"Centroid", "SemiAxes", "Orientation", "Max"}];

Show[stretched, Graphics[
   components[[All, 2]] /.
     {centroid_, semiAxes_, orientation_, maxR_} :> 
        centroid, {maxR*Sqrt[semiAxes[[1]]/semiAxes[[2]]], maxR}], 
       orientation, centroid]

enter image description here

As you can see:

  • the centroids are quite good, except for the ellipses near the border, because the border changes the distance transform
  • the minor radius of the ellipses is ok (it's just the distance from the center to the nearest background point)
  • the estimated orientation is ok,
  • but the semi-axis length ratio of the maximum is only a rough estimate.
  • You could probably apply smoothing before binarizing, use MorphologicalBinarize and hand-tune the filter/binarization parameters to improve the result


I can improve the shape estimate a bit further using WatershedComponents, with the distance transform maxima as seed points:

watershedComponents = 
  WatershedComponents[ColorNegate[distTransform], maxima] *       

enter image description here

Estimated Ellipses:

components = 
   watershedComponents, {"Centroid", "SemiAxes", "Orientation"}];

Show[stretched, Graphics[
   components /.
     (n_ -> {centroid_, semiAxes_, orientation_}) :>
       Rotate[Circle[centroid, semiAxes], orientation, centroid],
       Text[n, centroid]

enter image description here

share|improve this answer
Anytime I read you answers, I learn something new. Nice answer ... –  s.s.o Oct 5 '13 at 9:09
Those are remarkably good results! Very nice. It is not clear whether the same stretching to {w/5, h*5} will work for all of the OP's images, but we can wait for him to clarify that. Another thought: "the Hessian of the... distance transform" is not likely to be useful, as the distance function is not even first-differentiable at its maxima. –  Rahul Narain Oct 5 '13 at 9:41
@RahulNarain: Yes, you're right of course. I was thinking of the absolute value, which is not differentiable at the origin, but it's square is. But, as you said, that's not true for the distance transform. I'll delete that paragraph from the answer. –  nikie Oct 5 '13 at 9:50
Hi, thanks so much for the very detailed answer. Yes, luckily all of my images have ellipses at relatively the same orientation. I'm going to have to spend a bit of time going through this answer to decipher each segment but again, thanks so much for your help! –  Andrew Stewart Oct 7 '13 at 17:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.