function [sX,sY,ok] = detekce (obraz)
%% Load image: bw-B&W picture, img-edges of bw
bw = obraz;
Sobel = [1 0 -1; 2 0 -2; 1 0 -1];
img = im2bw(imfilter(bw,Sobel),0.3);
% figure(2), imshow(bw);
% figure(3), imshow(img);

%% Hough transform
[H,theta,rho] = hough(img,'ThetaResolution',1,'RhoResolution',2);
% figure(4);
% imshow(H,[],'XData',theta,'YData',rho,'InitialMagnification','fit');
% colormap(hot), colorbar, xlabel('\theta [°]'), ylabel('\rho (pixels from center)');
% title('Hough transform'), axis on, axis normal, hold on;

%% Search for maximums in Hough transform
numpeaks = 30;
[dim1 dim2] = size(H);
count = zeros(1,dim2);
for i = 3:(dim2-2)
    H2 = H;
    for j = 1:numpeaks
        [value1 index1] = max(H2(:,i-2:i+2));
        [value2 index2] = max(value1);
        count(i) = count(i) + value2;
        index2 = index1(index2);
        H2(index2,i-2:i+2) = zeros(1,5);
    end
end
[value index] = max(count);
H2 = H;
peaks = zeros(numpeaks,2);
for i = 1:numpeaks
    [value1 index1] = max(H2(:,index:index+4));
    [value2 index2] = max(value1);
    peaks(i,2) = index2;
    index2 = index1(index2);
    peaks(i,1) = index2;
    H2(index2,index:index+4) = zeros(1,5);
end
peaks(:,2) = peaks(:,2) + index - 1;
% x = theta(peaks(:,2)); y = rho(peaks(:,1));
% plot(x,y,'s','Color','white');

%% Find lines
lines = houghlines(img,theta,rho,peaks,'FillGap',5,'MinLength',80);
figure(1), imshow(bw), hold on;
A = zeros(1,length(lines));
if length(lines) > 1
    for i = 1:length(A)
%         xy = [lines(i).point1; lines(i).point2];
%         plot(xy(:,1),xy(:,2),'Color','green');
        A(i) = lines(i).rho;
        
        % Plot beginnings and ends of lines
%         plot(xy(1,1),xy(1,2),'x','Color','yellow');
%         plot(xy(2,1),xy(2,2),'x','Color','red');
    end
    % Find center of target
    first = 0;
    last = 0;
    B = sort(A);
    for i = 1:(length(B)-3)
        if (first == 0) && ((B(i+1)-B(i)) < 25) && ((B(i+2)-B(i+1)) < 25) && ((B(i+3)-B(i+2)) < 25)
            first = find(A == B(i));
        end
        if (first ~= 0) && ((B(i+1)-B(i)) < 25) && ((B(i+2)-B(i+1)) < 25) && ((B(i+3)-B(i+2)) > 25) && (last == 0)
            last = find(A == B(i+2));
            break;
        end
    end
    kodV1 = 0;
    if first ~= 0
        kodV1 = sqrt((lines(first).point2(1) - lines(first).point1(1))^2 + (lines(first).point2(2) - lines(first).point1(2))^2);
    end
    kodV2 = 0;
    if last ~= 0
        kodV2 = sqrt((lines(last).point2(1) - lines(last).point1(1))^2 + (lines(last).point2(2) - lines(last).point1(2))^2);
    end
    if kodV2 > kodV1
        kodU = uint16(kodV2 / 0.46577);
        if lines(last).theta > 0
            if lines(last).theta > 27.76
                % theta > 0, theta > alfa, last
                sY = uint16(lines(last).point2(2) + double(kodU / 2) * sind(180-27.76-lines(last).theta));
                sX = uint16(lines(last).point2(1) + double(kodU / 2) * cosd(180-27.76-lines(last).theta));
            else
                % theta > 0, theta < alfa, last
                sY = uint16(lines(last).point2(2) - double(kodU / 2) * sind(27.76+lines(last).theta));
                sX = uint16(lines(last).point2(1) - double(kodU / 2) * cosd(27.76+lines(last).theta));
            end
        else
            if abs(lines(last).theta) > 27.76
                % theta < 0, theta > alfa, last
                sY = uint16(lines(last).point2(2) + double(kodU / 2) * sind(-27.76-lines(last).theta));
                sX = uint16(lines(last).point2(1) - double(kodU / 2) * cosd(-27.76-lines(last).theta));
            else
                % theta < 0, theta < alfa, last
                sY = uint16(lines(last).point2(2) + double(kodU / 2) * sind(27.76+lines(last).theta));
                sX = uint16(lines(last).point2(1) - double(kodU / 2) * cosd(27.76+lines(last).theta));
            end
        end
    else
        kodU = uint16(kodV1 / 0.46577);
        if lines(first).theta > 0
            if lines(first).theta > 27.76
                % theta > 0, theta > alfa, first
                sY = uint16(lines(first).point2(2) + double(kodU / 2) * sind(-27.76+lines(first).theta));
                sX = uint16(lines(first).point2(1) + double(kodU / 2) * cosd(-27.76+lines(first).theta));
            else
                % theta > 0, theta < alfa, first
                sY = uint16(lines(first).point2(2) - double(kodU / 2) * sind(27.76-lines(first).theta));
                sX = uint16(lines(first).point2(1) + double(kodU / 2) * cosd(27.76-lines(first).theta));
            end
        else
            if abs(lines(first).theta) > 27.76
                % theta < 0, theta > alfa, first
                sY = uint16(lines(first).point2(2) - double(kodU / 2) * sind(180-27.76+lines(first).theta));
                sX = uint16(lines(first).point2(1) - double(kodU / 2) * cosd(180-27.76+lines(first).theta));
            else
                % theta < 0, theta < alfa, first
                sY = uint16(lines(first).point2(2) + double(kodU / 2) * sind(27.76-lines(first).theta));
                sX = uint16(lines(first).point2(1) + double(kodU / 2) * cosd(27.76-lines(first).theta));
            end
        end
    end
    plot(sX,sY,'x','Color','red'); hold off;
    ok = 1;
else
    sX = 0;
    sY = 0;
    ok = 0;
end
