function [xfloat2, sachovnice] = rucni_registrace2 (rgb1, rgb2, p_fixed, p_move)


% Needed m-files:
%   ImageNorm, jas, MakeChessBoard


            gb1 = rgb1( :, :, 2 );
            gb2 = rgb2( :, :, 2 );
            
            r1 = rgb1( :, :, 1 );
            g1 = rgb1( :, :, 2 );
            b1 = rgb1( :, :, 3 );
            r2 = rgb2( :, :, 1 );
            g2 = rgb2( :, :, 2 );
            b2 = rgb2( :, :, 3 );
            % korekce na nehomogenitu osvetleni - mozno zapoznamkovat
%             gb1 = jas ( double(gb1) ); 
%             gb2 = jas ( double(gb2) ); 


            %% priradim do promenych referencni a registrovany obrazek - musi byt v
            % rozsahu 0-1
            xfloat = ImageNorm2( double(gb1), [0 1]);
            xref = ImageNorm( double(gb2), [0,1] );
%             figure(1)
%             imshow(xfloat)
%             figure(2)
%             imshow(g1)
%             pause
%             xfloat = g1;
%             xref = g2;
%             clear gb1 gb2
            %% vyber korespondujicich bodu
           % [xyinput_out, xybase_out] = cpselect( xfloat, xref, 'Wait', true );

            %% Normalizace polohy bodu
            [nr_ref,nc_ref] = size( xref );
            [nr_fl,nc_fl] = size( xfloat );

          %  p_fixed = xybase_out;
          %  p_move = xyinput_out;

            p_fixed(:,1) = p_fixed(:,1)/nc_fl;
            p_fixed(:,2) = p_fixed(:,2)/nr_fl;

            p_move(:,1) = p_move(:,1)/nc_fl;
            p_move(:,2) = p_move(:,2)/nr_fl;

            %% Vypocet parametru kvadraticke transformace
            % pocet bodu
            [np,ll] = size( p_move );

            % Skladani matic pro reseni
            A = zeros(12*np,12);
            i = 1;
            r1 = zeros(1,6); r2 = zeros(1,6); r3 = zeros(1,6); r4 = zeros(1,6); r5 = zeros(1,6); r6 = zeros(1,6);
            d1 = zeros(1,6);

            for ii = 1:np,

                r1 = r1 + [(p_fixed(ii,1)^2), p_fixed(ii,1)*p_fixed(ii,2), (p_fixed(ii,2)^2), p_fixed(ii,1), p_fixed(ii,2), 1]*p_fixed(ii,1)^2;
                r2 = r2 + [(p_fixed(ii,1)^2), p_fixed(ii,1)*p_fixed(ii,2), (p_fixed(ii,2)^2), p_fixed(ii,1), p_fixed(ii,2), 1]*p_fixed(ii,1)*p_fixed(ii,2);
                r3 = r3 + [(p_fixed(ii,1)^2), p_fixed(ii,1)*p_fixed(ii,2), (p_fixed(ii,2)^2), p_fixed(ii,1), p_fixed(ii,2), 1]*p_fixed(ii,2)^2;
                r4 = r4 + [(p_fixed(ii,1)^2), p_fixed(ii,1)*p_fixed(ii,2), (p_fixed(ii,2)^2), p_fixed(ii,1), p_fixed(ii,2), 1]*p_fixed(ii,1);
                r5 = r5 + [(p_fixed(ii,1)^2), p_fixed(ii,1)*p_fixed(ii,2), (p_fixed(ii,2)^2), p_fixed(ii,1), p_fixed(ii,2), 1]*p_fixed(ii,2);
                r6 = r6 + [(p_fixed(ii,1)^2), p_fixed(ii,1)*p_fixed(ii,2), (p_fixed(ii,2)^2), p_fixed(ii,1), p_fixed(ii,2), 1];

                d1 = d1 + p_move(ii,1)*[p_fixed(ii,1)^2, p_fixed(ii,1)*p_fixed(ii,2), p_fixed(ii,2)^2, p_fixed(ii,1), p_fixed(ii,2), 1];
            end
            M1 = [r1;r2;r3;r4;r5;r6];

            r1 = zeros(1,6); r2 = zeros(1,6); r3 = zeros(1,6); r4 = zeros(1,6); r5 = zeros(1,6); r6 = zeros(1,6);
            d2 = zeros(1,6);

            for ii = 1:np,

                r1 = r1 + [(p_fixed(ii,1)^2), p_fixed(ii,1)*p_fixed(ii,2), (p_fixed(ii,2)^2), p_fixed(ii,1), p_fixed(ii,2), 1]*p_fixed(ii,1)^2;
                r2 = r2 + [(p_fixed(ii,1)^2), p_fixed(ii,1)*p_fixed(ii,2), (p_fixed(ii,2)^2), p_fixed(ii,1), p_fixed(ii,2), 1]*p_fixed(ii,1)*p_fixed(ii,2);
                r3 = r3 + [(p_fixed(ii,1)^2), p_fixed(ii,1)*p_fixed(ii,2), (p_fixed(ii,2)^2), p_fixed(ii,1), p_fixed(ii,2), 1]*p_fixed(ii,2)^2;
                r4 = r4 + [(p_fixed(ii,1)^2), p_fixed(ii,1)*p_fixed(ii,2), (p_fixed(ii,2)^2), p_fixed(ii,1), p_fixed(ii,2), 1]*p_fixed(ii,1);
                r5 = r5 + [(p_fixed(ii,1)^2), p_fixed(ii,1)*p_fixed(ii,2), (p_fixed(ii,2)^2), p_fixed(ii,1), p_fixed(ii,2), 1]*p_fixed(ii,2);
                r6 = r6 + [(p_fixed(ii,1)^2), p_fixed(ii,1)*p_fixed(ii,2), (p_fixed(ii,2)^2), p_fixed(ii,1), p_fixed(ii,2), 1];

            %     d2 = d2 + [p_move(ii,2)*p_fixed(ii,1)^2, p_fixed(ii,2)*p_fixed(ii,1)*p_fixed(ii,2), p_fixed(ii,2)*p_fixed(ii,2)^2, p_fixed(ii,2)*p_fixed(ii,1), p_fixed(ii,2)*p_fixed(ii,2),p_fixed(ii,2)];
                d2 = d2 + p_move(ii,2)*[p_fixed(ii,1)^2, p_fixed(ii,1)*p_fixed(ii,2), p_fixed(ii,2)^2, p_fixed(ii,1), p_fixed(ii,2), 1];
            end
            M2 = [r1;r2;r3;r4;r5;r6];

            M = [M1 zeros(size(M1));zeros(size(M1)) M2];
            d = [d1(:);d2(:)];

            %% reseni rovnic
            % M = inv(M);
            x = M\d;
            Mout = M;

            %% vysledna transformacni matice 
            M = [x(1:6)';x(7:12)'];

            %% Tady, do Xnew a Ynew, ulozime kam se transformuji souradnice
            [X,Y]=meshgrid( linspace(0,1,nc_fl), linspace(0,1,nr_fl) );

            Xnew = zeros( nr_fl, nc_fl );
            Ynew = zeros( nr_fl, nc_fl );
            for r = 1:nr_fl,
                 for c = 1:nc_fl
                   Xnew(r,c) = M(1,1)*X(r,c)^2+M(1,2)*X(r,c)*Y(r,c)+M(1,3)*Y(r,c)^2+M(1,4)*X(r,c)+M(1,5)*Y(r,c)+M(1,6);
                   Ynew(r,c) = M(2,1)*X(r,c)^2+M(2,2)*X(r,c)*Y(r,c)+M(2,3)*Y(r,c)^2+M(2,4)*X(r,c)+M(2,5)*Y(r,c)+M(2,6);
                 end
            end

            %% A interpoluje se
            xfloat2= interp2( X, Y, ImageNorm(xfloat,[1 256]), Xnew, Ynew, 'linear', 50 );
            xfloat2 = ImageNorm( double( xfloat2 ) );
            xfloat2 = xfloat2(1:nr_ref, 1:nc_ref);

            %% ted by bylo dobry ulozit: transformacni matici, zadane kontrolni body a
            %% registrovany obrazek. Ukladat to vcetne nazvu OCT nebo fundus souboru
            %save reg_param.mat M xyinput_out xybase_out xfloat2

            %imwrite(xfloat2,['databaze/' cell2mat(txt(1,i)) '/' cell2mat(txt(j,i)) '_rucni.JPG']);
            % tady lze pouzit imwrite pro ulozeni zregistrovaneho snimku (xfloat2) 
            %% vykresleni
