function [M2,N2]=transformation_image(X,Y,xx,yy,img,imd,nbpoints,m,n,haut,long,correc)
%correction rigide%
%%%%%%%%%%%%%%%%%%%
if correc==1
    xbarref=sum(xx)/nbpoints;ybarref=sum(yy)/nbpoints;
    xbardef=sum(X)/nbpoints;ybardef=sum(Y)/nbpoints;

    Xcor = X-xbardef;
    Ycor = Y-ybardef;
    x2 = xx-xbarref;
    y2 = yy-ybarref;

    tantheta=(Ycor'.*x2'-y2'.*Xcor')/(Xcor'.*x2'+Ycor'.*y2');
    t = -atan(tantheta);
    co = cos(t);
    si = sin(t);
    [haut long] = size(img);
    [m n] = meshgrid(1:long,1:haut);

    M2 = (m-xbardef)*co-(n-ybardef)*si+xbarref;
    N2 = (m-xbardef)*si+(n-ybardef)*co+ybarref;

    %correction affine%
    %%%%%%%%%%%%%%%%%%%
elseif correc==2

    A=[sum(xx.*xx) sum(xx.*yy) sum(xx) ;
        sum(yy.*xx) sum(yy.*yy) sum(yy) ;
        sum(xx) sum(yy) nbpoints];
    B=[sum(xx.*X);
        sum(yy.*X);
        sum(X)];
    coeffabc=inv(A)*B;
    C=[sum(xx.*xx) sum(xx.*yy) sum(xx) ;
        sum(yy.*xx) sum(yy.*yy) sum(yy) ;
        sum(xx) sum(yy) nbpoints];
    D=[sum(xx.*Y);
        sum(yy.*Y);
        sum(Y)];
    coeffdef=inv(C)*D;
    a=coeffabc(1,1);
    b=coeffabc(2,1);
    c=coeffabc(3,1);
    d=coeffdef(1,1);
    e=coeffdef(2,1);
    f=coeffdef(3,1);
    [haut long] = size(img);
    [m n] = meshgrid(1:long,1:haut);

    M2=(e*(m-c)-b*(n-f))/(e*a-d*b);
    N2=(d*(m-c)-a*(n-f))/(d*b-a*e);

    %correction quadratique%
    %%%%%%%%%%%%%%%%%%%%%%%%
else

    if nbpoints<6
        disp('pas assez de points, 6 minimum ')
        correc=2
    end
    tata=[ones(size(X)) X Y X.*Y X.^2 Y.^2];
    a=tata\xx;
    b=tata\yy;
    [haut long] = size(img);
    [m n] = meshgrid(1:long,1:haut);
    M2=a(1,1)+a(2,1)*m+a(3,1)*n+a(4,1)*m.*n+a(5,1)*m.*m+a(6,1)*n.*n;
    N2=b(1,1)+b(2,1)*m+b(3,1)*n+b(4,1)*m.*n+b(5,1)*m.*m+b(6,1)*n.*n;
end
