while 1

%ziskani rgb obrazu z IP kamery%%
    camera=ipcam('http://192.168.1.2/axis-cgi/mjpg/video.cgi?resolution=640x480', 'root', 'admin')
    rgb=snapshot(camera);
    
%%aplikace gaussova filtru%%
    gauss = imgaussfilt(rgb,2);
    
%%prevod na hsv%%
    hsv = rgb2hsv(gauss);

%%metoda prahovani%%
    image = hsv;
    mean_image = imfilter(image, fspecial('average',[15,15]),'replica');
    subtract = image-(mean_image+0.005); 
    black_white = im2bw(subtract,0);

%%morfologicke uzavreni%%
    originalBW = black_white;
    se = strel('disk',10);
    closeBW = imclose(originalBW,se);

%%morfologicke otevreni%%
    se = strel('disk',10);
    openBW = imopen(closeBW,se);

%%prohozeni cerne a bile%%
    reverseBnW=imcomplement(openBW);

%%vyfiltrovani objektu krome nejvetsiho%%
    BW2 = bwareafilt(reverseBnW,1);
    BW3=imfill(BW2, 'holes');
    
%%uzavreni okna a nasledne zobrazeni noveho s aktualizovanym zpracovanym obrazem%%
    close
    imshow(BW3); 
          
%ziskani souradnic pro vypocet trajektorie pohybu%%
    props = regionprops(BW3, 'Centroid');
    xCentroid = props.Centroid(1)
    yCentroid = props.Centroid(2)

    Bod_stredni = [xCentroid, yCentroid];
    Bod_pocatek = [320, 480];

%%vykresleni trajektorie do okna se zpracovanym obrazem%% 
    hold on
    plot([ Bod_pocatek(1) Bod_stredni(1)], [Bod_pocatek(2) Bod_stredni(2)],'.-r')

        
%sekundova pauza mezi dalsim cyklem (nove nactenym obrazem z kamery)%%    
    pause(1)
    
end
