%% Objektiv - Detekce hran
% Martin Zeman, 2009

function obrazek(fokus, kamera, S, vyska, sirka, prah)
    bila = zeros(1,401);
    focus = fokus;
    string=sprintf('lfp0%03d',focus);
    fprintf(S, '\002x%s\003x', string);
    while (S.BytesAvailable < 3)
        pause(0.1)
    end
    pause(0.5);
    img = getsnapshot(kamera);  
    figure(1);imshow(img);
    img2 = zeros(vyska,sirka);
    img3 = zeros(vyska-2,sirka-2);
    bila(fokus+1) = 0;
    % Prahování
    for i=1:vyska
        for j=1:sirka
            if img(i,j) >= prah*255
                img2(i,j)=255;
            end        
        end
    end
    % Laplac ve čtyř-okolí
    Laplac4 = [0 1 0; 1 -4 1; 0 1 0];
    for i=2:(vyska-1)
        for j=2:(sirka-1)
            m = Laplac4(1,1)*img2(i-1,j-1);
            m = m + Laplac4(1,2)*img2(i-1,j);
            m = m + Laplac4(1,3)*img2(i-1,j+1);
            m = m + Laplac4(2,1)*img2(i,j-1);
            m = m + Laplac4(2,2)*img2(i,j);
            m = m + Laplac4(2,3)*img2(i,j+1);
            m = m + Laplac4(3,1)*img2(i+1,j-1);
            m = m + Laplac4(3,2)*img2(i+1,j);
            img3(i-1,j-1) = m + Laplac4(3,3)*img2(i+1,j+1);
            if (img3(i-1,j-1) == 255)
                bila(fokus+1) = bila(fokus+1) + 1;
            end
        end
    end