function varargout = Filtrado_Imagenes(varargin)
%Nombres:   Roberto Hoyos y Carlos Silva
%Filtrado_Imagenes
%Interfaz grafica basada en el proyecto de la materia de graduacion
%Procesamiento digital de señales
%El programa carga imagenes les agrega diferentes tipos de ruido
%y al final les aplica diferentes filtros para restaurar las imagenes
%

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

% Last Modified by GUIDE v2.5 25-Feb-2009 15:29:16

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

%variables needed in this program
handles.x=[];              %variable para almacenar la imagen original
handles.pic='lena.jpg';     %variable para el string del nombre de la imagen a cargar
handles.tipo=1;             %variable para el tipo de imagen que se maneja (color o blanco y negro)
handles.ruido=1;            %variable para determinar el tipo de ruido a agregar
handles.d=0;                %variable para la densidad en el tipo de ruido salt & pepper
handles.var=0;              %variable para la varianza en el tipo de ruido gaussiano
handles.var2=0;             %varibale para la varianza en el tipo de ruido gaussiano mas blur
handles.j=[];               %variable para almacenar la variable con el ruido o la distorcion
handles.vw=2;               %variable para la vecindad en la cual sera aplicado el filtro wiener
handles.vm=2;               %variable para la vecindad en la cual sera aplicado el filtro de mediana
handles.restored=[];        %variable para almacenar la imagen restaurada por el filtro wiener
handles.restored2=[];        %variable para almacenar la imagen restaurada por el filtro de mediana
handles.restored3=[];        %variable para almacenar la imagen restaurada por el filtro wiener de deconvolucion
handles.appdeconv=0;        %bandera para saber si es aplicable o no la deconvolucion
handles.PSF=0;              %variable para el point spread function de la distorsion
handles.noise=0;            %ruido que se le agrega a la imagen con distorsion
handles.bandnoise=0;        %bandera que se usa para saber cuando se esta anadiendo ruido a la imagen con distorsion
handles.i=0;

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Filtrado_Imagenes_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 cargar1.
function cargar1_Callback(hObject, eventdata, handles)
% hObject    handle to cargar1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.x=imread(handles.pic);

% Update handles structure
guidata(hObject, handles);


% --- Executes on button press in mostrar1.
function mostrar1_Callback(hObject, eventdata, handles)
% hObject    handle to mostrar1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
figure, imshow(handles.x)
% Update handles structure
guidata(hObject, handles);


