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

% Last Modified by GUIDE v2.5 22-Apr-2017 13:59:08

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

% Choose default command line output for GUIfiltrace
handles.output = hObject;
% Update handles structure
handles.pushDet = 0;
handles.im = 0;
imshow(zeros(512,512));
set(handles.radiobutton1,'Value',0);
set(handles.radiobutton2,'Value',1);
handles.methods = 'TV ROF';
handles.sav = 0;

guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = GUIfiltrace_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 startfilt.
function startfilt_Callback(hObject, eventdata, handles)
% hObject    handle to startfilt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if (handles.pushDet>0)||handles.im ~= 0

set(handles.edit5,'BackgroundColor','red')
set(handles.edit5,'String','Please wait')
pause(0.1)
switch handles.methods
    case 'TV ROF'
        [handles.u PSNR SNR] = TVROF(handles.im);
        set(handles.text6,'String',[num2str(SNR) ' ' ' [dB]']);
        set(handles.text8,'String',[num2str(abs(PSNR)) ' ' ' [dB]']);
        handles.sav = 1;
    case 'PDE'
        [handles.u PSNR SNR] = EL_TV(handles.im);
        set(handles.text6,'String',[num2str(SNR) ' ' ' [dB]']);
        set(handles.text8,'String',[num2str(abs(PSNR)) ' ' ' [dB]']);
        handles.sav = 1;
    case'NLM'
         [handles.u PSNR SNR] = NLM_filt(handles.im);
        set(handles.text6,'String',[num2str(SNR) ' ' ' [dB]']);
        set(handles.text8,'String',[num2str(abs(PSNR)) ' ' ' [dB]']);
        handles.sav = 1;
    case'TV Poisson'
        [handles.u PSNR SNR] = TVPoiss(handles.im);
        set(handles.text6,'String',[num2str(SNR) ' ' ' [dB]']);
        set(handles.text8,'String',[num2str(abs(PSNR)) ' ' ' [dB]']);
        handles.sav = 1;
        
        
end
imshow(handles.u);
 set(handles.text10,'String','Denoised image');
handles.swit = 1;
handles.pom = handles.u;
handles.pomOrig = handles.im;
 set(handles.edit5,'BackgroundColor','green')
 set(handles.edit5,'String','Ready')
elseif handles.im == 0
        warndlg('You donīt have chosen image')
else
    errordlg('You must choose file!');
end
guidata(hObject, handles);
 

% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.methods = get(handles.radiobutton1,'String');
guidata(hObject, handles);
% Hint: get(hObject,'Value') returns toggle state of radiobutton1


% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.methods = get(handles.radiobutton2,'String');
guidata(hObject, handles);
% Hint: get(hObject,'Value') returns toggle state of radiobutton2


% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% handles.methods = get(handles.radiobutton2,'String')
handles.methods = get(handles.radiobutton3,'String');
guidata(hObject, handles);
% Hint: get(hObject,'Value') returns toggle state of radiobutton3
% --- Executes on button press in radiobutton1.





% --- Executes on button press in choosefile.
function choosefile_Callback(hObject, eventdata, handles)
% hObject    handle to choosefile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 handles.pushDet=1;
 handles.filename = 0;
 handles.pathname = 0;
[handles.filename handles.pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif;*.tiff','All Image Files'}, 'File Selector');
if handles.filename>0
handles.im = (im2double(imread([handles.pathname handles.filename])));
 handles.im = handles.im(:,:,1);
 imshow(handles.im);
 set(handles.text10,'String','Original image');
  handles.filenames =  handles.filename;
 handles.pathnames = handles.pathname ;
 handles.filename = 1;
 handles.pathname = 1;
else
    handles.sav=0;
end
if handles.filename ==0
    handles.pushDet = 0;
end
guidata(hObject, handles);


% --- Executes on button press in swit.
function swit_Callback(hObject, eventdata, handles)
% hObject    handle to swit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.im == 0
     warndlg('You donīt have chosen image')
else
if (handles.swit == 1)
    imshow(handles.pomOrig);
    handles.swit = 2;
     set(handles.text10,'String','Original image');
    guidata(hObject, handles);
elseif (handles.swit == 2)
    imshow(handles.pom)
    handles.swit = 1;
     set(handles.text10,'String','Denoised image');
    guidata(hObject, handles);
end
end


% --- Executes on slider movement.
function sliderL_Callback(hObject, eventdata, handles)
% hObject    handle to sliderL (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
% handels.lambda = get(sliderL,'Value');
handles.lambda = round(get(handles.sliderL,'Value'));
set(handles.lambtext,'String',['lam = ' ' ' num2str(handles.lambda)])
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function sliderL_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sliderL (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 lambtext_Callback(hObject, eventdata, handles)
% hObject    handle to lambtext (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 lambtext as text
%        str2double(get(hObject,'String')) returns contents of lambtext as a double


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


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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 plus.
function plus_Callback(hObject, eventdata, handles)
% hObject    handle to plus (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.im == 0
     warndlg('You donīt have chosen image')
else
[x y] = ginput(2);
x = sort(x); y = sort(y);
handles.pom = handles.pom(y(1):y(2),x(1):x(2));
handles.pomOrig = handles.pomOrig(y(1):y(2),x(1):x(2));
imshow(handles.pom)
guidata(hObject, handles);
end
% --- Executes on button press in minus.
function minus_Callback(hObject, eventdata, handles)
% hObject    handle to minus (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.im == 0
     warndlg('You donīt have chosen image')
else
imshow(handles.u)
handles.pom = handles.u;
handles.pomOrig = handles.im;
guidata(hObject, handles);
end



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


% --- Executes on button press in radiobutton9.
function radiobutton9_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.methods = get(handles.radiobutton9,'String');
guidata(hObject, handles);

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


% --- Executes on button press in SAVE.
function SAVE_Callback(hObject, eventdata, handles)
% hObject    handle to SAVE (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.im == 0
     
else
    if handles.sav>0
        
imwrite(handles.u,[handles.pathnames handles.filenames ' - denoised.tif'])
handles.sav = 0;
    else 
        warndlg('Denoised image can be saved only')
    end
end


% --- Executes on button press in CLEARF.
function CLEARF_Callback(hObject, eventdata, handles)
imshow(zeros(512,512));
handles.im = 0;
handles.pushDet=0;
 set(handles.text10,'String','No image');
 set(handles.text6,'String',' ');
  set(handles.text8,'String',' ');
guidata(hObject, handles);


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