clear all
clc

alpha=10;
iterations=3;
hsize=[5 5];
sigma=1;

h = fspecial('gaussian',hsize,sigma);

F1=zeros(150,150);
for kk=0:15


        posunx=kk;
        posuny=0;

       % Fs=imread('f_s.png');
       %Fs=imread('ruka.png');
       Fs=imread('tux.png');
       %Fs=imread('cara.png');
        Fs=Fs(:,:,1);
        F1=zeros(size(F1,1),size(F1,2));
        %F1(:,:)=255;
        
        posunxx=0.5*(size(F1,1)-size(Fs,1));
        posunyy=0.5*(size(F1,2)-size(Fs,2));

        
        F1(posunxx:(size(Fs,1)+posunxx-1),posunyy:(size(Fs,2)+posunyy-1))=Fs;
        
        F2=zeros(size(F1,1),size(F1,2));
       % F2(:,:)=255;
        posunxx=posunx+0.5*(size(F1,1)-size(Fs,1));
        posunyy=posuny+0.5*(size(F1,2)-size(Fs,2));
        
        F2(posunxx:(size(Fs,1)+posunxx-1),posunyy:(size(Fs,2)+posunyy-1))=Fs;



[height width]=size(F2);

F1=imfilter(F1,h,'replicate');
F2=imfilter(F2,h,'replicate');

[u v TempNormal]=OpticalflowHS(F1,F2,alpha,iterations,height,width);



        prah=0;
        xx=u(find(abs(u)>prah));
        yy=v(find(abs(v)>prah));

        xx=mean(mean(xx))
        yy=mean(mean(yy))
        
        na_pixl_X=xx/(posuny)
        na_pixl_Y=yy/(posunx)
        
        vysledeky(kk+1)=yy;
        vysledekx(kk+1)=xx;
        
        
        
        

        rozdilovy=uint8(zeros(size(F1,1),size(F1,2),3));
        rozdilovy(:,:,1)=F2;
        rozdilovy(:,:,3)=F1;%vykresli vysledny obraz do prostred platna 
        rozdilovy(:,:,2)=rozdilovy(:,:,3);
    
        figure(1);
quiver(u,v);
        
        figure(2)
        subplot(2,2,1)
        imshow(u);
        subplot(2,2,2)
        imshow(v);
        subplot(2,2,3)
        imshow(uint8(F1));
        subplot(2,2,4)
        imshow(abs(rozdilovy));

end



subplot(2,2,1);
plot(0:size(vysledeky,2)-1,vysledeky)
subplot(2,2,2);
plot(0:size(vysledekx,2)-1,vysledekx)