% --- Executes on button press in anadir.
function anadir_Callback(hObject, eventdata, handles)
% hObject    handle to anadir (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if(handles.ruido==1)
    handles.j = imnoise(handles.x,'salt & pepper',handles.d);
elseif(handles.ruido==2)
    handles.j = imnoise(handles.x,'gaussian',0,handles.var);
elseif(handles.ruido==3)
    LEN = 31;
    THETA = 11;
    handles.PSF = fspecial('motion',LEN,THETA);
    handles.j = imfilter(handles.x,handles.PSF,'circular','conv');
elseif(handles.ruido==4)
    LEN = 31;
    THETA = 11;
    handles.PSF = fspecial('motion',LEN,THETA);
    handles.i = imfilter(handles.x,handles.PSF,'circular','conv');
    handles.j = imnoise(handles.i,'gaussian',0,handles.var2);
end

MSE = mean(mean(mean((handles.x-handles.j).^2)));
s1='MSE = ';
s2 = int2str(MSE);
S = strcat(s1, s2);
set(handles.mse_noise, 'String', S)

% Update handles structure
guidata(hObject, handles);


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


% --- Executes on button press in filtrar3.
function filtrar3_Callback(hObject, eventdata, handles)
% hObject    handle to filtrar3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if(handles.bandnoise==0)
    if(handles.appdeconv==1)
        handles.restored3 = deconvwnr(handles.j,handles.PSF);
    end
elseif(handles.bandnoise==1)
    if(handles.appdeconv==1)
        noise= (handles.j - handles.i);
        NSR = sum(im2double(noise(:)).^2)/sum(im2double(handles.x(:)).^2);
        handles.restored3 = deconvwnr(handles.j,handles.PSF,NSR/2);
    end
end

MSE = mean(mean(mean((handles.x-handles.restored3).^2)));
s1='MSE = ';
s2 = int2str(MSE);
S = strcat(s1, s2);
set(handles.mse_deconv, 'String', S)

% Update handles structure
guidata(hObject, handles);


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


% --- Executes on button press in filtrar2.
function filtrar2_Callback(hObject, eventdata, handles)
% hObject    handle to filtrar2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if(handles.tipo==1)
    restored_1 = medfilt2(handles.j(:,:,1),[handles.vm handles.vm]);
    restored_2 = medfilt2(handles.j(:,:,2),[handles.vm handles.vm]); 
    restored_3 = medfilt2(handles.j(:,:,3),[handles.vm handles.vm]);
    handles.restored2 =cat( 3, restored_1,restored_2,restored_3);
elseif(handles.tipo==2)
    handles.restored2=medfilt2(handles.j,[handles.vm handles.vm]);
end

MSE = mean(mean(mean((handles.x-handles.restored2).^2)));
s1='MSE = ';
s2 = int2str(MSE);
S = strcat(s1, s2);
set(handles.mse_median, 'String', S)

% Update handles structure
guidata(hObject, handles);


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


% --- Executes on button press in filtrar1.
function filtrar1_Callback(hObject, eventdata, handles)
% hObject    handle to filtrar1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if(handles.tipo==1)
    restored_1 = wiener2(handles.j(:,:,1),[handles.vw handles.vw]);
    restored_2 = wiener2(handles.j(:,:,2),[handles.vw handles.vw]); 
    restored_3 = wiener2(handles.j(:,:,3),[handles.vw handles.vw]);
    handles.restored =cat( 3, restored_1,restored_2,restored_3);
elseif(handles.tipo==2)
    handles.restored=wiener2(handles.j,[handles.vw handles.vw]);
end

MSE = mean(mean(mean((handles.x-handles.restored).^2)));
s1='MSE = ';
s2 = int2str(MSE);
S = strcat(s1, s2);
set(handles.mse_wiener, 'String', S)

% Update handles structure
guidata(hObject, handles);


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


% --- Executes on selection change in vecindad2.
function vecindad2_Callback(hObject, eventdata, handles)
% hObject    handle to vecindad2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns vecindad2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from vecindad2
val=get(hObject,'Value');
str=get(hObject,'String');

switch str{val}
    case '[2 2]'
        handles.vm=2;
    case '[3 3]'
        handles.vm=3;
    case '[5 5]'
        handles.vm=5;
end

% Update handles structure
guidata(hObject, handles);


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

% Hint: popupmenu 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 selection change in vecindad1.
function vecindad1_Callback(hObject, eventdata, handles)
% hObject    handle to vecindad1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns vecindad1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from vecindad1
val=get(hObject,'Value');
str=get(hObject,'String');

switch str{val}
    case '[2 2]'
        handles.vw=2;
    case '[3 3]'
        handles.vw=3;
    case '[5 5]'
        handles.vw=5;
end

% Update handles structure
guidata(hObject, handles);



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

% Hint: popupmenu 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 selection change in imagenes.
function imagenes_Callback(hObject, eventdata, handles)
% hObject    handle to imagenes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns imagenes contents as cell array
%        contents{get(hObject,'Value')} returns selected item from imagenes
val=get(hObject,'Value');
str=get(hObject,'String');

switch str{val}
    case 'lena'
        handles.pic='lena.jpg';
    case 'boats'
        handles.pic='boats.jpg';
    case 'lena color'
        handles.pic='lena_color.jpg';
    case 'saturno'
        handles.pic='saturno1.jpg';
    case 'andromeda'
        handles.pic='andromeda.jpg';
    case 'test pattern'
        handles.pic='Test_pattern.jpg';
end
% Update handles structure
guidata(hObject, handles);


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

% Hint: popupmenu 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 selection change in varianza1.
function varianza1_Callback(hObject, eventdata, handles)
% hObject    handle to varianza1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns varianza1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from varianza1
val=get(hObject,'Value');
str=get(hObject,'String');

switch str{val}
    case '0.01'
        handles.var=0.01;
    case '0.05'
        handles.var=0.05;
    case '0.15'
        handles.var=0.15;
end

% Update handles structure
guidata(hObject, handles);


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

% Hint: popupmenu 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 selection change in densidad.
function densidad_Callback(hObject, eventdata, handles)
% hObject    handle to densidad (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns densidad contents as cell array
%        contents{get(hObject,'Value')} returns selected item from densidad
val=get(hObject,'Value');
str=get(hObject,'String');

switch str{val}
    case '0.005'
        handles.d=0.005;
    case '0.02'
        handles.d=0.02;
    case '0.05'
        handles.d=0.05;
end

% Update handles structure
guidata(hObject, handles);


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

% Hint: popupmenu 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 selection change in varianza2.
function varianza2_Callback(hObject, eventdata, handles)
% hObject    handle to varianza2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns varianza2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from varianza2
val=get(hObject,'Value');
str=get(hObject,'String');

switch str{val}
    case '0.01'
        handles.var2=0.01;
    case '0.05'
        handles.var2=0.05;
    case '0.15'
        handles.var2=0.15;
end

% Update handles structure
guidata(hObject, handles);


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

% Hint: popupmenu 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 color.
function color_Callback(hObject, eventdata, handles)
% hObject    handle to color (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 color
color=get(hObject,'Value');
if (color==1)
    handles.tipo=1;
end

% Update handles structure
guidata(hObject, handles);


% --- Executes on button press in BnN.
function BnN_Callback(hObject, eventdata, handles)
% hObject    handle to BnN (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 BnN
bnn=get(hObject,'Value');
if (bnn==1)
    handles.tipo=2;
end

% Update handles structure
guidata(hObject, handles);


% --- Executes on button press in saltnpepper.
function saltnpepper_Callback(hObject, eventdata, handles)
% hObject    handle to saltnpepper (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 saltnpepper
handles.ruido=1;
handles.appdeconv=0;
handles.bandnoise=0;
% Update handles structure
guidata(hObject, handles);


% --- Executes on button press in gaussian.
function gaussian_Callback(hObject, eventdata, handles)
% hObject    handle to gaussian (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 gaussian
handles.ruido=2;
handles.appdeconv=0;
handles.bandnoise=0;
% Update handles structure
guidata(hObject, handles);


% --- Executes on button press in blur.
function blur_Callback(hObject, eventdata, handles)
% hObject    handle to blur (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 blur
handles.ruido=3;
handles.appdeconv=1;
handles.bandnoise=0;
% Update handles structure
guidata(hObject, handles);


% --- Executes on button press in blurngaussian.
function blurngaussian_Callback(hObject, eventdata, handles)
% hObject    handle to blurngaussian (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 blurngaussian
handles.ruido=4;
handles.appdeconv=1;
handles.bandnoise=1;
% Update handles structure
guidata(hObject, handles);



