function varargout = guicko2(varargin)
% GUICKO2 M-file for guicko2.fig
%      GUICKO2, by itself, creates a new GUICKO2 or raises the existing
%      singleton*.
%
%      H = GUICKO2 returns the handle to a new GUICKO2 or the handle to
%      the existing singleton*.
%
%      GUICKO2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUICKO2.M with the given input arguments.
%
%      GUICKO2('Property','Value',...) creates a new GUICKO2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before guicko2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to guicko2_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help guicko2

% Last Modified by GUIDE v2.5 26-May-2014 14:42:56

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @guicko2_OpeningFcn, ...
                   'gui_OutputFcn',  @guicko2_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before guicko2 is made visible.
function guicko2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to guicko2 (see VARARGIN)

% Choose default command line output for guicko2
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes guicko2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = guicko2_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;






% --- Executes on button press in OpenImage.
function OpenImage_Callback(hObject, eventdata, handles)
% hObject    handle to OpenImage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% value = get(handles.OpenImage,'Value',str2num())
global INFO; global T;
set(handles.SelImage,'Enable','on')
INFO.Current = pwd;
openim
if INFO.prebehlo ~=0
PathName = INFO.PathName;
if PathName~=0 
   INFO.PathName2 = PathName; 
   set(handles.CurrentFolder, 'String', INFO.PathName2);
   obr = mat2gray(T(1).IMAGE.ORIG);
axes(handles.axes1);
imshow(obr)
   else 
    h = errordlg('Error - obrazky nenacitane');
    set(handles.CurrentFolder, 'String', 'Error');
end
else
end
% sliderMin = 1;
% sliderMax = length(INFO.pomocna); % this is variable
% sliderStep = [1/sliderMax, 1/sliderMax]; % major and minor steps of 1
% 
% set(handles.slider2, 'Min', sliderMin,'Max', sliderMax,'SliderStep', sliderStep,'Value',1);
% set(handles.slider2, );
% set(handles.slider2, );
% % set(handles.slider2, 'Value', sliderMin)
Names=get(handles.listbox3,'String');
for i = 1:length(T)
    Names{i} = T(i).FileName;
