function [ output ] = final_function( input )
%FINAL_FUNCTION 
%	output = FINAL_FUNCTION(vstupní obraz)
% 
%	Funkce pro nalezení krajnic a jejich aproximace přímkou
% 
%	output je struktura:
%         output.kl.k   smernice přímky (levá krajnice)
%         output.kl.q   konstanta přímky (levá krajnice)
% 
%         output.za.k   smernice přímky (pravé krajnice)
%         output.za.q   konstanta přímky (pravá krajnice)
% 
%         output.kl.u   uhel
%         output.kl.x   souřadnice x
%         output.kl.y   souřadnice y
% 
%         output.za.u   uhel
%         output.za.x   souřadnice x
%         output.za.y   souřadnice y
%
%   See also HSV_FILTER, LAB_FILTER, EDGE_FILTER.

%% primky


img_tmp=input;
input = im2bw(input,0.7);
[a,b]=  size(input);
stred = a/2;
a=a-50;
souradnice_kladne=zeros(a,2);
souradnice_zaporne=zeros(a,2);


for j=a:-1:90
    for i=stred:1:b
        if(input(j,i)==1)
            if(i==stred)
                stred=stred+10;
                break;
            end
            souradnice_kladne(j,:)=[i,j];
            break;
            
        end
    end
    
    
    
end

stred = a/2;
for j=a:-1:90
    for i=stred:-1:1
        if(input(j,i)==1)
            if(i==stred)
                stred=stred-10;
                break;
            end
            souradnice_zaporne(j,:)=[i,j];
            break;
            
        end
    end
end










%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

uhel2_kl=[0 0 0];
uhel_kl=[0 0 0];
uhel2_za=[0 0 0];
uhel_za=[0 0 0];

[k l]=size(souradnice_kladne);
uhel_kl =zeros(1,3);
tmp=1;
for i=1:1:k
    if(souradnice_kladne(i,1)~=0 && souradnice_kladne(i,1)~=b)
        uh=acot(souradnice_kladne(i,2)/souradnice_kladne(i,1))*(180/pi);
        if((~isnan(uh))&&(uh~=0)&&(uh~=90))
            uhel_kl(tmp,1)=uh;
            uhel_kl(tmp,2)=souradnice_kladne(i,1);
            uhel_kl(tmp,3)=souradnice_kladne(i,2);
            tmp=tmp+1;
        end
    end
end

med = median(uhel_kl(:,1));

[k l]=size(uhel_kl);
tmp=1;
for i=1:1:k
    if(uhel_kl(i,1)>=med*0.98 && uhel_kl(i,1)<=med*1.02)
        uhel2_kl(tmp,:)=uhel_kl(i,:);
        tmp=tmp+1;
    end
    
end

[k l]=size(souradnice_zaporne);
uhel_za =zeros(1,3);
tmp=1;
for i=1:1:k
    if(souradnice_zaporne(i,1)~=0 && souradnice_zaporne(i,1)~=1)
        uh=acot(souradnice_zaporne(i,2)/(320-souradnice_zaporne(i,1)))*(180/pi);
        if((~isnan(uh))&&(uh~=0)&&(uh~=90))
            uhel_za(tmp,1)=uh;
            uhel_za(tmp,2)=souradnice_zaporne(i,1);
            uhel_za(tmp,3)=souradnice_zaporne(i,2);
            tmp=tmp+1;
        end
    end
end

med = median(uhel_za(:,1));

[k l]=size(uhel_za);
tmp=1;
for i=1:1:k
    if(uhel_za(i,1)>=med*0.98 && uhel_za(i,1)<=med*1.02)
        uhel2_za(tmp,:)=uhel_za(i,:);
        tmp=tmp+1;
    end
    
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%///////////////////////////////////////////////////////////////////////

[g h]= size(uhel2_kl);
size_kl=g;
[g h]= size(uhel2_za);
size_za=g;



if(size_kl<10 || size_za<10)
    input = edge(img_tmp,'log');
    input = bwareaopen(input,50);
end
    
if(size_kl<10)
    uhel2_kl=[0 0 0];
    uhel_kl=[0 0 0];
end
if(size_za<10)
    uhel2_za=[0 0 0];
    uhel_za=[0 0 0];
end


stred = a/2;
if(size_kl<10)
    for j=a:-1:90
        for i=stred:1:b
            if(input(j,i)==1)
                if(i==stred)
                    stred=stred+10;
                    break;
                end
                souradnice_kladne(j,:)=[i,j];
                break;
                
            end
        end
        
        
        
    end
end


stred = a/2;
if(size_za<10)
    for j=a:-1:90
        for i=stred:-1:1
            if(input(j,i)==1)
                if(i==stred)
                    stred=stred-10;
                    break;
                end
                souradnice_zaporne(j,:)=[i,j];
                break;
                
            end
        end
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


[k l]=size(souradnice_kladne);
uhel_kl =zeros(1,3);
tmp=1;
for i=1:1:k
    if(souradnice_kladne(i,1)~=0 && souradnice_kladne(i,1)~=b)
        uh=acot(souradnice_kladne(i,2)/souradnice_kladne(i,1))*(180/pi);
        if((~isnan(uh))&&(uh~=0)&&(uh~=90))
            uhel_kl(tmp,1)=uh;
            uhel_kl(tmp,2)=souradnice_kladne(i,1);
            uhel_kl(tmp,3)=souradnice_kladne(i,2);
            tmp=tmp+1;
        end
    end
end

med = median(uhel_kl(:,1));

[k l]=size(uhel_kl);
tmp=1;
for i=1:1:k
    if(uhel_kl(i,1)>=med*0.98 && uhel_kl(i,1)<=med*1.02)
        uhel2_kl(tmp,:)=uhel_kl(i,:);
        tmp=tmp+1;
    end
end

[k l]=size(souradnice_zaporne);
uhel_za =zeros(1,3);
tmp=1;
for i=1:1:k
    if(souradnice_zaporne(i,1)~=0 && souradnice_zaporne(i,1)~=1)
        uh=acot(souradnice_zaporne(i,2)/(320-souradnice_zaporne(i,1)))*(180/pi);
        if((~isnan(uh))&&(uh~=0)&&(uh~=90))
            uhel_za(tmp,1)=uh;
            uhel_za(tmp,2)=souradnice_zaporne(i,1);
            uhel_za(tmp,3)=souradnice_zaporne(i,2);
            tmp=tmp+1;
        end
    end
end

med = median(uhel_za(:,1));

[k l]=size(uhel_za);
tmp=1;
for i=1:1:k
    if(uhel_za(i,1)>=med*0.98 && uhel_za(i,1)<=med*1.02)
        uhel2_za(tmp,:)=uhel_za(i,:);
        tmp=tmp+1;
    end
    
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x_val = [ones(length(uhel2_kl(:,3)),1),uhel2_kl(:,3)];
[A B] = regress(uhel2_kl(:,2),x_val);

output.kl.k=A(2);
output.kl.q=A(1);

x_val = [ones(length(uhel2_za(:,3)),1),uhel2_za(:,3)];
[A B] = regress(uhel2_za(:,2),x_val);


output.za.k=A(2);
output.za.q=A(1);

output.kl.u=uhel2_kl(:,1);
output.kl.x=uhel2_kl(:,2);
output.kl.y=uhel2_kl(:,3);

output.za.u=uhel2_za(:,1);
output.za.x=uhel2_za(:,2);
output.za.y=uhel2_za(:,3);



end