%             figure(1);
%             imshow(xfloat2,[]);
%             title('Registered');
% 
%             figure(2);
%             imshow(xref,[])
%             title('Reference');



            %% evaluace
            sachovnice = MakeChessBoard( xref, xfloat2, 150 );   
            diferencni = abs( double(xfloat2) - double(xref) );
            
            %imwrite(sachovnice,['databaze/vysledky_rucni/' cell2mat(txt(j,i)) '.JPG']);
            
%             figure(3);
%             imshow(sachovnice,[])
%             figure(4);
%             imshow(diferencni,[])

            % % %% kontrola prepoctu bodu do originalniho obrazku - toto vyuzijes pri
            % % % prepoctu souradnic z registrovaneho snimku do originalniho fundus snimku
            % % 
            % % % oznaceni bodu v registrovanem snimku
            % % figure
            % % imshow( xfloat2 ); hold on;
            % % [col,row] = ginput(1);
            % % col = col/nc_fl;
            % % row = row/nr_fl;
            % % 
            % % plot(col,row,'r*');
            % % 
            % % % prepocet bodu
            % % COLnew = M(1,1)*col^2+M(1,2)*row*col+M(1,3)*row^2+M(1,4)*col+M(1,5)*row+M(1,6);
            % % ROWnew = M(2,1)*col^2+M(2,2)*row*col+M(2,3)*row^2+M(2,4)*col+M(2,5)*row+M(2,6);
            % % 
            % % % dvojka je tam proto, ze se na zacatku decimuje
            % % ROWnew = 2*ROWnew*nr_fl;
            % % COLnew = 2*COLnew*nc_fl;
            % % 
            % % figure
            % % imshow( double(rgb(:,:,2)), [] ); hold on;
            % % plot( COLnew, ROWnew,'r*');

            
            
        end
