clc
clear all
close all
warning off all

[num txt] = xlsread('seznam_registrace2');

rozsah = size(txt);

cesta_koresp_sourad = 'c:\Users\Michal\Documents\VUTBR\Magistr\Diplomová práce\Programy\Hledání korespondenčních značek\Koresp_souradnice_norm\';
cesta_obr_reg = 'c:\Users\Michal\Documents\VUTBR\Magistr\Oči\registrovane_interp2\';
cesta_vysl = 'c:\Users\Michal\Documents\VUTBR\Magistr\Diplomová práce\Programy\Hledání korespondenčních značek\Vykreslene_korespondence\';
start = 1;
vse = 0;
for i = start:rozsah(2)
    for j = 2:rozsah(1)
        if length(cell2mat(txt(j,i))) == 0

            continue
        else
        vse = vse + 1;          

        end
    end
end
count = 0;
nul = 0;
m = 1;

for i = start:rozsah(2)
    pp = 0;
    for j = 3:rozsah(1)
        if length(cell2mat(txt(j,i))) == 0
            nul = nul + 1;
            continue
        else
            pp = pp + 1;
            clc
            count = count+1;
            disp('Hotovo [%]')
            disp(count/vse*100)
            
            soubor1 = cell2mat(txt(2,i));
            soubor2 = cell2mat(txt(j,i))
            cesta_obr = cell2mat(txt(1,i));
            
            
            obr1 = imread([cesta_obr '' soubor1 '' '.jpg']);
            obr2 = imread([cesta_obr_reg '' soubor2 '' '.bmp']);

            obr1 = imresize(obr1,[1000 1500]);
            obr2 = imresize(obr2,[1000 1500]);
            
            load([cesta_koresp_sourad '' soubor1 '' '_korespondence.mat'])
            sourad1 = koresp_sourad_vse(m:m+1,:);        
            sourad2 = koresp_sourad_vse(m+2:m+3,:);
            D = koresp_sourad_vse(m+4,:);
            D(find (D==0))=[];
            sourad1(:,find (sourad1(1,:)==0))=[];
            sourad2(:,find (sourad2(1,:)==0))=[];
            m = m+5;
            D2 = D;
            prah_nastav = 0.5;
            for p = 0.15:0.01:prah_nastav
                D(D>p) = [];
                if length(D)>18 
                    prah = p;
                    break
                else
                    prah = prah_nastav;
                end
                D = D2;
            end
            %%
            disp(['Práh: ' '' num2str(prah)])
            sourad1(:,find (D>prah))=[];
            sourad2(:,find (D>prah))=[];
            D(D>prah)=[];
            
%             sourad1(:,15) = [];
%             sourad2(:,15) = [];
%             D(15) = [];
            obr_g11 = double(obr1(:,:,2))/double(max(max(obr1(:,:,2))));
            obr_g22 = double(obr2(:,:,2))/double(max(max(obr2(:,:,2))));
            for k = 1:length(sourad1)
                s1 = sourad1(:,k);
                s2 = sourad2(:,k);
                

                
                obr_g11(s1(1) - 7 : s1(1) + 7,s1(2)) = 1;
                obr_g11(s1(1),s1(2) - 7 : s1(2) + 7) = 1;
                
                
                obr_g22(s2(1) - 7 : s2(1) + 7,s2(2)) = 1;
                obr_g22(s2(1),s2(2) - 7 : s2(2) + 7) = 1;

                
    

                
            end
            
            imwrite(obr_g11,[cesta_vysl '' soubor1 '_' mat2str(pp) '' '.png'])
            imwrite(obr_g22,[cesta_vysl '' soubor2 '_' mat2str(pp) '' '.png'])
            
%             figure(1)
%             imshow(obr_g11)
%             figure(2)
%             imshow(obr_g22)

                
  
            pause()    
                
            
            
        end
    end
    

    
    m = 1;
end





