donnees_aberrantes; n_tests = 1000; % Parametres de l'algorithme RANSAC : n_donnees = length(x_donnees_bruitees); S1 = 2; S2 = 0.5; k_max = floor(nchoosek(n_donnees,3)/n_donnees); parametres = [S1 S2 k_max]; % Estimation du rayon et de la position du centre : [C_estime,R_estime] = RANSAC_3(x_donnees_bruitees,y_donnees_bruitees,parametres,n_tests); % Affichage du cercle estime : x_cercle_estime = C_estime(1)+R_estime*cos(theta_cercle); y_cercle_estime = C_estime(2)+R_estime*sin(theta_cercle); plot(x_cercle_estime([1:end 1]),y_cercle_estime([1:end 1]),'b','LineWidth',3); % Affichage des points conformes au modele : conformes = abs(sqrt((x_donnees_bruitees-C_estime(1)).^2+ ... (y_donnees_bruitees-C_estime(2)).^2)-R_estime)<=S1; plot(x_donnees_bruitees(conformes), ... y_donnees_bruitees(conformes),'b+','MarkerSize',10,'LineWidth',2); lg = legend(' Cercle initial', ... ' Donnees (bruitees + aberrantes)', ... ' Cercle estime', ... ' Donnees conformes', ... 'Location','Best'); function [C_estime,R_estime] = RANSAC_3(x_donnees_bruitees,y_donnees_bruitees,parametres,n_tests) n = length(x_donnees_bruitees); erreur = +inf; for j = 1:parametres(3) j/parametres(3)*100 points_model = randperm(n, 3); [C_model, R_model] = cercle_3_points(x_donnees_bruitees(points_model), y_donnees_bruitees(points_model)); points_candidat = []; for i = setdiff(1:n, points_model) [C_candidat, R_candidat] = estimation(x_donnees_bruitees([points_model i]), y_donnees_bruitees([points_model i]), n_tests); % ma fonction d'erreur ne doit pas être la bonne mais ça marche % a peu près bien ~~ erreur_candidat = mean( abs( sqrt((C_model(1)-x_donnees_bruitees([points_model i])).^2 + (C_model(2)-y_donnees_bruitees([points_model i])).^2 )) - R_model ); if erreur_candidat < parametres(1) points_candidat = [ points_candidat i ]; end end if length(points_candidat)/n > parametres(2) [C_new, R_new] = estimation(x_donnees_bruitees([points_model points_candidat]), y_donnees_bruitees([points_model points_candidat]), n_tests); erreur_new = mean( abs( sqrt((C_model(1)-x_donnees_bruitees([points_model points_candidat])).^2 + (C_model(2)-y_donnees_bruitees([points_model points_candidat])).^2 )) - R_model ); if erreur_new < erreur C_estime = C_new; R_estime = R_new; erreur = erreur_new; end end end end function [C_estime,R_estime] = estimation(x_donnees_bruitees,y_donnees_bruitees,n_tests) G = mean( [ x_donnees_bruitees.' y_donnees_bruitees.' ] ); R_moyen = mean( sqrt((x_donnees_bruitees.'-G(1)).^2 + (y_donnees_bruitees.'-G(2)).^2) ); x = repmat(x_donnees_bruitees.', 1, n_tests, n_tests); y = repmat(y_donnees_bruitees.', 1, n_tests, n_tests); x_rand = repmat((randn(1, n_tests)*R_moyen - R_moyen/2) + G(1), length(x_donnees_bruitees), 1, n_tests); y_rand = repmat((randn(1, n_tests)*R_moyen - R_moyen/2) + G(2), length(y_donnees_bruitees), 1, n_tests); r_rand = randn(1, 1, n_tests)*R_moyen - R_moyen/2; R = repmat(r_rand, length(x_donnees_bruitees), n_tests, 1); dist = (sqrt((x-x_rand).^2 + (y-y_rand).^2) - R).^2; somme = reshape(sum(dist), [n_tests, n_tests]); [min_val, idx] = min(somme(:)); [row, col] = ind2sub(size(somme), idx); C_estime = [ x_rand(1, row ) y_rand(1, row ) ]; R_estime = r_rand(col); end