Got Questions? Get Answers.
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:
Elliptical Regression -- Fit an Ellipse to Data Points

Subject: Elliptical Regression -- Fit an Ellipse to Data Points

From: Carlos Junior

Date: 28 Jul, 2011 18:13:29

Message: 1 of 1

I am implementing an algorithm to find an ellipse at a cloud of data points.
I am trying to implement this algorithm, that seem to work well but is not in MatLab format: http://www.nlreg.com/ellipse.htm

Does anyone would like to discuss or exchange ideas about that implementation?

My code is written below. I cannot continue because the command solve is to lazy...

----------------------------------------------------------------------
%NLREG
clear
clc
name = 'cloud_of_points.png' ;
Im = imread( name ) ;
Im = rgb2gray( Im ) ;
Im = 255 - Im ;
[ Y , X ] = find( Im ) ;
DATA = [ X' ; Y' ] ;

% Parameters
CG = mean(DATA',1) ;
Xcenter = CG(1) ; %X coordinate of center of ellipse
Ycenter = CG(2) ; %Y coordinate of center of ellipse
TiltAngle = 0 ; %Rotation of ellipse in counter-clockwise direction (radians)

DataAngle = atan( Y - ones(length(Y),1) * Ycenter ./ X - ones(length(X),1) * Xcenter ) ;
DataDistance = sqrt( ( X - ones(length(X),1) * Xcenter ).^2 + ( Y - ones(length(Y),1) * Ycenter ).^2 ) ;

% Compute the angle for the point on the ellipse with the tilt angle.
Angle = DataAngle - ones(length(DataAngle),1) * TiltAngle ;

% Compute the radius of the ellipse (distance from center to perimeter) for
% this data angle. (Uses polar coordinate equation for an ellipse.)
syms Ydim Xdim
r = sqrt( ( (ones(length(Angle),1) * Xdim).^2 .* (ones(length(Angle),1) * Ydim).^2 ) ./ ( ( ones(length(Angle),1) * Xdim .* sin(Angle) ).^2 + ( ones(length(Angle),1) * Ydim .* cos(Angle) ).^2 ) ) ;

%Compute the difference between the distance for the data point and the ellipse.
Deviation = DataDistance - r ;

%Minimize the sum of squared deviations.
Deviation_quadrado = Deviation.^2 ;

i_final = length( Deviation_quadrado ) ;
S = 0 ;
for i = 1 : i_final
    S = S + Deviation_quadrado(i,1) ;
end

Eq1 = diff( S , Xdim ) ;
Eq2 = diff( S , Ydim ) ;

[ Xdim , Ydim ] = solve( Eq1 , Eq2 )
----------------------------------------------------------------------

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