function varargout = gui_kalibrace(varargin)
% aplikacni rozhrani provadi (gui) kalibraci pro segmentaci

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_kalibrace_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_kalibrace_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 gui_kalibrace is made visible.
function gui_kalibrace_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 gui_kalibrace (see VARARGIN)
global im_pozice obr_l;
obr_l = im2double(imread('kalibrace/kalibrace.bmp'));
axes(handles.axes_kurzor);
imshow(obr_l);
slider_value=get(handles.slider_prevzorkovat,'value');
set(handles.prevzorkovat,'string',num2str(slider_value));
im_pozice = impoint;


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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = gui_kalibrace_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 rad_r.
function rad_r_Callback(hObject, eventdata, handles)
global prostor volba;
prostor=1; % barevny prostor
volba=0; % volba slozek barevneho prostoru

set(handles.edit_prah,'string',num2str(0.02));
set(handles.edit_prah2,'string','není');
set(handles.edit_prah3,'string','není');
set(handles.edit_adapt,'string',num2str(0.40));
set(handles.edit_adapt2,'string','není');
set(handles.edit_adapt3,'string','není');

%nastaveni radiobutonů
set(handles.rad_ir,'Value',0)
set(handles.rad_r,'Value',1)
set(handles.rad_rb,'Value',0)
set(handles.rad_cr,'Value',0)
set(handles.rad_cbcr,'Value',0)
set(handles.rad_rcrh,'Value',0)


% --- Executes on button press in rad_rb.
function rad_rb_Callback(hObject, eventdata, handles)
global prostor volba;
prostor=1; % barevny prostor
volba=1; % volba slozek barevneho prostoru

set(handles.edit_prah,'string',num2str(0.02));
set(handles.edit_prah2,'string',num2str(0.022));
set(handles.edit_prah3,'string','není');
set(handles.edit_adapt,'string',num2str(0.55));
set(handles.edit_adapt2,'string',num2str(0.4));
set(handles.edit_adapt3,'string','není');
%nastaveni radiobutonů
set(handles.rad_ir,'Value',0)
set(handles.rad_r,'Value',0)
set(handles.rad_rb,'Value',1)
set(handles.rad_cr,'Value',0)
set(handles.rad_cbcr,'Value',0)
set(handles.rad_rcrh,'Value',0)


% --- Executes on button press in rad_cr.
function rad_cr_Callback(hObject, eventdata, handles)
global prostor volba;
prostor=2; % barevny prostor
volba=0; % volba slozek barevneho prostoru

set(handles.edit_prah,'string',num2str(0.015));
set(handles.edit_prah2,'string','není');
set(handles.edit_prah3,'string','není');
set(handles.edit_adapt,'string',num2str(0.6));
set(handles.edit_adapt2,'string','není');
set(handles.edit_adapt3,'string','není');
%nastaveni radiobutonů
set(handles.rad_ir,'Value',0)
set(handles.rad_r,'Value',0)
set(handles.rad_rb,'Value',0)
set(handles.rad_cr,'Value',1)
set(handles.rad_cbcr,'Value',0)
set(handles.rad_rcrh,'Value',0)


% --- Executes on button press in rad_cbcr.
function rad_cbcr_Callback(hObject, eventdata, handles)
global prostor volba;
prostor=2; % barevny prostor
volba=1; % volba slozek barevneho prostoru

set(handles.edit_prah,'string',num2str(0.012));
set(handles.edit_prah2,'string',num2str(0.009));
set(handles.edit_prah3,'string','není');
set(handles.edit_adapt,'string',num2str(0.6));
set(handles.edit_adapt2,'string',num2str(0.6));
set(handles.edit_adapt3,'string','není');
%nastaveni radiobutonů
set(handles.rad_ir,'Value',0)
set(handles.rad_r,'Value',0)
set(handles.rad_rb,'Value',0)
set(handles.rad_cr,'Value',0)
set(handles.rad_cbcr,'Value',1)
set(handles.rad_rcrh,'Value',0)


% --- Executes on button press in rad_rcrh.
function rad_rcrh_Callback(hObject, eventdata, handles)
global prostor volba;
prostor=3; % barevny prostor
volba=0; % volba slozek barevneho prostoru

set(handles.edit_prah,'string',num2str(0.019));
set(handles.edit_prah2,'string',num2str(0.012));
set(handles.edit_prah3,'string',num2str(0.08));
set(handles.edit_adapt,'string',num2str(0.55));
set(handles.edit_adapt2,'string',num2str(0.6));
set(handles.edit_adapt3,'string',num2str(0.3));
%nastaveni radiobutonů
set(handles.rad_ir,'Value',0)
set(handles.rad_r,'Value',0)
set(handles.rad_rb,'Value',0)
set(handles.rad_cr,'Value',0)
set(handles.rad_cbcr,'Value',0)
set(handles.rad_rcrh,'Value',1)


