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

% Last Modified by GUIDE v2.5 10-Sep-2009 21:49:16

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Degradacion_Filtrado_OpeningFcn, ...
                   'gui_OutputFcn',  @Degradacion_Filtrado_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 Degradacion_Filtrado is made visible.
function Degradacion_Filtrado_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 Degradacion_Filtrado (see VARARGIN)
movegui(hObject,'center')
set(handles.IMAGEN_ORIGINAL,'Enable','off')
set(handles.APLICAR,'Enable','off')
set(handles.RECUPERAR,'Enable','off')
% Choose default command line output for Degradacion_Filtrado
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Degradacion_Filtrado_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 ABRIR_IMAGEN.
function ABRIR_IMAGEN_Callback(hObject, eventdata, handles)
% hObject    handle to ABRIR_IMAGEN (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[nombre ruta]=uigetfile('*.jpg','Seleccionar imagen');
if nombre == 0
    return
end
% Leer imagen
imagen=imread(fullfile(ruta,nombre));
%image(imagen,'parent',handles.axes1)
set(handles.IMAGEN_ORIGINAL,'Enable','on')
set(handles.APLICAR,'Enable','on')
set(handles.RECUPERAR,'Enable','on')
handles.img_orig=imagen;
handles.RGB=imagen;
guidata(hObject, handles)
% --- Executes on button press in RGB_Binaria.
function RGB_Binaria_Callback(hObject, eventdata, handles)
% hObject    handle to RGB_Binaria (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 RGB_Binaria
val=get(hObject,'Value');
switch val
    case 1
        set(hObject,'String','Binaria')
        imagen=uint8(255*(im2bw(handles.img_orig)));
        colormap gray        
    otherwise
        set(hObject,'String','RGB')
        imagen=handles.img_orig;
end
handles.RGB=imagen;
imagesc(imagen,'parent',handles.axes1)
guidata(hObject,handles)

% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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 popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1
val=get(handles.popupmenu1,'Value');
switch val
    case 1 % Gaussiano
        set(handles.text2,'String','Varianza')
        set(handles.edit1,'String',0)      % Media
        set(handles.edit2,'String',0.01) % Varianza
    case 2 % Poisson
        set(handles.text1,'String','')
        set(handles.text2,'String','')
        set(handles.edit1,'String',0) %Longitud
        set(handles.edit2,'String',0) % Theta
    case 3 % Salt & Pepper
        set(handles.text1,'String','Densidad')
        set(handles.text2,'String','')
        set(handles.edit1,'String',0.05) % Densidad
        set(handles.edit2,'String',0)    
    otherwise % Speckle
        set(handles.text1,'String','Media')
        set(handles.text2,'String','Varianza')
        set(handles.edit1,'String',0)      % Media
        set(handles.edit2,'String',0.04) % Varianza
end

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



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


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


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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 APLICAR.
function APLICAR_Callback(hObject, eventdata, handles)
% hObject    handle to APLICAR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Llamar la imagen
imagen=handles.RGB;
% Obtener el tipo de ruido
valor=get(handles.popupmenu1,'value');
% Obtener la media y la varianza
edit1=str2double(get(handles.edit1,'String'));
edit2=str2double(get(handles.edit2,'String'));
% Control de errores
if edit1<0||isempty(edit1)||isnan(edit1)
    errordlg('Error de ingreso en el primer parametro','ERROR')
    return
end
if valor ~=2 
    if edit2<0||isempty(edit2)||isnan(edit2)
        errordlg('Error de ingreso en el segundo parámetro','ERROR')
        return
    end
end
% Aplicar el ruido
switch valor
    case 1
        J = imnoise(imagen,'gaussian',edit1,edit2);
    case 2
        J = imnoise(imagen,'poisson');
    case 3
        J = imnoise(imagen,'salt & pepper',edit1);
    otherwise
        J = imnoise(imagen,'speckle',edit2);
end
handles.distorsionada=J;
image(J,'parent',handles.axes1)
guidata(hObject,handles)
MSE = mean(mean(mean((imagen-J).^2)));
S = int2str(MSE);
set(handles.ECM_1, 'String', S)
function ECM_1_Callback(hObject, eventdata, handles)
% hObject    handle to ECM_1 (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 ECM_1 as text
%        str2double(get(hObject,'String')) returns contents of ECM_1 as a double

% --- Executes during object creation, after setting all properties.
function ECM_1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ECM_1 (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 selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (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 popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2
val=get(handles.popupmenu2,'Value');
switch val
    case 1 % Average
    case 2 % Disk
    case 3 % Gaussian
    case 4 % Motion    
    otherwise % Unsharp
end


% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (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 RECUPERAR.
function RECUPERAR_Callback(hObject, eventdata, handles)
% hObject    handle to RECUPERAR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
J=handles.distorsionada;
valor=get(handles.popupmenu2,'value');
switch valor
    case 1
        PSF = fspecial('average');%point-spread function
        J1 = imfilter(J,PSF,'circular','conv');
    case 2
        PSF = fspecial('disk');%point-spread function
        J1 = imfilter(J,PSF,'replicate','corr');
    case 3
        PSF = fspecial('gaussian');%point-spread function
        J1 = imfilter(J,PSF,'circular','conv');
    case 4
        PSF = fspecial('motion');%point-spread function
        J1 = imfilter(J,PSF,'circular','conv');
    otherwise
        PSF = fspecial('unsharp');%point-spread function
        J1 = imfilter(J,PSF,'circular','conv');
end
handles.RGB=J1;
image1 = handles.img_orig;
image(J1,'parent',handles.axes2)
guidata(hObject,handles)
MSE = mean(mean(mean((image1-J1).^2)));
S = int2str(MSE);
set(handles.ECM_2, 'String', S)

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


% --- Executes during object creation, after setting all properties.
function ECM_2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ECM_2 (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 SALIR.
function SALIR_Callback(hObject, eventdata, handles)
% hObject    handle to SALIR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ans1=questdlg('¿Desea salir del programa?','SALIR','Si','No','No');
if strcmp(ans1,'No')
return;
end
clear,clc,close all

% --- Executes on button press in IMAGEN_ORIGINAL.
function IMAGEN_ORIGINAL_Callback(hObject, eventdata, handles)
% hObject    handle to IMAGEN_ORIGINAL (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
imagen=handles.img_orig;
handles.RGB=handles.img_orig;
image(imagen,'parent',handles.axes1)
%imshow(imagen,'parent',handles.axes1)
guidata(hObject,handles)

