WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

Ellipse in einem Bild fitten

 

kenchet
Forum-Anfänger

Forum-Anfänger


Beiträge: 15
Anmeldedatum: 06.05.10
Wohnort: Eindhoven
Version: MATLAB 7.10.0 R2010a
     Beitrag Verfasst am: 28.06.2010, 21:02     Titel: Ellipse in einem Bild fitten
  Antworten mit Zitat      
Hallo Leute,

ich habe Bilder einer mehr oder weniger ellipsoiden Form. Ich will ein Fit mit einer Ellipse machen. Wie das mit einem Kreis geht weiss ich bereits, aber das mit der Ellipse funktioniert leider nicht. Ich will ausserdem die geometrischen Daten der Ellipse (Breite und Hoehe wissen) Kann mir da jemand bitte helfen?

Das bisherige Skript mit dem Kreis fit ist hier

Code:

RGB = imread('090812 26124A Nuc Posterior S0070 P2 N0000.jpg');
imshow(RGB);

BW = im2bw(RGB,0.2);
imshow(BW)


dim = size(BW);

%col = round(dim(2)/2)-12;
col = round(dim(2)/2)-712;

row = find(BW(:,col), 1);

connectivity = 8;
num_points   = 1545;
%num_points   = 45;
contour = bwtraceboundary(BW, [row, col], 'N', connectivity, num_points);

%imshow(RGB);
hold on;

plot(contour(:,2),contour(:,1),'r','LineWidth',2);


x = contour(:,2);
y = contour(:,1);

% solve for parameters a, b, and c in the least-squares sense by
% using the backslash operator
abc = [x y ones(length(x),1)] \ -(x.^2+y.^2);
a = abc(1); b = abc(2); c = abc(3);

% calculate the location of the center and the radius
xc = -a/2;
yc = -b/2;
radius  =  sqrt((xc^2+yc^2)-c)

% display the calculated center
plot(xc,yc,'rx','LineWidth',2);

% plot the entire circle
theta = 0:0.01:2*pi;

% use parametric representation of the circle to obtain coordinates
% of points on the circle
Xfit = radius*cos(theta) + xc;
Yfit = radius*sin(theta) + yc;

plot(Xfit, Yfit);

message = sprintf('The estimated radius is %2.3f pixels', radius);
text(15,15,message,'Color','r','FontWeight','bold');
 


edit by Maddy: Code-Umgebung hinzugefügt
Private Nachricht senden Benutzer-Profile anzeigen


Maddy
Ehrenmitglied

Ehrenmitglied



Beiträge: 494
Anmeldedatum: 02.10.08
Wohnort: Greifswald
Version: ---
     Beitrag Verfasst am: 29.06.2010, 12:32     Titel:
  Antworten mit Zitat      
Was verstehst du unter Bildern mit elliptischer Form?

Als ich das gestern nacht gelesen habe, musste ich spontan an den Heydemann-Algorithmus denken. Wird in der Interferometrie verwendet und fittet ziemlich gut in Daten (x,y) die entsprechende Funktion (z.B. Ellipse).
_________________

>> why
The computer did it.
Private Nachricht senden Benutzer-Profile anzeigen
 
kenchet
Themenstarter

Forum-Anfänger

Forum-Anfänger


Beiträge: 15
Anmeldedatum: 06.05.10
Wohnort: Eindhoven
Version: MATLAB 7.10.0 R2010a
     Beitrag Verfasst am: 29.06.2010, 13:14     Titel:
  Antworten mit Zitat      
also ich meine Bilder von einem ellipsoiden Objekt. Eine Funktion zum Fitten habe ich nun. Aber ich habe das Problem, dass mein Programm nur die obere Haelfte meiner Ellipse findet, untere Haelfte leider nicht, daher funktioniert das Fitten auch nicht. Momentan sieht der Code folgendermassen aus

Code:

RGB = imread('090812 26124A Nuc Posterior S0070 P2 N0000.jpg');
imshow(RGB);

BW = im2bw(RGB,0.2);
imshow(BW)


dim = size(BW);

%col = round(dim(2)/2)-12;
col = round(dim(2)/2)-712;
row = find(BW(:,col), 1);

connectivity = 8;
num_points   = 1545;
%num_points   = 45;
contour = bwtraceboundary(BW, [row, col], 'N', connectivity, num_points);

%imshow(RGB);
hold on;

plot(contour(:,2),contour(:,1),'r','LineWidth',2);


x = contour(:,2);
y = contour(:,1);

BW = ~im2bw(RGB,0.2);

col2 = round(dim(2)/2)-7;
row2 = find(BW(dim(1)/2:dim(1),1));

connectivity2 = 8;
num_points2   = 15;
contour2 = bwtraceboundary(BW, [row2, col2], 'S', connectivity2, num_points2);

