Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Fitting an ellipse to an image

Subject: Fitting an ellipse to an image

From: Luca

Date: 12 Jun, 2012 14:57:07

Message: 1 of 2

I have the following problem. I have an image where there is an object that looks very much like half an ellipsoidal annulus in an uniform background.
It is very clear and sharp.
Therefore I want to fit an ellipsoidal annulus to it.
Seems an easy task.
I'm working in 3D therefore I have 11 parameters to describe this object: center coordinate (p1,p2,p3), short axis(p4), long axis (p5), thickness of the annulus (p6), description of the plane cutting in half the ellipsoidal annulus (p7,p8,p9), mean value of the background (p10), mean value of the ellipsoidal annulus (p11).

First thing I've tried: do an intensity segmentation, get a map of 1 and 0 (where there is and where there isn't my annulus), make my ellipsoidal annulus mask, xor the two mask, count the number of 1 in the xor (in this way I had only 9 parameters, not 11, as the last two did not apply). Minimize the difference with fminsearch.
Works great as long as the first segmentation went fine . Which does not always happens. So, to make my code more sensible and to avoid a non-useful step as the first segmentation, I tried to generate a full image of this annulus in the background and then calculate a difference between this image and mine. (I used sum of squared differences).
I tried to minimize this function with fminunc (for what I've been told I shouldn't be using fminsearch already with 9 parameters, go figure with 11) and it does not work. Can't get anywhere. It stops at iteration 1 and says: "can't minimize the function along the current search direction". I investigated and I think that the problem lies in the fact that this objective function has a randomatic behaviour for very small changes in the parameters (for dx=~0.001. amplitude of local minima induced by such dx: ~0.0001*f(x) ). I think this is screwing fminunc up.
I think that the reason for this is that my annulus is computed as a discrete mask. Therefore a small change in its parameters does not change the whole mask but just a couple of pixels at a time, producing a random and not continous behaviour.
Said that I don't have any idea of how to generate a "less discretized" ellipsoid. Can anybody suggest me anything?

BTW, out of desperation I've tried fminsearch.
It works fine.
Any suggestion on what could I do?

Subject: Fitting an ellipse to an image

From: Matt J

Date: 12 Jun, 2012 16:01:07

Message: 2 of 2

"Luca " <l.presottoRE@MOVE.campus.unimib.NOTit> wrote in message <jr7lc3$akh$1@newscl01ah.mathworks.com>...
>
> Said that I don't have any idea of how to generate a "less discretized" ellipsoid. Can anybody suggest me anything?
===============

I would first extract the coordinates of your ellipse boundaries and fit a 3D plane to them using SVD. Then compute the 2D coordinates of these points in this 3D plane. Then use the file below to fit an ellipse to the 2D points.


function report=ellipsefit(XY)
%ELLIPSEFIT - form 2D ellipse fit to given x,y data
%
% report=ellipsefit(XY)
%
%in:
%
% XY: Input matrix of 2D coordinates to be fit. Each column XY(:,i) is [xi;yi]
%
%out: Finds the ellipse fitting the input data parametrized both as
% A*x^2+B*x*y C*y^2+D*x+E*y=1 and [x-x0,y-y0]*Q*[x-x0;y-y0]=1
%
% report: a structure output with the following fields
%
% report.Q: the matrix Q
% report.d: the vector [x0,y0]
% report.ABCDE: the vector [A,B,C,D,E]
% report.AxesDiams: The minor and major ellipse diameters
% report.theta: The counter-clockwise rotation of the ellipse.
%
%NOTE: The code will give errors if the data fit traces out a non-elliptic or
% degenerate conic section.
%
%See also ellipsoidfit

X=XY(1,:).';
Y=XY(2,:).';

M= [X.^2, X.*Y, Y.^2, X, Y, -ones(size(X,1),1)];

[U,S,V]=svd(M,0);
ABCDEF=V(:,end);

if size(ABCDEF,2)>1
    
    error 'Data cannot be fit with unique ellipse'
else

    ABCDEF=num2cell(ABCDEF);
end

[A,B,C,D,E,F]=deal(ABCDEF{:});


Q=[A, B/2;B/2 C];
x0=-Q\[D;E]/2;
dd=F+x0'*Q*x0;

Q=Q/dd;

[R,eigen]=eig(Q);
eigen=eigen([1,4]);

if ~all(eigen>=0), error 'Fit produced a non-elliptic conic section'; end

idx=eigen>0;
eigen(idx)=1./eigen(idx);
AxesDiams = 2*sqrt(eigen);

theta=atand(tand(-atan2(R(1),R(2))*180/pi));


report.Q=Q;
report.d=x0(:).';
report.ABCDE=[A, B, C, D, E]/F;
report.AxesDiams=sort(AxesDiams(:)).';
report.theta=theta;

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us