Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does fitEllipse work in OpenCV?

I am working with opencv and I need to understand how does the function fitEllipse exactly works. I looked at the code at (https://github.com/Itseez/opencv/blob/master/modules/imgproc/src/shapedescr.cpp) and I know it uses least-squares to determine the likely ellipses. I also looked at the paper given in the documentation(Andrew W. Fitzgibbon, R.B.Fisher. A Buyer’s Guide to Conic Fitting. Proc.5th British Machine Vision Conference, Birmingham, pp. 513-522, 1995.)

But I cannot understand exactly the algorithm. For example, why does it need to solve 3 times the least square problem? why bd is initialized to 10000 before the first svd(I guess it is juste a random value for the initialization but why this value can be random?)? why does the values in Ad needs to be negative before the first svd?

Thank you!

like image 994
vanpact Avatar asked Feb 07 '13 11:02

vanpact


People also ask

What does fitEllipse return?

The function fitEllipse returns a RotatedRect that contains all the parameters of the ellipse. An ellipse is defined by 5 parameters: xc : x coordinate of the center. yc : y coordinate of the center.

How do you draw an ellipse in Opencv?

Step 6: Set start and end angle. Step 6: Set the color. Step 7: Set the thickness. Step 8: Draw the ellipse by passing the above parameters in the cv2.


Video Answer


1 Answers

Here is Matlab code.. it might help

function [Q,a]=fit_ellipse_fitzgibbon(data)
  % function [Q,a]=fit_ellipse_fitzgibbon(data)
  %
  % Ellipse specific fit, according to:
  %
  %  Direct Least Square Fitting of Ellipses,
  %  A. Fitzgibbon, M. Pilu and R. Fisher. PAMI 1996
  %
  %
  % See Also:
  %   FIT_ELLIPSE_LS
  %   FIT_ELLIPSE_HALIR

  [m,n] = size(data);
  assert((m==2||m==3)&&n>5);
  x = data(1,:)';
  y = data(2,:)';

  D = [x.^2 x.*y y.^2 x y ones(size(x))];   % design matrix
  S = D'*D;                                 % scatter matrix
  C(6,6)=0; C(1,3)=-2; C(2,2)=1; C(3,1)=-2; % constraints matrix
  % solve the generalized eigensystem
  [V,D] = eig(S, C);
  % find the only negative eigenvalue
  [n_r, n_c] = find(D<0 & ~isinf(D));
  if isempty(n_c),
    warning('Error getting the ellipse parameters, will do LS');
    [Q,a] = fit_ellipse_ls(data); %
    return;
  end
  % the parameters
  a = V(:, n_c);
  [A B C D E F] = deal(a(1),a(2),a(3),a(4),a(5),a(6)); % deal is slow!
  Q = [A B/2 D/2; B/2 C E/2; D/2 E/2 F];
end % fit_ellipse_fitzgibbon

Fitzibbon solution has some numerical stability though. See the work of Halir for a solution to this.

It is essentially least squares solution, but specifically designed so that it will produce a valid ellipse, not just any conic.

like image 143
bendervader Avatar answered Nov 23 '22 06:11

bendervader