plot(contour2(:,2),contour2(:,1),'r','LineWidth',2);

x = x(:);
y = y(:);

% remove bias of the ellipse - to make matrix inversion more accurate. (will be added later on).
mean_x = mean(x);
mean_y = mean(y);
x = x-mean_x;
y = y-mean_y;

% the estimation for the conic equation of the ellipse
X = [x.^2, x.*y, y.^2, x, y ];
a = sum(X)/(X'*X);

% check for warnings
if ~isempty( lastwarn )
    disp( 'stopped because of a warning regarding matrix inversion' );
    ellipse_t = [];
    return
end

% extract parameters from the conic equation
[a,b,c,d,e] = deal( a(1),a(2),a(3),a(4),a(5) );

% remove the orientation from the ellipse
if ( min(abs(b/a),abs(b/c)) > orientation_tolerance )
   
    orientation_rad = 1/2 * atan( b/(c-a) );
    cos_phi = cos( orientation_rad );
    sin_phi = sin( orientation_rad );
    [a,b,c,d,e] = deal(...
        a*cos_phi^2 - b*cos_phi*sin_phi + c*sin_phi^2,...
        0,...
        a*sin_phi^2 + b*cos_phi*sin_phi + c*cos_phi^2,...
        d*cos_phi - e*sin_phi,...
        d*sin_phi + e*cos_phi );
    [mean_x,mean_y] = deal( ...
        cos_phi*mean_x - sin_phi*mean_y,...
        sin_phi*mean_x + cos_phi*mean_y );
else
    orientation_rad = 0;
    cos_phi = cos( orientation_rad );
    sin_phi = sin( orientation_rad );
end

% check if conic equation represents an ellipse
test = a*c;
switch (1)
case (test>0),  status = '';
case (test==0), status = 'Parabola found';  warning( 'fit_ellipse: Did not locate an ellipse' );
case (test<0),  status = 'Hyperbola found'; warning( 'fit_ellipse: Did not locate an ellipse' );
end

% if we found an ellipse return it's data
if (test>0)
   
    % make sure coefficients are positive as required
    if (a<0), [a,c,d,e] = deal( -a,-c,-d,-e ); end
   
    % final ellipse parameters
    X0          = mean_x - d/2/a;
    Y0          = mean_y - e/2/c;
    F           = 1 + (d^2)/(4*a) + (e^2)/(4*c);
    [a,b]       = deal( sqrt( F/a ),sqrt( F/c ) );    
    long_axis   = 2*max(a,b);
    short_axis  = 2*min(a,b);

    % rotate the axes backwards to find the center point of the original TILTED ellipse
    R           = [ cos_phi sin_phi; -sin_phi cos_phi ];
    P_in        = R * [X0;Y0];
    X0_in       = P_in(1);
    Y0_in       = P_in(2);
   
    % pack ellipse into a structure
    ellipse_t = struct( ...
        'a',a,...
        'b',b,...
        'phi',orientation_rad,...
        'X0',X0,...
        'Y0',Y0,...
        'X0_in',X0_in,...
        'Y0_in',Y0_in,...
        'long_axis',long_axis,...
        'short_axis',short_axis,...
        'status','' );
else
    % report an empty structure
    ellipse_t = struct( ...
        'a',[],...
        'b',[],...
        'phi',[],...
        'X0',[],...
        'Y0',[],...
        'X0_in',[],...
        'Y0_in',[],...
        'long_axis',[],...
        'short_axis',[],...
        'status',status );
end

% check if we need to plot an ellipse with it's axes.
if (nargin>2) & ~isempty( axis_handle ) & (test>0)
   
    % rotation matrix to rotate the axes with respect to an angle phi
    R = [ cos_phi sin_phi; -sin_phi cos_phi ];
   
    % the axes
    ver_line        = [ [X0 X0]; Y0+b*[-1 1] ];
    horz_line       = [ X0+a*[-1 1]; [Y0 Y0] ];
    new_ver_line    = R*ver_line;
    new_horz_line   = R*horz_line;
   
    % the ellipse
    theta_r         = linspace(0,2*pi);
    ellipse_x_r     = X0 + a*cos( theta_r );
    ellipse_y_r     = Y0 + b*sin( theta_r );
    rotated_ellipse = R * [ellipse_x_r;ellipse_y_r];
   
    % draw
    hold_state = get( axis_handle,'NextPlot' );
    set( axis_handle,'NextPlot','add' );
    plot( new_ver_line(1,:),new_ver_line(2,:),'r' );
    plot( new_horz_line(1,:),new_horz_line(2,:),'r' );
    plot( rotated_ellipse(1,:),rotated_ellipse(2,:),'r' );
    set( axis_handle,'NextPlot',hold_state );
end


edit by Maddy: Code-Umgebung hinzugefügt
Private Nachricht senden Benutzer-Profile anzeigen
 
kenchet
Themenstarter

Forum-Anfänger

Forum-Anfänger


Beiträge: 15
Anmeldedatum: 06.05.10
Wohnort: Eindhoven
Version: MATLAB 7.10.0 R2010a
     Beitrag Verfasst am: 29.06.2010, 15:22     Titel:
  Antworten mit Zitat      
also momentan sieht es so aus, anstatt eines Kreises will ich eine Ellipse fitten und Breite und Hoehe messen.

BW.pdf
 Beschreibung:

Download
 Dateiname:  BW.pdf
 Dateigröße:  262.71 KB
 Heruntergeladen:  625 mal
Private Nachricht senden Benutzer-Profile anzeigen
 
kenchet
Themenstarter

Forum-Anfänger

Forum-Anfänger


Beiträge: 15
Anmeldedatum: 06.05.10
Wohnort: Eindhoven
Version: MATLAB 7.10.0 R2010a
     Beitrag Verfasst am: 29.06.2010, 16:50     Titel:
  Antworten mit Zitat      
das Problem mit der unteren Ellipsenhaelfe ist geloest, aber ich schaffe es grade nicht die Ellipse zu fitten. Bekomme immer ein "out of memory" als Fehler. Was kann ich da machen?
Private Nachricht senden Benutzer-Profile anzeigen
 
kenchet
Themenstarter

Forum-Anfänger

Forum-Anfänger


Beiträge: 15
Anmeldedatum: 06.05.10
Wohnort: Eindhoven
Version: MATLAB 7.10.0 R2010a
     Beitrag Verfasst am: 30.06.2010, 15:48     Titel:
  Antworten mit Zitat      
alles funktioniert nun so wie es soll...

Code:

RGB = imread('Object');
imshow(RGB);

BW = im2bw(RGB,0.2);
imshow(BW)

dim = size(BW);
%% Contour1 upper Curve (usually anterior pole)
%col = round(dim(2)/2)-12;
col = round(dim(2)/2)-752;
row = find(BW(:,col), 1);

% col2 = round(dim(2)/2)-7;
% row2 = find(BW(round(dim(1)/2):round(dim(1)),col2),1);

connectivity = 8;
num_points   = 1545;
%num_points   = 45;
contour = bwtraceboundary(BW, [row, col], 'N', connectivity, num_points);

%imshow(RGB);
hold on;

plot(contour(:,2),contour(:,1),'r','LineWidth',2);


% BW = ~im2bw(RGB,0.2);
%% Contour2 lower right Curve (usually posterior pole)
col2 = round(dim(2)/2)+522;
row2 = find(~BW(round(dim(1)/2):1:round(dim(1)),col2),1);
row2 = row2 + round(dim(1)/2)-1;

connectivity2 = 8;
num_points2   = 245;
contour2 = bwtraceboundary(BW, [row2-1, col2], 'N', connectivity2, num_points2);

plot(contour2(:,2),contour2(:,1),'r','LineWidth',2);

%% Contour3 lower left Curve (usually posterior pole)
col3 = round(dim(2)/2)-352;
row3 = find(~BW(round(dim(1)/2):1:round(dim(1)),col3),1);
row3 = row3 + round(dim(1)/2)-1;

connectivity3 = 8;
num_points3   = 245;
contour3 = bwtraceboundary(BW, [row3-1, col3], 'N', connectivity3, num_points3);

plot(contour3(:,2),contour3(:,1),'r','LineWidth',2);

%% Contour4 left curve of equator
row4 = round(dim(1)/2+150);
col4 = find (BW(row4,:),1);

connectivity4 = 8;
num_points4   = 300;
contour4 = bwtraceboundary(BW, [row4, col4], 'W', connectivity4, num_points4);

plot(contour4(:,2),contour4(:,1),'r','LineWidth',2);

%% Contour5 right curve of equator
row5 = round(dim(1)/2-150);
col5 = find (~BW(row5,round(dim(2)/2):1:dim(2)),1);
col5 = col5 + round(dim(2)/2);

connectivity5 = 8;
num_points5   = 300;
contour5 = bwtraceboundary(BW, [row5, col5-2], 'W', connectivity5, num_points5);

plot(contour5(:,2),contour5(:,1),'r','LineWidth',2);

%% Ellipse
x = [contour(:,2) ;contour2(:,2);contour3(:,2);contour4(:,2);contour5(:,2)];
y = [contour(:,1) ;contour2(:,1);contour3(:,1);contour4(:,1);contour5(:,1)];
x = transpose(x);
y = transpose(y);

y51s70p1 = fit_ellipse( x,y)
Private Nachricht senden Benutzer-Profile anzeigen
 
Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

Du kannst Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum posten
Du kannst Dateien in diesem Forum herunterladen
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2025 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.