Fitting an ellipse such that the ratio of its radii is in a range

193 Views Asked by At

I need to fit an ellipse to a group of points. However, I have an issue and I appreciate if anyone can help me. The issue is that I need to have the fitted ellipse such that the ratio of its radii is not too big or too small (is in a known range). For example see the below figure. Ellipse 1 in this figure is the ellipse that the fitting algorithm finds. It seems that it somehow tries to find the smallest possible ellipse. However, I need to find something like Ellipse 2. Basically I need to avoid any big change in the slope of the boundary of the ellipse, or in other words, the ratio of its radii not too big or too small. As it is clear in the figure, the ratio of the radii of the Ellipse 1 is much bigger (as its horizontal radius is too small) than that of Ellipse 2.

I am using the following algorithm in Matlab which is from the article "Numerically stable direct least squares fitting of ellipses".

I very much appreciate any suggestion/solution.

enter image description here

The algorithm:

 function a = fit_ellipse(x, y)
 D1 = [x .^ 2, x .* y, y .^ 2]; % quadratic part of the design matrix
 D2 = [x, y, ones(size(x))]; % linear part of the design matrix
 S1 = D1' * D1; % quadratic part of the scatter matrix
 S2 = D1' * D2; % combined part of the scatter matrix
 S3 = D2' * D2; % linear part of the scatter matrix
 T = - inv(S3) * S2'; % for getting a2 from a1
 M = S1 + S2 * T; % reduced scatter matrix
 M = [M(3, :) ./ 2; - M(2, :); M(1, :) ./ 2]; % premultiply by inv(C1)
 [evec, eval] = eig(M); % solve eigensystem
 cond = 4 * evec(1, :) .* evec(3, :) - evec(2, :) .^ 2; % evaluate a’Ca
 a1 = evec(:, find(cond > 0)); % eigenvector for min. pos. eigenvalue
 a = [a1; T * a1]; % ellipse coefficients
 end