%Main
clear; close all; clc;
im = imread('7b.JPG');
[centers,LH,LD,PH,PD, Allcenters] = findCircles(im);
if (isempty(LH) || isempty(LD) ||isempty(PH) ||isempty(PD))
    [LH,LD,PD,PH] = nalezeniZbyvajicihoBodu(centers, im,LH,LD,PD,PH);
end
[im, LH,PH,LD, PD] = rotateImage(im, LH,PH,LD, PD);
Width = round(sqrt((PH(1)-LH(1))^2 + (PH(2)-LH(2))^2));    %šířka, vzdálenost bodů
Heigh = round(sqrt((LD(1)-LH(1))^2 + (LD(2)-LH(2))^2));    %výška
im2 = imcrop(im,[LH(1)+30,LH(2)-20,Width-60,round((Heigh+50)/1.5)]);
%im2 = imcrop(im,[LH(1),LH(2)-150,Width-30,Heigh+50]);
im2BW = Preprocessing(im2);
[startingPoint,MaxNumOfStcks] = whereToStart(im2BW);
[Tops] = findTops(im2BW,startingPoint,MaxNumOfStcks,im2);
Tops = findWrongTopsAutoGUI(Tops,1);

imshow(im2);
hold on;
plot(Tops(:,2),Tops(:,1), 'r+', 'MarkerSize', 20, 'LineWidth', 2);


    