function varargout = histo(varargin)
% HISTO M-file for histo.fig
%      HISTO, by itself, creates a new HISTO or raises the existing
%      singleton*.
%
%      H = HISTO returns the handle to a new HISTO or the handle to
%      the existing singleton*.
%
%      HISTO('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in HISTO.M with the given input arguments.
%
%      HISTO('Property','Value',...) creates a new HISTO or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before histo_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to histo_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 histo

% Last Modified by GUIDE v2.5 25-May-2014 15:34:13

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @histo_OpeningFcn, ...
                   'gui_OutputFcn',  @histo_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 histo is made visible.
function histo_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 histo (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = histo_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 OK.
function OK_Callback(hObject, eventdata, handles)
% hObject    handle to OK (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global INFO HIS
b = HIS.max;
pos = round(get(handles.slider1,'Value'));
positions2 = HIS.positions2;
prah = positions2(pos);
prah = prah*b;
INFO.Tresh = prah;
close


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

% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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 INFO HIS
SelImage = INFO.Selected_Image;
X =SelImage;
positions2=HIS.positions2;
counts2=HIS.counts2;
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;
 X = imfilter(X,h1);
 b = max(X);
b=max(b);
a = min(X);
a=min(a);
X= X./b;
X1 = mat2gray(X);

pos = round(get(hObject,'Value'));
tresh = positions2(pos);
set(handles.Prah,'String',num2str(tresh))
BIN2 = im2bw(X1,tresh);
SE1 = strel('disk', 3);
BIN2 = imopen (BIN2,SE1);
axes(handles.axes2);
imshow(BIN2)

axes(handles.axes3);

bar(handles.axes3,positions2.*b,counts2)
hold(handles.axes3,'on')
line('Parent',handles.axes3,'XData', [positions2(pos).*b positions2(pos).*b], 'YData', [min(counts2) max(counts2)], 'LineStyle', '-', ...
'LineWidth', 2, 'Color','g');
hold(handles.axes3,'off')

BIN = BIN2;
BIN = imclearborder(BIN);
HA = bwboundaries(BIN);
axes(handles.axes1);
imshow(X)
hold(handles.axes1,'on')
for ii = 1:length(HA)
    hranice1 = HA{ii};
    plot(handles.axes1,hranice1(:,2), hranice1(:,1), 'g', 'LineWidth', 1)
end
hold(handles.axes1,'off')
        guidata(hObject, handles);


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
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)


% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject    handle to start (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 HIS
SelImage = INFO.Selected_Image;
X =SelImage;


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;
 X = imfilter(X,h1);
 b = max(X);
b=max(b);
a = min(X);
a=min(a);
X= X./b;
X1 = mat2gray(X);
axes(handles.axes1);
imshow(X)

% vector = linspace(a:b:b);
[counts2,positions2]=imhist(X);
     positions2(counts2==0)=[];
     counts2(counts2==0)=[];
     [Val2,Ind2]=max(counts2);
axes(handles.axes3);
bar(handles.axes3,positions2.*b,counts2)
   
sliderMin = 1;
sliderMax = length(positions2); % this is variable
sliderStep = [1/sliderMax, 1/sliderMax]; % major and minor steps of 1
set(handles.slider1, 'Min', sliderMin,'Max', sliderMax,'SliderStep', sliderStep,'Value',1);
   
  HIS.positions2 = positions2;
  HIS.counts2 = counts2;
  HIS.X1 = X1;
  HIS.max = b;
  

  


% set(handles.slider2, );
% set(handles.slider2, );
% set(handles.slider2, 'Value', sliderMin)
set(handles.slider1,'Visible','on');
set(handles.OK,'Visible','on');
set(handles.axes1,'Visible','on');
set(handles.axes2,'Visible','on');
set(handles.axes3,'Visible','on');
set(handles.axes1,'XTick',[],'YTick',[])
set(handles.axes2,'XTick',[],'YTick',[])
set(handles.axes3,'XTick',[],'YTick',[])
function Prah_Callback(hObject, eventdata, handles)
% hObject    handle to Prah (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 Prah as text
%        str2double(get(hObject,'String')) returns contents of Prah as a double


% --- Executes during object creation, after setting all properties.
function Prah_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Prah (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



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
