function [matchedFP] = match_LACC (img1, img2, FP1, FP2, Nhood)
% Funkcia pre parovanie najdenych vyznacnych bodov pomocou funkcie
% vzajomnej korelacie oblasti v okoli tychto bodov

%%  
    
    if (size(FP1,1)) < (size(FP2,1))
        
        matchedFP = zeros(size(FP1,1) , 4);
                
        for i = 1:(size (FP1,1))
            LACC_max = 0;
            if (FP1(i,1)<= Nhood) || ((FP1(i,1)+Nhood)>=size(img1,1)) || (FP1(i,2)<=Nhood) || ((FP1(i,2)+Nhood)>=size(img1,2))
            else
                imgFP1 = img1( (FP1(i,1)-Nhood):(FP1(i,1)+Nhood) , (FP1(i,2)-Nhood):(FP1(i,2)+Nhood) ) ;
                
                for j = 1:(size(FP2,1))
                    if (FP2(j,1)<= Nhood) || ((FP2(j,1)+Nhood)>=size(img2,1)) || (FP2(j,2)<=Nhood) || ((FP2(j,2)+Nhood)>=size(img2,2))
                    else
                       
                        imgFP2 = img2( (FP2(j,1)-Nhood):(FP2(j,1)+Nhood) , (FP2(j,2)-Nhood):(FP2(j,2)+Nhood) ) ;
                        LACC = corr2(imgFP1,imgFP2);

                        if LACC > LACC_max
                            matchedFP2_temp = FP2(j,:);
                            LACC_max = LACC;
                            
                            %Testovacia premenna pre zobrazenie LACC 
                            %pre jednotlive sparovane body
%                             LACC_all(i) = LACC;   
                        
                        end
                        
                    end
                
                end
            matchedFP(i,1:2) = FP1(i,:);
            matchedFP(i,3:4) = matchedFP2_temp;
            
            end
                
        end
        
    else
        
        matchedFP = zeros(size(FP2,1) , 4);
                
        for i = 1:(size (FP2,1))
            LACC_max = 0;
            if (FP2(i,1)<= Nhood) || ((FP2(i,1)+Nhood)>=size(img2,1)) || (FP2(i,2)<=Nhood) || ((FP2(i,2)+Nhood)>=size(img2,2))
            else
                imgFP2 = img2( (FP2(i,1)-Nhood):(FP2(i,1)+Nhood) , (FP2(i,2)-Nhood):(FP2(i,2)+Nhood) ) ;
                
                for j = 1:(size(FP1,1))
                    if (FP1(j,1)<= Nhood) || ((FP1(j,1)+Nhood)>=size(img1,1)) || (FP1(j,2)<=Nhood) || ((FP1(j,2)+Nhood)>=size(img1,2))
                    else
                       
                        imgFP1 = img1( (FP1(j,1)-Nhood):(FP1(j,1)+Nhood) , (FP1(j,2)-Nhood):(FP1(j,2)+Nhood) );
                        LACC = corr2(imgFP2,imgFP1);

                        if LACC > LACC_max
                            matchedFP1_temp = FP1(j,:);
                            LACC_max = LACC;
                            
                            %Testovacia premenna pre zobrazenie LACC 
                            %pre jednotlive sparovane body
%                             LACC_all(i) = LACC;   
                        
                        end
                        
                    end
                
                end
            matchedFP(i,3:4) = FP2(i,:);
            matchedFP(i,1:2) = matchedFP1_temp;
            
            end
                
        end
        
    end
    
end

%% Testovaci kod vo funkcii - 
% nacitavanie segmentov obrazu s doplnenim 0 tam kde obraz nie je,
% testovanie hranic obrazu vlastne

% img2=(img(:,:,4));
% img1=(img(:,:,3));
% FP2 = FP{4};
% FP1 = FP{3};


% if ((FP1(i,1) - Nhood) < 1) && ( (FP1(i,2) - Nhood) < 1 ) %lavy horny roh
%                 imgFP1 (((abs(FP1(i,1)-Nhood))+2):(FP1(i,1)+Nhood) , (((abs(FP1(i,2)-Nhood))+2):(FP2(i,2)+Nhood)))  = img1(1:(FP1(i,1)+Nhood) , 1:(FP1(i,2)+Nhood));
%             elseif ((FP1(i,1) + Nhood) > size(img1,1)) && ( (FP1(i,2) - Nhood) < 1 ) %lavy dolny roh
%                 
%             elseif ((FP1(i,1) + Nhood) > size(img1,1)) && ( (FP1(i,2) + Nhood) > size(img1,2) ) %pravy dolny roh
%                 
%             elseif ((FP1(i,1) - Nhood) < 1) && ( (FP1(i,2) + Nhood) > size(img1,2) ) %pravy horny roh
%                 
%             elseif ((FP1(i,2) - Nhood) < 1) %lavy okraj
%                 
%             elseif ((FP1(i,1) + Nhood) > size(img1,1)) %dolny okraj
%                 
%             elseif ((FP1(i,2) + Nhood) > size(img1,2)) %pravy okraj
%                 
%             elseif ((FP1(i,1) - Nhood) > 1) %horny okraj
%                 
%             else % kdekolvek inde v obraze
%                 imgFP1 = img1( (FP1(i,1)-Nhood):(FP1(i,1)+Nhood) , (FP1(i,2)-Nhood):(FP1(i,2)+Nhood) ) ;
%             end