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

% Last Modified by GUIDE v2.5 07-Jul-2015 08:40:49

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = matlab_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 Open.
function Open_Callback(hObject, eventdata, handles)
[filename, pathname]= uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files'; '*.*','All Files' },'Choose an image file ...');

if isequal(filename,0)
   warndlg('File not selected');
   return
end

image=imread(strcat(pathname,filename));

axes(handles.Orig_Imag);
imshow(image);

set(handles.Orig_Imag,'UserData',image);
% hObject    handle to Open (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in Save.
function Save_Callback(hObject, eventdata, handles)
[filename, pathname]= uiputfile({'*.jpg;*.tif;*.png;*.gif','All Image Files'; '*.*','All Files' },'Save image file as ...');

if isequal(filename,0)
   warndlg('File not selected');
   return
end

image=get(handles.Proc_Imag,'UserData');

imwrite(image,strcat(pathname,filename));
% 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)


% --- Executes on button press in Copy_to_Original.
function Copy_to_Original_Callback(hObject, eventdata, handles)
image = get(handles.Proc_Imag, 'UserData');

axes(handles.Orig_Imag);
imshow(image);

set(handles.Orig_Imag, 'UserData', image);
% hObject    handle to Copy_to_Original (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in Apply.
function Apply_Callback(hObject, eventdata, handles)
image=get(handles.Orig_Imag, 'UserData');
len=get(handles.slider_pixels, 'Value');
if get(handles.radiobutton_Horizontal, 'Value')
    h=fspecial('motion', len, 0);
else if get(handles.radiobutton_Vertical, 'Value')
    h=fspecial('motion', len, 90);
end
end
var=get(handles.slider_Gaussian, 'Value');
sizfft=max(size(image));

I=fft2(image, sizfft, sizfft);
H=fft2(h, sizfft, sizfft);


if var>0
    blurredfft=I.*H;
    blurred=uint8(ifft2(blurredfft));
    noise=im2uint8(sqrt(var)*randn(size(blurred)));
    blurred=blurred+noise;
else
    blurredfft=I.*H;
    blurred=uint8(ifft2(blurredfft));
end
axes(handles.Proc_Imag)
imshow(blurred)
set(handles.Proc_Imag, 'UserData', blurred)
% hObject    handle to Apply (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on slider movement.
function slider_pixels_Callback(hObject, eventdata, handles)
siz=round(get(hObject,'Value'));
if not(mod(siz+1,2)==0)
    siz=siz+1;
end
set(hObject,'Value',siz);

set(handles.text_pixels_static,'String',[num2str(siz) ' px']);
% hObject    handle to slider_pixels (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


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


% --- Executes on button press in Inverse_filter.
function Inverse_filter_Callback(hObject, eventdata, handles)
image=get(handles.Orig_Imag, 'UserData');
long=get(handles.slider_pixels, 'Value');
if get(handles.radiobutton_Horizontal, 'Value')
h=fspecial('motion', long, 0);
else if get(handles.radiobutton_Vertical, 'Value')
    h=fspecial('motion', long, 90);
    end
end
var_n=get(handles.slider_Gaussian, 'Value');
noise_2=(sqrt(var_n)*randn(size(image)));

sizfft=max(size(image));
FFTimag=fft2(image, sizfft, sizfft);
FFTfilt=fft2(h, sizfft, sizfft);
FFTfilt_inv=1./FFTfilt;

if var_n==0
    FFTrest=FFTimag.*FFTfilt_inv;
    image_proc=ifft2(FFTrest);
    image_proc=real(image_proc);
else
%         H_max=str2double(get(handles.slider_Gaussian, 'String'));
%         FFTfilt_inv(abs(FFTfilt_inv)>H_max)=0;
    H_abs=abs(FFTfilt).^2;
    S_n=abs(fft2(noise_2)).^2;
    S_f=abs(FFTimag).^2;
    K=S_n./S_f;
    FFTrest=FFTfilt_inv.*(H_abs./(H_abs+K)).*FFTimag;
    image_proc=ifft2(FFTrest);    
 end

 if isreal(FFTimag) && isreal(FFTfilt)
            image_proc = real(image_proc);
 end

image_proc=uint8(image_proc);

axes(handles.Proc_Imag)
imshow(image_proc)
set(handles.Proc_Imag, 'UserData', image_proc)

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


% --- Executes on slider movement.
function slider_Gaussian_Callback(hObject, eventdata, handles)
sliderValue=get(handles.slider_Gaussian, 'Value');
set(handles.text_Gaussian, 'String', num2str(sliderValue));
% hObject    handle to slider_Gaussian (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


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


% --- Executes on slider movement.
function slider_angle_Callback(hObject, eventdata, handles)
sliderValue=get(handles.slider_angle, 'Value');
set(handles.text_angle_static, 'String', num2str(sliderValue));
% hObject    handle to slider_angle (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


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


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


% --- Executes during object creation, after setting all properties.
function edit_vari_orig_im_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_vari_orig_im (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 radiobutton_Horizontal.
function radiobutton_Horizontal_Callback(hObject, eventdata, handles)
if get(handles.radiobutton_Horizontal, 'Value')==1
    set(handles.radiobutton_Vertical, 'Value', 0);
end
% hObject    handle to radiobutton_Horizontal (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 radiobutton_Horizontal


% --- Executes on button press in radiobutton_Vertical.
function radiobutton_Vertical_Callback(hObject, eventdata, handles)
if get(handles.radiobutton_Vertical, 'Value')==1
    set(handles.radiobutton_Horizontal, 'Value', 0);
end
% hObject    handle to radiobutton_Vertical (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 radiobutton_Vertical
