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

% Last Modified by GUIDE v2.5 20-May-2015 21:52:40

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = gaussian_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 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 Save_as.
function Save_as_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_as (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



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


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


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

image=get(handles.Orig_Imag, 'UserData');
image=double(image)/255; %normaliza los valores
var=str2double(get(handles.edit_variance, 'String'));
mean=str2double(get(handles.edit_mean, 'String'));

gauss=image+sqrt(var)*randn(size(image))+ mean; %suma a la imagen original
                                                %la media y el producto de
                                                %valores pseudoaleatorios
                                                %de la distribucion
                                                %uniforme de la imagen y la
                                                %desviacion estandar
gauss=uint8(gauss*255); %desnormaliza los valores

axes(handles.Proc_Imag)
imshow(gauss)
set(handles.Proc_Imag, 'UserData', gauss)
% 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 button press in Restore.
function Restore_Callback(hObject, eventdata, handles)

image=get(handles.Orig_Imag, 'UserData');
image=double(image)/255; %normaliza los valores
I=padarray(image, [1,1]); %envuelve 1 fila y 1 columna de la matriz de la imagen con 0s
[n,m]=size(I); % devuelve un vector de dos componentes que son el numero de filas y el numero de columnas de la matriz:
h=ones(3); %crea una mascara de unos 3x3
h=(1/9)*h;

image_proc=I;

n_times=get(handles.slider_N_times, 'Value');


for i=2:n-1
    for j=2:m-1
        for z=1:n_times
        image_proc(i,j)=sum(sum(h.*I(i-1:i+1, j-1:j+1))); %suma filas y columnas del producto mascara*pix elcentral y vecino 
                                                          %y lo almacena en
                                                          %el pixel sobre
                                                          %el que estamos
                                                          %trabajando,
                                                          %generando la
                                                          %media
                                                        
        end
    end
end
image_proc=image_proc(2:end-1, 2:end-1); %elimina los bordes creados por el padarray

image_proc=uint8(image_proc*255); %desnormaliza los valores

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



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


% --- Executes during object creation, after setting all properties.
function edit_Density_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_Density (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 Apply_Salt.
function Apply_Salt_Callback(hObject, eventdata, handles)
image=get(handles.Orig_Imag, 'UserData');
image=double(image)/255;
d=str2double(get(handles.edit_Density, 'String'));

image_proc=image;
z=rand(size(image_proc));
a = find(z < d/2); %encuentra los valores pseudoaleatorios menores que la mitad de la densidad
image_proc(a) = 0; % y les asigna 0 - pimienta
b = find(z >= d/2 & z < d); %encuentra los valores pseudoaleatorios mayores que la mitad de la densidad y menores que la densidad entera
image_proc(b) = 1; % y les asigna el valor máximo 1 - sal


image_proc=uint8(image_proc*255);
axes(handles.Proc_Imag)
imshow(image_proc)
set(handles.Proc_Imag, 'UserData', image_proc)
% hObject    handle to Apply_Salt (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 Restore_Salt.
function Restore_Salt_Callback(hObject, eventdata, handles)
image=get(handles.Orig_Imag, 'UserData');
image=double(image)/255;
image=padarray(image,[1,1]);
[n,m]=size(image);
image_proc=image;

n_times=get(handles.slider_N_times, 'Value');


for i=2:n-1
    for j=2:m-1
        for z=1:n_times
        v=image(i-1:i+1, j-1:j+1); %los 9 pixeles
        v=sort(v); %ordena los valores de menor a mayor
        image_proc(i,j)=v(5); %el valor central del vector de 9 pixeles, que es la posicion 5
        end
    end    
end
image_proc=image_proc(2:end-1, 2:end-1); %elimina los bordes creados por el padarray

image_proc=uint8(image_proc*255); %desnormaliza los valores

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

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



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


% --- Executes during object creation, after setting all properties.
function edit_Density_Pepper_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_Density_Pepper (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_N_times_Callback(hObject, eventdata, handles)
sliderValue=get(handles.slider_N_times, 'Value');
set(handles.edit_N_times, 'String', num2str(sliderValue));

% hObject    handle to slider_N_times (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_N_times_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_N_times (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_N_times_Callback(hObject, eventdata, handles)
% hObject    handle to edit_N_times (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_N_times as text
%        str2double(get(hObject,'String')) returns contents of edit_N_times as a double


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