0

I have an image with arbitrary regions shape (say objects), let's assume the background pixels are labeled as zeros whereas any object has a unique label (pixels of object 1 are labeled as 1, object 2 pixels are labeled as 2,...). Now for every object, I need to find the best elliptical fit of its pixels. This requires finding the center of the object, the major and minor axis, and the rotation angle. How can I find these?

Thank you;

user84310
  • 121
  • 1
  • 6

2 Answers2

1

Principal Component Analysis (PCA) is one way to go. See Wikipedia here.

The centroid is easy enough to find if your shapes are convex - just a weighted average of intensities over the xy positions - and PCA will give you the major and minor axes, hence the orientation.

Once you have the centre and axes, you have the basis for a set of ellipses that cover your shape. Extending the axes - in proportion - and testing each pixel for in/out, you can find the ellipse that just covers your shape. Or if you prefer, you can project each pixel position onto the major and minor axes and find the rough limits in one pass and then test in/out on "corner" cases.

It may help if you post an example image.

Roger Rowland
  • 25,885
  • 11
  • 72
  • 113
  • are you sure? yes it would give the direction and 2 magnitutes but they are only statistical. the OP needs an algorithm that gives the minimum ellipse that contains all (not 99%) of the pixels – Bernd Elkemann Dec 14 '13 at 09:25
  • @eznme if you use all of the PCA output, that covers all of the pixels. Maybe you're confusing this with using PCA for dimensionality reduction? It will give vectors for the axes and you can decide to extend to two-sigma, three-sigma, whatever precision you like. – Roger Rowland Dec 14 '13 at 09:27
  • the problem is how to choose the sigmas. we dont want a sigma that makes an ellipse that covers 99% or 99.9% of the pixels and we dont want a a sigma that is too large (ie a sigma of which sigma*0.7 would already cover 100% of pixels). choosing the **minimum** sigma that covers 100% of pixels is what makes the problem hard. – Bernd Elkemann Dec 14 '13 at 09:51
  • @eznme it's not hard - see my updated answer - if you have all of the pixel positions, you just need to project them on to the two axes and maintain the limits of the projections. 100% fit. – Roger Rowland Dec 14 '13 at 09:53
  • i find that hard, but it works. (and there is no easier solution) – Bernd Elkemann Dec 14 '13 at 09:55
  • @eznme well, it's quite simple linear algebra, but I grant you that there's no easier solution. Maybe I'll put more detail in the answer. – Roger Rowland Dec 14 '13 at 09:57
  • Thank you for your responses, I think I managed to get the centroid using some equations, but I still don't know how the PCA works. If you can provide a MATLAB example, that would be great. I will add a sample image in the question for your reference. – user84310 Dec 14 '13 at 11:25
  • 1
    @user84310: See my answer for a built-in Matlab command that does the PCA for you. – DCS Dec 14 '13 at 12:55
  • @user84310 as @DCS says, if you're using Matlab, `regionprops` is your answer - there is also [a related question here](http://stackoverflow.com/q/11757377/2065121) – Roger Rowland Dec 14 '13 at 14:53
1

As you seem to be using Matlab, you can simply use the regionprops command, given that you have the Image Processing Toolbox.

It can extract all the information you need (and many more properties of image regions) and it will do the PCA for you, if the PCA-based based approach suits your needs.

Doc is here, look for the 'Centroid', 'Orientation', 'MajorAxisLength' and 'MinorAxisLength' parameters specifically.

DCS
  • 3,354
  • 1
  • 24
  • 40