% --- Executes on button press in rad_ir.
function rad_ir_Callback(hObject, eventdata, handles)
global prostor volba;
prostor=4; % IR prostor
volba=0; % volba IR

set(handles.edit_prah,'string',num2str(0.25));
set(handles.edit_prah2,'string','není');
set(handles.edit_prah3,'string','není');
set(handles.edit_adapt,'string',num2str(0.3));
set(handles.edit_adapt2,'string','není');
set(handles.edit_adapt3,'string','není');
%nastaveni radiobutonů
set(handles.rad_ir,'Value',1)
set(handles.rad_r,'Value',0)
set(handles.rad_rb,'Value',0)
set(handles.rad_cr,'Value',0)
set(handles.rad_cbcr,'Value',0)
set(handles.rad_rcrh,'Value',0)

% Hint: get(hObject,'Value') returns toggle state of rad_ir



function edit_adapt_Callback(hObject, eventdata, handles)
% hObject    handle to edit_adapt (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 edit_adapt as text
%        str2double(get(hObject,'String')) returns contents of edit_adapt as a double


% --- Executes during object creation, after setting all properties.
function edit_adapt_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_adapt (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 edit_prah_Callback(hObject, eventdata, handles)
% hObject    handle to edit_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 edit_prah as text
%        str2double(get(hObject,'String')) returns contents of edit_prah as a double


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


% --- Executes on button press in start_kalibrace.
function start_kalibrace_Callback(hObject, eventdata, handles)
global prostor volba im_pozice obr_l;
% ir_v=get(handles.rad_ir,'value')
% if ir_v==1 % v pripade ze se jedna o IR prostor, je nutne prevest na jednoslozkovy sedotonovy obrazek
%     obr_l=rgb2gray(obr_l);
% end
souradnice = fix(getPosition(im_pozice));
souradnice=[souradnice(2),souradnice(1)]
prah1=str2num(get(handles.edit_prah,'string'));
prah2=str2num(get(handles.edit_prah2,'string'));
prah3=str2num(get(handles.edit_prah3,'string'));
prah=[prah1,prah2,prah3];
adapt=str2num(get(handles.edit_adapt,'string'));
adapt2=str2num(get(handles.edit_adapt2,'string'));
adapt3=str2num(get(handles.edit_adapt3,'string'));
adapt_krit_vaha=[adapt,adapt2,adapt3];

prevzorkovat=get(handles.slider_prevzorkovat,'value')
set(handles.prevzorkovat,'string',num2str(prevzorkovat));
[ obr_seg ] = kalibrace( obr_l, prevzorkovat, souradnice, prostor, volba, prah, adapt_krit_vaha );
axes(handles.axes_segment);
imshow(obr_seg);


function edit_prah2_Callback(hObject, eventdata, handles)
% hObject    handle to edit_prah2 (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 edit_prah2 as text
%        str2double(get(hObject,'String')) returns contents of edit_prah2 as a double


% --- Executes during object creation, after setting all properties.
function edit_prah2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_prah2 (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 edit_prah3_Callback(hObject, eventdata, handles)
% hObject    handle to edit_prah3 (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 edit_prah3 as text
%        str2double(get(hObject,'String')) returns contents of edit_prah3 as a double


% --- Executes during object creation, after setting all properties.
function edit_prah3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_prah3 (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 exit_kalibrace.
function exit_kalibrace_Callback(hObject, eventdata, handles)
close ();
% hObject    handle to exit_kalibrace (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function edit_adapt2_Callback(hObject, eventdata, handles)
% hObject    handle to edit_adapt2 (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 edit_adapt2 as text
%        str2double(get(hObject,'String')) returns contents of edit_adapt2 as a double


% --- Executes during object creation, after setting all properties.
function edit_adapt2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_adapt2 (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 edit_adapt3_Callback(hObject, eventdata, handles)
% hObject    handle to edit_adapt3 (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 edit_adapt3 as text
%        str2double(get(hObject,'String')) returns contents of edit_adapt3 as a double


% --- Executes during object creation, after setting all properties.
function edit_adapt3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_adapt3 (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 slider movement.
function slider_prevzorkovat_Callback(hObject, eventdata, handles)
% hObject    handle to slider_prevzorkovat (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.prevzorkovat,'string',num2str(get(hObject,'Value')));
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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



function prevzorkovat_Callback(hObject, eventdata, handles)
% hObject    handle to prevzorkovat (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 prevzorkovat as text
%        str2double(get(hObject,'String')) returns contents of prevzorkovat as a double


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