end
set(handles.listbox3,'String',Names)
INFO.Names = Names;
% --- Executes during object creation, after setting all properties.
function CurrentFolder_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CurrentFolder (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

global T;
value = get(handles.slider2,'Value');
value = round(value);
obr = mat2gray(T(value).IMAGE.ORIG);
axes(handles.axes1);
imshow(obr)

% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
global T
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in listbox3.
function listbox3_Callback(hObject, eventdata, handles)
% hObject    handle to listbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listbox3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox3
global T myGLOBAL INFO;
% D = INFO.Detected;
pom = get(handles.listbox3,'Value');
if length(pom)==1
    if get(handles.CheckOrig,'Value')==1
       Im = mat2gray(T(pom).IMAGE.ORIG);
       axes(handles.axes1);
       imshow(Im)
       
    elseif get(handles.CheckDet,'Value')==1
        pom2 = get(handles.listbox3,'Value');
        D = INFO.Detected;
        HA = myGLOBAL(D(pom2)).Boundaries;
        Im = mat2gray(T(D(pom2)).IMAGE.ORIG);
       
        axes(handles.axes1);
        imshow(Im)
        hold(handles.axes1,'on')
%         imshow(Im, 'Parent',handles.axes1)
        for ii = 1:length(HA)
                  hranice1 = HA{ii};
                  plot(handles.axes1,hranice1(:,2), hranice1(:,1),'Color','g', 'LineWidth', 1)
        end
        hold(handles.axes1,'off')
        guidata(hObject, handles);
    elseif get(handles.CheckMass,'Value')==1
        D = INFO.Detected;
        idx = get(handles.listbox3,'Value');
        
        X = T(D(idx)).IMAGE.ORIG;
        HA = myGLOBAL(D(idx)).Boundaries;
        X = mat2gray(X);
        properts = myGLOBAL(D(idx)).Properties;
        parts=myGLOBAL(D(idx)).Particles;
        
        axes(handles.axes1);
        imshow(X)
        hold(handles.axes1,'on')
        %         imshow(Im, 'Parent',handles.axes1)
        for ii = 1:length(HA)
            hranice1 = HA{ii};
            plot(handles.axes1,hranice1(:,2), hranice1(:,1),'Color','g', 'LineWidth', 1)
        end
        [m,n] = size(X);
        
        for i = 1:length(parts)
            cent = properts(i).Centroid;                % centroid
            %     rad = round(properts(i).EquivDiameter/2);
            rad = round(properts(i).MajorAxisLength/2);
            % polomer
            diam = round(properts(i).EquivDiameter);    % priemer
            %                     rectangle('Parent',handles.axes1,'Position',[cent(1)-rad-3,cent(2)-rad-3,round(properts(i).MajorAxisLength+5),round(properts(i).MajorAxisLength+5)],...
            %                           'Curvature',[1,1],...
            %                          'LineWidth',1,'EdgeColor','y')
            X1 = cent(1);
            Y1 = round(cent(2)+rad+round(rad*0.25));
            Y2 = round(cent(2)-rad-round(rad*0.25));
            textik = sprintf('%.2f',parts(i).MassFinal);
            textik = num2str(textik);
            
            if cent(2)<m/2
                txt = text('Parent',handles.axes1,'Position',[X1,Y1],'String',textik);
                set(txt,'Color','y','HorizontalAlignment','center','VerticalAlignment','top')
            else
                txt = text('Parent',handles.axes1,'Position',[X1,Y2],'String',textik);
                set(txt,'Color','y','HorizontalAlignment','center','VerticalAlignment','bottom')
            end
        end
        hold(handles.axes1,'off')
        guidata(hObject, handles);
    elseif get(handles.Shape,'Value')==1
         D = INFO.Detected;
       idx = get(handles.listbox3,'Value');
                
                X = T(D(idx)).IMAGE.ORIG;
                HA = myGLOBAL(D(idx)).Boundaries;
                X = mat2gray(X);
                properts = myGLOBAL(D(idx)).Properties;
                parts=myGLOBAL(D(idx)).Particles;
                
                axes(handles.axes1);
                imshow(X)
                hold(handles.axes1,'on')
%         imshow(Im, 'Parent',handles.axes1)
                Shape1=myGLOBAL(D(idx)).Shape1;
                Shape2=myGLOBAL(D(idx)).Shape2;
                Shape3=myGLOBAL(D(idx)).Shape3;
                Shape4=myGLOBAL(D(idx)).Shape4;
              for jj = 1:length(Shape1) 
              HA = myGLOBAL(D(idx)).Boundaries(Shape1(jj));
                        for ii = 1:length(HA)
                          hranice1 = HA{ii};
                          plot(handles.axes1,hranice1(:,2), hranice1(:,1),'Color','g', 'LineWidth', 1)
                        end
              end
              for jj = 1:length(Shape2) 
              HB = myGLOBAL(D(idx)).Boundaries(Shape2(jj));
                        for ii = 1:length(HB)
                          hranice1 = HB{ii};
                          plot(handles.axes1,hranice1(:,2), hranice1(:,1),'Color','r', 'LineWidth', 1)
                        end
              end
              for jj = 1:length(Shape3) 
              HC = myGLOBAL(D(idx)).Boundaries(Shape3(jj));
                        for ii = 1:length(HC)
                          hranice1 = HC{ii};
                          plot(handles.axes1,hranice1(:,2), hranice1(:,1),'Color','y', 'LineWidth', 1)
                        end
              end
              for jj = 1:length(Shape4) 
              HD = myGLOBAL(D(idx)).Boundaries(Shape4(jj));
                        for ii = 1:length(HD)
                          hranice1 = HD{ii};
                          plot(handles.axes1,hranice1(:,2), hranice1(:,1),'Color','g', 'LineWidth', 1)
                        end
              end
              
%                 for i = 1:length(parts) 
%                     cent = properts(i).Centroid;                % centroid
%                 %     rad = round(properts(i).EquivDiameter/2); 
%                     rad = round(properts(i).MajorAxisLength/2);
%                     % polomer
%                     diam = round(properts(i).EquivDiameter);    % priemer
% %                     rectangle('Parent',handles.axes1,'Position',[cent(1)-rad-3,cent(2)-rad-3,round(properts(i).MajorAxisLength+5),round(properts(i).MajorAxisLength+5)],...
% %                           'Curvature',[1,1],...
% %                          'LineWidth',1,'EdgeColor','y')
%                      X1 = cent(1);
%                      Y1 = round(cent(2)+rad+round(rad*0.25));
%                      Y2 = round(cent(2)-rad-round(rad*0.25));
%                      textik = sprintf('%.2f',parts(i).MassFinal);
%                      textik = num2str(textik);
%                      [m,n] = size(X);
%                      if cent(2)<m/2
%                          txt = text('Parent',handles.axes1,'Position',[X1,Y1],'String',textik);
%                          set(txt,'Color','w','HorizontalAlignment','center','VerticalAlignment','top')
%                      else
%                          txt = text('Parent',handles.axes1,'Position',[X1,Y2],'String',textik);
%                          set(txt,'Color','w','HorizontalAlignment','center','VerticalAlignment','bottom')
%                      end
%                      
%                 end
%                 hold(handles.axes1,'off')
%                 guidata(hObject, handles);
  elseif get(handles.STD,'Value')==1
         D = INFO.Detected;
         idx = get(handles.listbox3,'Value');
                
                X = T(D(idx)).IMAGE.ORIG;
                HA = myGLOBAL(D(idx)).Boundaries;
                X = mat2gray(X);
                properts = myGLOBAL(D(idx)).Properties;
                parts=myGLOBAL(D(idx)).Particles;
                
                axes(handles.axes1);
                imshow(X)
                hold(handles.axes1,'on')
%         imshow(Im, 'Parent',handles.axes1)
                STDb=INFO.STDClass;
                len = myGLOBAL(D(idx)).GlobalProp.STD;
                      for ii = 1:length(len)
                            STDa = myGLOBAL(D(idx)).GlobalProp.STD(ii);
                            hranice1 = HA{ii};
                            if STDa>STDb
                                  plot(handles.axes1,hranice1(:,2), hranice1(:,1),'Color','r', 'LineWidth', 1)
                            else 
                                plot(handles.axes1,hranice1(:,2), hranice1(:,1),'Color','g', 'LineWidth', 1)
                            end
                      end
             %% vykreslenie hmotnosti
             
%                 for i = 1:length(parts) 
%                     cent = properts(i).Centroid;                % centroid
%                 %     rad = round(properts(i).EquivDiameter/2); 
%                     rad = round(properts(i).MajorAxisLength/2);
%                     % polomer
%                     diam = round(properts(i).EquivDiameter);    % priemer
% %                     rectangle('Parent',handles.axes1,'Position',[cent(1)-rad-3,cent(2)-rad-3,round(properts(i).MajorAxisLength+5),round(properts(i).MajorAxisLength+5)],...
% %                           'Curvature',[1,1],...
% %                          'LineWidth',1,'EdgeColor','y')
%                      X1 = cent(1);
%                      Y1 = round(cent(2)+rad+round(rad*0.25));
%                      Y2 = round(cent(2)-rad-round(rad*0.25));
%                      textik = sprintf('%.2f',parts(i).MassFinal);
%                      textik = num2str(textik);
%                      [m,n] = size(X);
%                      if cent(2)<m/2
%                          txt = text('Parent',handles.axes1,'Position',[X1,Y1],'String',textik);
%                          set(txt,'Color','w','HorizontalAlignment','center','VerticalAlignment','top')
%                      else
%                          txt = text('Parent',handles.axes1,'Position',[X1,Y2],'String',textik);
%                          set(txt,'Color','w','HorizontalAlignment','center','VerticalAlignment','bottom')
%                      end
%                      
%                 end
                hold(handles.axes1,'off')
                guidata(hObject, handles);
        elseif get(handles.A_P,'Value')==1
         D = INFO.Detected;
         idx = get(handles.listbox3,'Value');
                
                X = T(D(idx)).IMAGE.ORIG;
                HA = myGLOBAL(D(idx)).Boundaries;
                X = mat2gray(X);
                properts = myGLOBAL(D(idx)).Properties;
                parts=myGLOBAL(D(idx)).Particles;
                
                axes(handles.axes1);
                imshow(X)
                hold(handles.axes1,'on')
%         imshow(Im, 'Parent',handles.axes1)
                STDb=INFO.APClass;
                abnormSTD = 1;
                      for ii = 1:length(HA)
                            STDa = (myGLOBAL(D(idx)).GlobalProp.AREA(ii));
                            hranice1 = HA{ii};
                            if STDa>STDb(1) || STDa<STDb(2)
                                  plot(handles.axes1,hranice1(:,2), hranice1(:,1),'Color','r', 'LineWidth', 1)
                                  abnormSTD = abnormSTD +1;
                            else 
                                plot(handles.axes1,hranice1(:,2), hranice1(:,1),'Color','g', 'LineWidth', 1)
                                
                            end
                      end
             %% vykreslenie hmotnosti
             
%                 for i = 1:length(parts) 
%                     cent = properts(i).Centroid;                % centroid
%                 %     rad = round(properts(i).EquivDiameter/2); 
%                     rad = round(properts(i).MajorAxisLength/2);
%                     % polomer
%                     diam = round(properts(i).EquivDiameter);    % priemer
% %                     rectangle('Parent',handles.axes1,'Position',[cent(1)-rad-3,cent(2)-rad-3,round(properts(i).MajorAxisLength+5),round(properts(i).MajorAxisLength+5)],...
% %                           'Curvature',[1,1],...
% %                          'LineWidth',1,'EdgeColor','y')
%                      X1 = cent(1);
%                      Y1 = round(cent(2)+rad+round(rad*0.25));
%                      Y2 = round(cent(2)-rad-round(rad*0.25));
%                      textik = sprintf('%.2f',parts(i).MassFinal);
%                      textik = num2str(textik);
%                      [m,n] = size(X);
%                      if cent(2)<m/2
%                          txt = text('Parent',handles.axes1,'Position',[X1,Y1],'String',textik);
%                          set(txt,'Color','w','HorizontalAlignment','center','VerticalAlignment','top')
%                      else
%                          txt = text('Parent',handles.axes1,'Position',[X1,Y2],'String',textik);
%                          set(txt,'Color','w','HorizontalAlignment','center','VerticalAlignment','bottom')
%                      end
%                      
%                 end
                hold(handles.axes1,'off')
                guidata(hObject, handles);
    elseif get(handles.MassClass,'Value')==1
         D = INFO.Detected;
         idx = get(handles.listbox3,'Value');
                
                X = T(D(idx)).IMAGE.ORIG;
                HA = myGLOBAL(D(idx)).Boundaries;
                X = mat2gray(X);
                properts = myGLOBAL(D(idx)).Properties;
                parts=myGLOBAL(D(idx)).Particles;
                
                axes(handles.axes1);
                imshow(X)
                hold(handles.axes1,'on')
%         imshow(Im, 'Parent',handles.axes1)
                STDb=INFO.MassClass;
                
                      for ii = 1:length(HA)
                            STDa = myGLOBAL(D(idx)).GlobalProp.MASS(ii);
                            hranice1 = HA{ii};
                            if STDa>STDb(1) || STDa<STDb(2)
                                  plot(handles.axes1,hranice1(:,2), hranice1(:,1),'Color','r', 'LineWidth', 1)
                                  
                            else 
                                plot(handles.axes1,hranice1(:,2), hranice1(:,1),'Color','g', 'LineWidth', 1)
                                
                            end
                      end
             %% vykreslenie hmotnosti
             
                for i = 1:length(parts) 
                    cent = properts(i).Centroid;                % centroid
                %     rad = round(properts(i).EquivDiameter/2); 
                    rad = round(properts(i).MajorAxisLength/2);
                    % polomer
                    diam = round(properts(i).EquivDiameter);    % priemer
%                     rectangle('Parent',handles.axes1,'Position',[cent(1)-rad-3,cent(2)-rad-3,round(properts(i).MajorAxisLength+5),round(properts(i).MajorAxisLength+5)],...
%                           'Curvature',[1,1],...
%                          'LineWidth',1,'EdgeColor','y')
                     X1 = cent(1);
                     Y1 = round(cent(2)+rad+round(rad*0.25));
                     Y2 = round(cent(2)-rad-round(rad*0.25));
                     textik = sprintf('%.2f',parts(i).MassFinal);
                     textik = num2str(textik);
                     [m,n] = size(X);
                     if cent(2)<m/2
                         txt = text('Parent',handles.axes1,'Position',[X1,Y1],'String',textik);
                         set(txt,'Color','w','HorizontalAlignment','center','VerticalAlignment','top')
                     else
                         txt = text('Parent',handles.axes1,'Position',[X1,Y2],'String',textik);
                         set(txt,'Color','w','HorizontalAlignment','center','VerticalAlignment','bottom')
                     end
                     
                end
                hold(handles.axes1,'off')
                guidata(hObject, handles);
    elseif get(handles.CheckDetBack,'Value')==1
        D=INFO.Detected;
       if rem(pom,2)==1
        pom = pom/2+0.5;
        HA = myGLOBAL(D(pom)).Boundaries;
        Im = mat2gray(T(D(pom)).IMAGE.ORIG);
        axes(handles.axes1);
        imshow(Im)
        hold(handles.axes1,'on')
%         imshow(Im, 'Parent',handles.axes1)
        for ii = 1:length(HA)
                  hranice1 = HA{ii};
                  plot(handles.axes1,hranice1(:,2), hranice1(:,1),'Color','g', 'LineWidth', 1)
        end
        hold(handles.axes1,'off')
        guidata(hObject, handles);
       else
        pom = pom/2;
        Im = myGLOBAL(D(pom)).Images.BINAR ;
        axes(handles.axes1);
        imshow(Im)
       end
      end
else
end
                

% --- Executes during object creation, after setting all properties.
function listbox3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in MassGrad.
function MassGrad_Callback(hObject, eventdata, handles)
% hObject    handle to MassGrad (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of MassGrad
val = get(hObject,'Value');
if val ==1
    set(handles.Automat,'Value',0)
    set(handles.HistGrad,'Value',0)
end
% --- Executes on button press in Automat.
function Automat_Callback(hObject, eventdata, handles)
% hObject    handle to Automat (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
val = get(hObject,'Value');
if val ==1
    set(handles.MassGrad,'Value',0)
    set(handles.HistGrad,'Value',0)
end
% Hint: get(hObject,'Value') returns toggle state of Automat


% --- Executes on button press in HistGrad.
function HistGrad_Callback(hObject, eventdata, handles)
% hObject    handle to HistGrad (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of HistGrad
val = get(hObject,'Value');
if val ==1
    set(handles.Automat,'Value',0)
    set(handles.MassGrad,'Value',0)
    
end

% --- Executes on button press in detection.
function detection_Callback(hObject, eventdata, handles)
% hObject    handle to detection (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global T INFO
pomoc1 = get(handles.MassGrad,'Value');
pomoc2 = get(handles.Automat,'Value');
pomoc3 = get(handles.HistGrad,'Value');
SelImage = INFO.Selected_Image;
% SelImage = mat2gray(SelImage);

if pomoc1 == 1
   detection(SelImage)
     default=('NIE');
   button = questdlg('Uložiť hodnotu prahu ?','Potvrdenie prahu','ANO','NIE','ZRUSIT',default);
   if button  == 'NIE'
       INFO.Tresh = [];
   else
   end
elseif pomoc2 == 1
%     automat
    b = max(SelImage);
    b=max(b);
%     SelImage = mat2gray(SelImage);
SelImage = SelImage./b;
    h1 = [1 2 1;2 4 2;1 2 1]./16;
% h1 = [1 2 4 2 1; 2 4 8 4 2; 4 8 16 8 4;2 4 8 4 2; 1 2 4 2 1]./16;
    SelImage = imfilter(SelImage,h1);
    level = graythresh(SelImage);
    BIN = im2bw(SelImage,level);
    Se = strel('disk',3);
    BIN = imopen(BIN,Se);
    HA = bwboundaries(BIN);
    aa=figure;
    imshow(SelImage)
    hold on
        for ii = 1:length(HA)
            hranice1 = HA{ii};
            plot(hranice1(:,2), hranice1(:,1), 'g', 'LineWidth', 1)
        end
    hold off
    
    INFO.Tresh = level*b;
    default=('NIE');
     button2 = questdlg('Uložiť hodnotu prahu ?','Potvrdenie prahu','ANO','NIE',default);
   if button2  == 'NIE'
       INFO.Tresh = [];
   else
   end
elseif pomoc3 == 1
%     manual(SelImage)
histo
else
    errordlg('Chyba - Vyberte metodu');
end
if isempty(INFO.Tresh)==0
   set(handles.pushbuttonOK,'Enable','on')
   set(handles.AllImages,'Enable','on')
   set(handles.SelectedImages,'Enable','on')
else 
    set(handles.pushbuttonOK,'Enable','off')
end

% --- Executes on button press in SelImage.
function SelImage_Callback(hObject, eventdata, handles)
% hObject    handle to SelImage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global INFO T myGLOBAL;
set(handles.MassGrad,'Enable','on')
set(handles.Automat,'Enable','on')
set(handles.HistGrad,'Enable','on')
set(handles.detection,'Enable','on')
imag = get(handles.listbox3,'Value');
SelImage = T(imag).IMAGE.ORIG;
INFO.Selected_Image = SelImage;
Name = T(imag).FileName;
set(handles.ImTXT,'String',Name)

function ImTXT_Callback(hObject, eventdata, handles)
% hObject    handle to ImTXT (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ImTXT as text
%        str2double(get(hObject,'String')) returns contents of ImTXT as a double


% --- Executes during object creation, after setting all properties.
function ImTXT_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ImTXT (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttonOK.
function pushbuttonOK_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonOK (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global T INFO myGLOBAL
pomocnicek = 0;
% if INFO.Tresh==0
    if get(handles.AllImages,'Value') == 1
       
        volaco = linspace(1,length(T),length(T));
        INFO.Detected = volaco;
        detALL
        pomocnicek = 1;
        Names = INFO.Names;
        pom = length(T);
        % myGLOBAL(i).Boundaries = HA;
%         x = 1;
%             for i = pom+1:pom*2
%                     Names{i} = ['DET_',T(x).FileName];
%                     x = x+1;
%             end
%         set(handles.listbox3,'String',Names)
%         INFO.Names = Names;
        
    elseif get(handles.SelectedImages,'Value') == 1
        
        V = get(handles.listbox3,'Value');
        INFO.Detected = V;
        detSelected
        pomocnicek = 1;
    else 
        errordlg('Vyber prazdny')
    end
    if pomocnicek == 1
        set(handles.CheckDet,'Enable','on')
        set(handles.MassCalc,'Enable','on')
        set(handles.CheckDetBack,'Enable','on')
       
        
    else
    end
% --- Executes on button press in pushbuttonRESET.
function pushbuttonRESET_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonRESET (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global INFO;
% INFO = []; T(:).Boundaries = [];
% Name = 'Empty';
% set(handles.ImTXT,'String',Name)
% set(handles.listbox3,'Max',1,'Min',0)
% for i = 1:length(T)
%     Names{i} = T(i).FileName;
% end
% set(handles.listbox3,'String',Names)
clear all
close
guicko2

% --- Executes on button press in AllImages.
function AllImages_Callback(hObject, eventdata, handles)
% hObject    handle to AllImages (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of AllImages
val = get(hObject,'Value');
if val == 1 
    set(handles.SelectedImages,'Value',0)
end
set(handles.listbox3,'Max',1,'Min',0)

% --- Executes on button press in SelectedImages.
function SelectedImages_Callback(hObject, eventdata, handles)
% hObject    handle to SelectedImages (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global T INFO
% Hint: get(hObject,'Value') returns toggle state of SelectedImages
val = get(hObject,'Value');
if val == 1 
    set(handles.AllImages,'Value',0)
end
set(handles.listbox3,'Max',length(T),'Min',0)


% --- Executes on button press in MassCalc.
function MassCalc_Callback(hObject, eventdata, handles)
% hObject    handle to MassCalc (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global T INFO myGLOBAL
masscalc
klasif
set(handles.MassDispInteract,'Enable','on')
set(handles.CheckMass,'Enable','on')
 set(handles.CheckClass,'Enable','on')
% --- Executes on button press in MassDispAll.
% function MassDispAll_Callback(hObject, eventdata, handles)
% % hObject    handle to MassDispAll (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% global INFO T;
% % Hint: get(hObject,'Value') returns toggle state of MassDispAll
% val = get(hObject,'Value');
% D = INFO.Detected;
set(handles.uipanel9,'Visible','on')     
 
% --- Executes on button press in MassDispInteract.
function MassDispInteract_Callback(hObject, eventdata, handles)
% hObject    handle to MassDispInteract (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global INFO T;
val = get(hObject,'Value');
if val == 1 
%         set(handles.MassDispAll,'Value',0)
        idx = get(handles.listbox3,'Value'); 
        D = INFO.Detected;
        pom = length(D);
        pom2 = length(T);

%             if idx>pom2 && idx<=(pom2+pom)
%                 idx2 = idx-pom2;
        massimage(idx)
%             end
end
% Hint: get(hObject,'Value') returns toggle state of MassDispInteract


% --- Executes on button press in CLASS.
function CLASS_Callback(hObject, eventdata, handles)
% hObject    handle to CLASS (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global INFO T myGLOBAL

        D = INFO.Detected;
        Names = INFO.Names;
        pom = length(D);
        pom2 = length(T)+length(D);
        % myGLOBAL(i).Boundaries = HA;
%         x = 1;
%             for i = (pom2+pom+1):(pom2+pom+pom)
%                     Names{i} = ['CLASS_',T(D(x)).FileName];
%                     x = x+1;
%             end
%         set(handles.listbox3,'String',Names)

guide2


% --- Executes on button press in CheckOrig.
function CheckOrig_Callback(hObject, eventdata, handles)
% hObject    handle to CheckOrig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global T
% Hint: get(hObject,'Value') returns toggle state of CheckOrig
set(handles.CheckDet,'Value',0)
set(handles.CheckMass,'Value',0)
set(handles.CheckClass,'Value',0)
set(handles.CheckDetBack,'Value',0)
set(handles.listbox3,'Value',1)
set(handles.STD,'Value',0)
set(handles.A_P,'Value',0)
set(handles.MassClass,'Value',0)
set(handles.Shape,'Value',0)


            for i = 1:length(T)
                    Names{i} = [T(i).FileName];
                   
            end
        set(handles.listbox3,'String',Names)
% --- Executes on button press in CheckDet.
function CheckDet_Callback(hObject, eventdata, handles)
% hObject    handle to CheckDet (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global T INFO
% Hint: get(hObject,'Value') returns toggle state of CheckDet
set(handles.CheckOrig,'Value',0)
set(handles.CheckMass,'Value',0)
set(handles.CheckClass,'Value',0)
set(handles.CheckDetBack,'Value',0)
set(handles.listbox3,'Value',1)
set(handles.STD,'Value',0)
set(handles.A_P,'Value',0)
set(handles.MassClass,'Value',0)
set(handles.Shape,'Value',0)
D = INFO.Detected;
% D = get(handles.listbox3,'Value');
        
        x = 1;
            for i = 1:length(D)
                    Names{i} = ['DET_',T(D(x)).FileName];
                    x = x+1;
            end
        set(handles.listbox3,'String',Names)
  
% --- Executes on button press in CheckMass.
function CheckMass_Callback(hObject, eventdata, handles)
% hObject    handle to CheckMass (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global INFO T
set(handles.CheckDet,'Value',0)
set(handles.CheckOrig,'Value',0)
set(handles.CheckClass,'Value',0)
set(handles.CheckDetBack,'Value',0)
set(handles.listbox3,'Value',1)
set(handles.STD,'Value',0)
set(handles.A_P,'Value',0)
set(handles.MassClass,'Value',0)
set(handles.Shape,'Value',0)
% Hint: get(hObject,'Value') returns toggle state of CheckMass
D = INFO.Detected;
x = 1;
            for i = 1:length(D)
                    Names{i} = ['MASS_',T(D(x)).FileName];
                    x = x+1;
            end
        set(handles.listbox3,'String',Names)

% --- Executes on button press in CheckClass.
function CheckClass_Callback(hObject, eventdata, handles)
% hObject    handle to CheckClass (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% set(handles.CheckDet,'Value',0)
global INFO T
% Hint: get(hObject,'Value') returns toggle state of CheckClass
set(handles.CheckDet,'Value',0)
set(handles.CheckMass,'Value',0)
set(handles.CheckOrig,'Value',0)
set(handles.CheckDetBack,'Value',0)
set(handles.listbox3,'Value',1)
D = INFO.Detected;
x = 1;
            for i = 1:length(D)
                    Names{i} = ['CLASS_',T(D(x)).FileName];
                    x = x+1;
            end
        set(handles.listbox3,'String',Names)

% --- Executes on button press in CheckDetBack.
function CheckDetBack_Callback(hObject, eventdata, handles)
% hObject    handle to CheckDetBack (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global INFO T
set(handles.CheckDet,'Value',0)
set(handles.CheckMass,'Value',0)
set(handles.CheckClass,'Value',0)
set(handles.CheckOrig,'Value',0)
set(handles.listbox3,'Value',1)
set(handles.STD,'Value',0)
set(handles.A_P,'Value',0)
set(handles.MassClass,'Value',0)
set(handles.Shape,'Value',0)
% Hint: get(hObject,'Value') returns toggle state of CheckDetBack
D = INFO.Detected;
x = 1;
            for i = 1:2:length(D)*2
                    Names{i} = ['DET_',T(D(x)).FileName];
                    Names{i+1} = ['BACKGR_',T(D(x)).FileName];
                    x = x+1;
            end
        set(handles.listbox3,'String',Names)


% --- Executes on button press in newfig.
function newfig_Callback(hObject, eventdata, handles)
% hObject    handle to newfig (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global T INFO myGLOBAL;
axes(handles.axes1)
if isempty(T)==0

    if get(handles.CheckOrig,'Value')==1
        pom = get(handles.listbox3,'Value')
        Im = mat2gray(T(pom).IMAGE.ORIG);
        figure; imshow(Im)
    elseif get(handles.CheckDet,'Value')==1
        D = INFO.Detected;
        pom = get(handles.listbox3,'Value')
        HA = myGLOBAL(D(pom)).Boundaries;
        Im = mat2gray(T(D(pom)).IMAGE.ORIG);
        
        figure;
        imshow(Im)
        hold on
        %         imshow(Im, 'Parent',handles.axes1)
        for ii = 1:length(HA)
            hranice1 = HA{ii};
            plot(hranice1(:,2), hranice1(:,1),'Color','g', 'LineWidth', 1)
        end
        hold off
    elseif get(handles.CheckMass,'Value')==1
        D = INFO.Detected;
        idx = get(handles.listbox3,'Value');
        
        X = T(D(idx)).IMAGE.ORIG;
        HA = myGLOBAL(D(idx)).Boundaries;
        X = mat2gray(X);
        properts = myGLOBAL(D(idx)).Properties;
        parts=myGLOBAL(D(idx)).Particles;
        
        figure;
        imshow(X)
        hold on
        %         imshow(Im, 'Parent',handles.axes1)
        for ii = 1:length(HA)
            hranice1 = HA{ii};
            plot(hranice1(:,2), hranice1(:,1),'Color','g', 'LineWidth', 1)
        end
        [m,n] = size(X);
        
        for i = 1:length(parts)
            cent = properts(i).Centroid;                % centroid
            %     rad = round(properts(i).EquivDiameter/2);
            rad = round(properts(i).MajorAxisLength/2);
            % polomer
            diam = round(properts(i).EquivDiameter);    % priemer
            %                     rectangle('Parent',handles.axes1,'Position',[cent(1)-rad-3,cent(2)-rad-3,round(properts(i).MajorAxisLength+5),round(properts(i).MajorAxisLength+5)],...
            %                           'Curvature',[1,1],...
            %                          'LineWidth',1,'EdgeColor','y')
            X1 = cent(1);
            Y1 = round(cent(2)+rad+round(rad*0.25));
            Y2 = round(cent(2)-rad-round(rad*0.25));
            textik = sprintf('%.2f',parts(i).MassFinal);
            textik = num2str(textik);
            
            if cent(2)<m/2
                txt = text('Position',[X1,Y1],'String',textik);
                set(txt,'Color','y','HorizontalAlignment','center','VerticalAlignment','top')
            else
                txt = text('Position',[X1,Y2],'String',textik);
                set(txt,'Color','y','HorizontalAlignment','center','VerticalAlignment','bottom')
            end
        end
        hold off
    elseif get(handles.CheckClass,'Value')==1
        D = INFO.Detected;
        idx = get(handles.listbox3,'Value');
        
        X = T(D(idx)).IMAGE.ORIG;
        HA = myGLOBAL(D(idx)).Boundaries;
        X = mat2gray(X);
        properts = myGLOBAL(D(idx)).Properties;
        parts=myGLOBAL(D(idx)).Particles;
        
        figure
        imshow(X)
        hold on
        %         imshow(Im, 'Parent',handles.axes1)
        Shape1=myGLOBAL(D(idx)).Shape1;
        Shape2=myGLOBAL(D(idx)).Shape2;
        Shape3=myGLOBAL(D(idx)).Shape3;
        Shape4=myGLOBAL(D(idx)).Shape4;
        for jj = 1:length(Shape1)
            HA = myGLOBAL(D(idx)).Boundaries(Shape1(jj));
            for ii = 1:length(HA)
                hranice1 = HA{ii};
                p1 = plot(hranice1(:,2), hranice1(:,1),'Color','g', 'LineWidth', 1);
            end
        end
        for jj = 1:length(Shape2)
            HB = myGLOBAL(D(idx)).Boundaries(Shape2(jj));
            for ii = 1:length(HB)
                hranice1 = HB{ii};
                p2 = plot(hranice1(:,2), hranice1(:,1),'Color','r', 'LineWidth', 1);
            end
        end
        for jj = 1:length(Shape3)
            HC = myGLOBAL(D(idx)).Boundaries(Shape3(jj));
            for ii = 1:length(HC)
                hranice1 = HC{ii};
                p3 = plot(hranice1(:,2), hranice1(:,1),'Color','y', 'LineWidth', 1);
            end
        end
        for jj = 1:length(Shape4)
            HD = myGLOBAL(D(idx)).Boundaries(Shape4(jj));
            for ii = 1:length(HD)
                hranice1 = HD{ii};
                p4 = plot(hranice1(:,2), hranice1(:,1),'Color','b', 'LineWidth', 1);
            end
        end
        %       H = {'Okruhle';'Stvorcove';'Komplexne';'Obdlznikove'};
        %       legend([p1,p2,p3],H)
        
        %                 for i = 1:length(parts)
        %                     cent = properts(i).Centroid;                % centroid
        %                 %     rad = round(properts(i).EquivDiameter/2);
        %                     rad = round(properts(i).MajorAxisLength/2);
        %                     % polomer
        %                     diam = round(properts(i).EquivDiameter);    % priemer
        % %                     rectangle('Parent',handles.axes1,'Position',[cent(1)-rad-3,cent(2)-rad-3,round(properts(i).MajorAxisLength+5),round(properts(i).MajorAxisLength+5)],...
        % %                           'Curvature',[1,1],...
        % %                          'LineWidth',1,'EdgeColor','y')
        %                      X1 = cent(1);
        %                      Y1 = round(cent(2)+rad+round(rad*0.25));
        %                      Y2 = round(cent(2)-rad-round(rad*0.25));
        %                      textik = sprintf('%.2f',parts(i).MassFinal);
        %                      textik = num2str(textik);
        %                      [m,n] = size(X);
        %                      if cent(2)<m/2
        %                          txt = text('Position',[X1,Y1],'String',textik);
        %                          set(txt,'Color','w','HorizontalAlignment','center','VerticalAlignment','top')
        %                      else
        %                          txt = text('Position',[X1,Y2],'String',textik);
        %                          set(txt,'Color','w','HorizontalAlignment','center','VerticalAlignment','bottom')
        %                      end
        %
        %                 end
        %                 hold off
    elseif get(handles.CheckDetBack,'Value')==1
        D=INFO.Detected;
        pom = get(handles.listbox3,'Value');
        if rem(pom,2)==1
            pom = pom/2+0.5;
            HA = myGLOBAL(D(pom)).Boundaries;
            Im = mat2gray(T(D(pom)).IMAGE.ORIG);
            figure
            imshow(Im)
            hold on
            %         imshow(Im, 'Parent',handles.axes1)
            for ii = 1:length(HA)
                hranice1 = HA{ii};
                plot(hranice1(:,2), hranice1(:,1),'Color','g', 'LineWidth', 1)
            end
            hold off
        else
            pom = pom/2;
            Im = myGLOBAL(pom).Images.BINAR ;
            figure
            imshow(Im)
        end
    end
else errordlg('Niesu nacitane obrazky')
end

% --- Executes on button press in Shape.
function Shape_Callback(hObject, eventdata, handles)
% hObject    handle to Shape (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global INFO
% Hint: get(hObject,'Value') returns toggle state of Shape
p = get(hObject,'Value');
if p==1
    set(handles.STD,'Value',0);
    set(handles.A_P,'Value',0);
    set(handles.MassClass,'Value',0);
end
% --- Executes on button press in STD.
function STD_Callback(hObject, eventdata, handles)
% hObject    handle to STD (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global INFO
% Hint: get(hObject,'Value') returns toggle state of STD
p = get(hObject,'Value');
if p==1
    set(handles.Shape,'Value',0);
    set(handles.A_P,'Value',0);
    set(handles.MassClass,'Value',0);
end
av = mean(INFO.STD);
Std = std(INFO.STD,1);
av = av+1.5*Std;
INFO.STDClass = av;
% --- Executes on button press in A_P.
function A_P_Callback(hObject, eventdata, handles)
% hObject    handle to A_P (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global INFO
% Hint: get(hObject,'Value') returns toggle state of A_P
p = get(hObject,'Value');
if p==1
    set(handles.STD,'Value',0);
    set(handles.Shape,'Value',0);
    set(handles.MassClass,'Value',0);
end
av = mean(INFO.AREA);
Std = std(INFO.AREA,1);

% av1 = 1.25*av;
% av2 = 0.75*av;
av1 = av+1.5*Std;
av2 = av-1.5*Std;

av = [av1 av2];
INFO.APClass = av;
% --- Executes on button press in MassClass.
function MassClass_Callback(hObject, eventdata, handles)
% hObject    handle to MassClass (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global INFO
% Hint: get(hObject,'Value') returns toggle state of MassClass
p = get(hObject,'Value');
if p==1
    set(handles.STD,'Value',0);
    set(handles.A_P,'Value',0);
    set(handles.Shape,'Value',0);
end
av = mean(INFO.MASS);
Std = std(INFO.MASS,1);

% av1 = 1.25*av;
% av2 = 0.75*av;
av1 = av+1.5*Std;
av2 = av-1.5*Std;

av = [av1 av2];
INFO.MassClass = av;


% --- Executes during object creation, after setting all properties.
function pushbuttonOK_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pushbuttonOK (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function detection_CreateFcn(hObject, eventdata, handles)
% hObject    handle to detection (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
