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

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help progetto

% Last Modified by GUIDE v2.5 13-Nov-2007 23:52:51

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = progetto_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;


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


% --- Executes during object creation, after setting all properties.
function editfile_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfile (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 during object creation, after setting all properties.
function lb_SelezionaFiltro_CreateFcn(hObject, eventdata, handles)
% hObject    handle to lb_SelezionaFiltro (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox 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 lb_SelezionaFiltro.
function lb_SelezionaFiltro_Callback(hObject, eventdata, handles)
% hObject    handle to lb_SelezionaFiltro (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 lb_SelezionaFiltro contents as cell array
%        contents{get(hObject,'Value')} returns selected item from lb_SelezionaFiltro
nindex = get(hObject,'Value');

% evidenzia parametri interessati dal editfiltro
blu=[0,0,1];
nero=[0,0,0];


%set(handles.text_slider1,'ForegroundColor',blu);
abilita(handles.slider1,handles.text_slider1,blu,'on');
switch nindex                                      % opzione filtro selezionato    
    case {1,3,4,6}                                 % Ideale,Gauss
        abilita(handles.slider2,handles.text_slider2,nero,'off');
        abilita(handles.slider3,handles.text_slider3,nero,'off');
        abilita(handles.slider6,handles.text_slider6,nero,'off');
        abilita(handles.slider7,handles.text_slider7,nero,'off');
        abilita(handles.slider8,handles.text_slider8,nero,'off');
        
    case {2,5}                                     % ButterWort
        abilita(handles.slider1,handles.text_slider1,blu,'on');
        abilita(handles.slider3,handles.text_slider2,blu,'on');
        abilita(handles.slider2,handles.text_slider3,nero,'off');
        abilita(handles.slider6,handles.text_slider6,nero,'off');
        abilita(handles.slider7,handles.text_slider7,nero,'off');
        abilita(handles.slider8,handles.text_slider8,nero,'off');
    case 7                                         % Laplaciano
        abilita(handles.slider1,handles.text_slider1,nero,'off');
        abilita(handles.slider2,handles.text_slider2,nero,'off');
        abilita(handles.slider3,handles.text_slider3,nero,'off');
        abilita(handles.slider6,handles.text_slider6,nero,'off');
        abilita(handles.slider7,handles.text_slider7,nero,'off');
        abilita(handles.slider8,handles.text_slider8,nero,'off');
    case 8                                         % Band-Reject    
        abilita(handles.slider3,handles.text_slider2,nero,'off');
        abilita(handles.slider2,handles.text_slider3,blu,'on');
        abilita(handles.slider6,handles.text_slider6,nero,'off');
        abilita(handles.slider7,handles.text_slider7,nero,'off');
        abilita(handles.slider8,handles.text_slider8,nero,'off');
    case 9                                         % High Bost Filtering su Gaussiano
        abilita(handles.slider2,handles.text_slider2,nero,'off');
        abilita(handles.slider3,handles.text_slider3,nero,'off');
        abilita(handles.slider6,handles.text_slider6,blu,'on');
        abilita(handles.slider7,handles.text_slider7,nero,'off');
        abilita(handles.slider8,handles.text_slider8,nero,'off');
    case 10                                        % High Frequence Emphasiss su Gaussiano
        abilita(handles.slider2,handles.text_slider2,nero,'off');
        abilita(handles.slider3,handles.text_slider3,nero,'off');
        abilita(handles.slider6,handles.text_slider6,nero,'off');
        abilita(handles.slider7,handles.text_slider7,blu,'on');
        abilita(handles.slider8,handles.text_slider8,blu,'on');
    case {11,12}                                   % Sobel Prewitt
        abilita(handles.slider1,handles.text_slider1,nero,'off');
        abilita(handles.slider2,handles.text_slider2,nero,'off');
        abilita(handles.slider3,handles.text_slider3,nero,'off');
        abilita(handles.slider6,handles.text_slider6,nero,'off');
        abilita(handles.slider7,handles.text_slider7,nero,'off');
        abilita(handles.slider8,handles.text_slider8,nero,'off');
end

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (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 slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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
D0 = int16(get(hObject,'Value'));
set(handles.edit_slider1,'string',D0);

% --- Executes during object creation, after setting all properties.
function edit_slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_slider1 (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_slider1_Callback(hObject, eventdata, handles)
% hObject    handle to edit_slider1 (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_slider1 as text
%        str2double(get(hObject,'String')) returns contents of edit_slider1 as a double
D0 = get(hObject,'string');
set(handles.slider1,'value',str2num(D0));

% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (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 slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (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
D1 = int16(get(hObject,'Value'));
set(handles.edit_slider2,'string',D1);


% --- Executes during object creation, after setting all properties.
function edit_slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_slider2 (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_slider2_Callback(hObject, eventdata, handles)
% hObject    handle to edit_slider2 (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_slider2 as text
%        str2double(get(hObject,'String')) returns contents of edit_slider2 as a double
D1 = get(hObject,'string');
set(handles.slider2,'value',str2num(D1));


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

%val = str2num(get(hObject,'String'));
%if isnumeric(val) 
%   set(handles.slider3,'Value',val)
%end 
D1 = get(hObject,'string');
set(handles.slider3,'value',str2num(D1));

% --- Executes during object creation, after setting all properties.
function slider3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider3 (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 slider3_Callback(hObject, eventdata, handles)
% hObject    handle to slider3 (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
D1 = int8(get(hObject,'Value'));
set(handles.edit_slider3,'String',D1);


% --- Executes on button press in pb_elabora.
function pb_elabora_Callback(hObject, eventdata, handles)
% hObject    handle to pb_elabora (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
filtro(handles);


% --- Executes on button press in pb_about.
function pb_about_Callback(hObject, eventdata, handles)
% hObject    handle to pb_about (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.text_about,'Visible','off');
set(handles.pb_about,'Visible','off');

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


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


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


% --------------------------------------------------------------------
function Open_Callback(hObject, eventdata, handles)
% 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)

abilita2 = get(handles.togglebutton2,'Enable');  %abilita/disabilita bottoni
abilita3 = get(handles.togglebutton3,'Enable');  %abilita/disabilita bottoni
if strncmpi(abilita2,'on',2) &  strncmpi(abilita3,'on',2) 
    %[FileName,PathName] = uigetfile({'*.bmp';'*.jpg';'*.gif'},'Selezionare un file immagine');
    [FileName,PathName] = uigetfile('*.*','Selezionare un file immagine');
    if FileName > 0                         % se nome file valido
        c=strcat(PathName,FileName);
        set(handles.editfile,'string',c);
        f=imread(c);                        % carica file
        axes(handles.axes1);
        imshow(f);   

        axes(handles.axes2);                % resetta immagini minimonitor
        newplot;
        axes(handles.axes3);
        newplot;
        axes(handles.axes4);
        newplot;
        axes(handles.axes5);
        newplot;
        set(handles.togglebutton2,'Enable','off');  %disabilita bottoni switch
        set(handles.togglebutton3,'Enable','off');
        set(handles.togglebutton4,'Enable','off');
        set(handles.togglebutton5,'Enable','off');
 
        handles.lprimaelab = 0;            % setta variabile per prima elaborazione
        handles.video=1;
        
        % evidenzia parametri interessati dal editfiltro
        blu=[0,0,1];
        nero=[0,0,0];
        abilita(handles.slider1,handles.text_slider1,blu,'on');
        abilita(handles.slider2,handles.text_slider2,nero,'off');
        abilita(handles.slider3,handles.text_slider3,nero,'off');
        abilita(handles.slider6,handles.text_slider6,nero,'off');
        abilita(handles.slider7,handles.text_slider7,nero,'off');
        abilita(handles.slider8,handles.text_slider8,nero,'off');
        
        guidata(hObject, handles);         % Salva struttura handles
    end
else
    errordlg('Prima di caricare una nuova immagine occorre ripristinare la visualizzazione originaria dei monitor, riportando sul monitor principale l immagine filtrata (click su unico bottone switch-monitor abilitato)');
    return ;    
end 

% --------------------------------------------------------------------
function Close_Callback(hObject, eventdata, handles)
% hObject    handle to Close (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
delete(handles.figure1);


% --- Executes on slider movement.
function slider4_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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
D1 = get(handles.slider5,'Value');
D2 = get(handles.slider4,'Value');
view( D1,D2);

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


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_slider1 (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 slider5_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (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
D1 = get(handles.slider5,'Value');
D2 = get(handles.slider4,'Value');
view( D1,D2);

% --- Executes during object creation, after setting all properties.
function slider5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (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 edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit_slider2 (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_slider2 as text
%        str2double(get(hObject,'String')) returns contents of edit_slider2 as a double
D1 = get(hObject,'string');
set(handles.slider6,'value',str2num(D1));


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_slider2 (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 edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit_slider3 (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_slider3 as text
%        str2double(get(hObject,'String')) returns contents of edit_slider3 as a double
D1 = get(hObject,'string');
set(handles.slider7,'value',str2num(D1));


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_slider3 (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 during object creation, after setting all properties.
function edit_slider6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_slider6 (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_slider6_Callback(hObject, eventdata, handles)
% hObject    handle to edit_slider6 (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_slider6 as text
%        str2double(get(hObject,'String')) returns contents of edit_slider6 as a double
D1 = get(hObject,'string');
set(handles.slider6,'value',str2num(D1));


% --- Executes during object creation, after setting all properties.
function slider7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider7 (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 slider7_Callback(hObject, eventdata, handles)
% hObject    handle to slider7 (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
D1 = double(get(hObject,'Value'));
D1=(round(D1*100)/100);
set(handles.edit_slider7,'string',D1);


% --- Executes during object creation, after setting all properties.
function edit_slider7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_slider7 (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_slider7_Callback(hObject, eventdata, handles)
% hObject    handle to edit_slider7 (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_slider7 as text
%        str2double(get(hObject,'String')) returns contents of edit_slider7 as a double
D1 = get(hObject,'string');
set(handles.slider7,'value',str2num(D1));


% --- Executes during object creation, after setting all properties.
function slider8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider8 (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 slider8_Callback(hObject, eventdata, handles)
% hObject    handle to slider8 (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
D1 = double(get(hObject,'Value'));
D1=(round(D1*100)/100);
set(handles.edit_slider8,'string',D1);



% --- Executes during object creation, after setting all properties.
function edit_slider8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_slider8 (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_slider8_Callback(hObject, eventdata, handles)
% hObject    handle to edit_slider8 (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_slider8 as text
%        str2double(get(hObject,'String')) returns contents of edit_slider8 as a double
D1 = get(hObject,'string');
set(handles.slider8,'value',str2num(D1));


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

D1 = double(get(hObject,'Value'));
D1=(round(D1*100)/100);
set(handles.edit_slider6,'string',D1);


% --------------------------------------------------------------------
function About2_Callback(hObject, eventdata, handles)
% hObject    handle to About2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.text_about,'Visible','on');
set(handles.pb_about,'Visible','on');


% --- Executes on button press in togglebutton2.
function togglebutton2_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton2 (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 togglebutton2
h=handles.axes1;                                %scambia handle
handles.axes1=handles.axes2;
handles.axes2=h;

abilita = get(handles.togglebutton3,'Enable');  %abilita/disabilita bottoni
if strncmpi(abilita,'on',2)
    set(handles.togglebutton3,'Enable','off');
    set(handles.togglebutton4,'Enable','off');
    set(handles.togglebutton5,'Enable','off');
    handles.video=2;
else
    set(handles.togglebutton3,'Enable','on');
    set(handles.togglebutton4,'Enable','on');
    set(handles.togglebutton5,'Enable','on');
    handles.video=1;
end    

handles.lprimaelab = -1;                         % setta variabile per ricarica immagine originale
guidata(hObject,handles);                        % registra in struttura handles
visualizzatori(handles);                         % rivisualizza immagini sui display


% --- Executes on button press in togglebutton2.
function togglebutton3_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton2 (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 togglebutton2
h=handles.axes1;
handles.axes1=handles.axes3;
handles.axes3=h;

abilita = get(handles.togglebutton4,'Enable');
if strncmpi(abilita,'on',2)
    set(handles.togglebutton2,'Enable','off');
    set(handles.togglebutton4,'Enable','off');
    set(handles.togglebutton5,'Enable','off');
    handles.video=3;
else
    set(handles.togglebutton2,'Enable','on');
    set(handles.togglebutton4,'Enable','on');
    set(handles.togglebutton5,'Enable','on');
    handles.video=1;
end
handles.lprimaelab = -1;                         % setta variabile per ricarica immagine originale
guidata(hObject,handles);                        % registra in struttura handles
visualizzatori(handles);                         % rivisualizza immagini sui display

% --- Executes on button press in togglebutton4.
function togglebutton4_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton4 (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 togglebutton4
h=handles.axes1;
handles.axes1=handles.axes4;
handles.axes4=h;

abilita = get(handles.togglebutton3,'Enable');
if strncmpi(abilita,'on',2)
    set(handles.togglebutton3,'Enable','off');
    set(handles.togglebutton2,'Enable','off');
    set(handles.togglebutton5,'Enable','off');
    handles.video=4;
else
    set(handles.togglebutton3,'Enable','on');
    set(handles.togglebutton2,'Enable','on');
    set(handles.togglebutton5,'Enable','on');
    handles.video=1;
end    

guidata(hObject,handles);                        % registra in struttura handles
visualizzatori(handles);                         % rivisualizza immagini sui display


% --- Executes on button press in togglebutton5.
function togglebutton5_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton5 (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 togglebutton5
h=handles.axes1;
handles.axes1=handles.axes5;
handles.axes5=h;

abilita = get(handles.togglebutton3,'Enable');
if strncmpi(abilita,'on',2)
    set(handles.togglebutton3,'Enable','off');
    set(handles.togglebutton4,'Enable','off');
    set(handles.togglebutton2,'Enable','off');
    set(handles.slider4,'Visible','on');
    set(handles.slider5,'Visible','on');
    handles.video=5;
    set(handles.pb_zoom,'Enable','off');
else
    set(handles.togglebutton3,'Enable','on');
    set(handles.togglebutton4,'Enable','on');
    set(handles.togglebutton2,'Enable','on');
    set(handles.slider4,'Visible','off');
    set(handles.slider5,'Visible','off');
    handles.video=1;
    set(handles.pb_zoom,'Enable','on');
end
guidata(hObject,handles);                        % registra in struttura handles 
visualizzatori(handles);                         % rivisualizza immagini sui display

% --------------------------------------------------------------------
function  filtro(handles);
% effettua l'elaborazione dell'immagine 
% handles   struttura puntatori oggetti grafici

cFile=get(handles.editfile,'string');               % carica immagine originale
[s1 s2]=size(cFile);                                % se nomefile valido
if  s2 < 1 
   errordlg('Scegliere un file valido (Menu\File\Open)');
   return ;
end

set(gcbf,'Pointer','watch');                        % attiva indicatori di elaborazione in corso
set(handles.text_elaborazione,'Visible','on');
drawnow;                                            % forza aggiornamento schermo
f=double(imread(cFile));                            % carica immagine

nindex = get(handles.lb_SelezionaFiltro, 'Value');  % opzione filtro
D0 = get(handles.slider1,'Value');                  % freq.taglio D0

PQ=paddedsize(size(f));                             % setta dim. immagine, vedere paddedsize 

F=(fft2(f,PQ(1),PQ(2)));                            % calcolo Trasf.Fourier
Fc=fftshift(F);                                     % shift
S=abs(Fc);
c=255/log(max(S(:)));
S2=c.*log(1.*S);                                    % compressione logaritmica

M=PQ(1);N=PQ(2);                                    % riordino indici
u=0:(M-1);
v=0:(N-1);
    idx=find(u>M/2);
    u(idx)=u(idx)-M;
    idy=find(v>N/2);
    v(idy)=v(idy)-N;
[V,U]=meshgrid(v,u);

D=sqrt(U.^2+V.^2);                                  % calcolo distanza dal centro
switch nindex                                       % opzione filtro selezionato    
    case 1                                          % Lowpass Ideale
        H=double(D<=D0);                            
        Hdesc='H=double(D<=D0)';
    case 2                                          % Lowpass ButterWort
        n =  get(handles.slider3,'Value');          % n ordine 
        H=1./(1+(D./D0).^(2*n));                    
        Hdesc='H=1./(1+(D./D0).^(2*n))';
        
    case 3                                          % Lowpass Gaussiano
        H=exp(-(D.^2)./(2*(D0^2)));                 
        Hdesc='H=exp(-(D.^2)./(2*(D0^2)))';
    case 4                                          % Highpass Ideale
        H=double(D>D0);                             
        Hdesc='H=double(D>D0)';
    case 5                                          % Highpass ButterWort
        n =  get(handles.slider3,'Value');          % n ordine 
        H=1-(1./(1+(D./D0).^(2*n)));                
        Hdesc='H=1-(1./(1+(D./D0).^(2*n)))';
    case 6                                          % Highpass Gaussiano
        H=1-(exp(-(D.^2)./(2*(D0^2))));             
        Hdesc='H=1-(exp(-(D.^2)./(2*(D0^2))))';
    case 7                                          % Highpass Laplaciano
        H=-(U.^2+V.^2);                             
        Hdesc='H=-(U.^2+V.^2)';
    case 8                                          % Band-Reject    
        D1 = get(handles.slider2,'Value');          % Freq.taglio D1 
        H=double((D>=D0)&(D<=(D1)));                
        Hdesc='H=double((D>=D0)&(D<=(D1)))';
    case 9                                          % High Bost Filtering su Gaussiano
        A =  str2num(get(handles.edit_slider6,'String'));                  
        H=1-(exp(-(D.^2)./(2*(D0^2))));              
        H=(A-1)+H;
        Hdesc='H=(A-1)+H1;H1=1-(exp(-(D.^2)./(2*(D0^2))))';
    case 10                                         % High Frequence Emphasiss su Gaussiano
        a =  str2num(get(handles.edit_slider7,'String'));
        b =  str2num(get(handles.edit_slider8,'String'));
        H=1-(exp(-(D.^2)./(2*(D0^2))));              
        H=a+b*H; 
        Hdesc='H=a+b*H1;H1=1-(exp(-(D.^2)./(2*(D0^2))))';
    case 11                                         % Sobel
        h=fspecial('sobel');                        
        H=freqz2(h,PQ(1),PQ(2));
        F=fft2(f,size(H,1),size(H,2));
        Hdesc='H=fspecial(sobel)';
    case 12                                         % Prewitt
        h=fspecial('prewitt');                      
        H=freqz2(h,PQ(1),PQ(2));
        F=fft2(f,size(H,1),size(H,2));
        Hdesc='H=fspecial(prewitt)';
end

set(handles.editfiltro,'string',Hdesc);             % visualizza formula filtro
G=H.*F;                                             % calcolo immagine filtrata
g=real(ifft2(G));

g=g(1:size(f,1), 1:size(f,2));                      % ridefinisce size immagine filtrata
if nindex < 11 
    H=fftshift(H);
    H=real(H);
else                                                % Sobel e Prewitt
    H=freqz2(h,32,32);
    H=abs(H);
    handles.imagofiltro2=h;
end     

handles.imago=f;                                    % setta e salva in struttura handles
handles.imagofiltrata=g;
handles.imagospettro=S2;
handles.imagofiltro=H;
guidata(handles.figure1, handles);               

visualizzatori(handles);                            % visualizza prodotti elaborazione

set(handles.text_elaborazione,'Visible','off');
set(gcf,'Pointer','arrow');
%----------------------------------------------------

function visualizzatori(handles)
% effettua caricamento immagini prodotte dall'elaborazione nei 5 visualizzatori 

nindex = get(handles.lb_SelezionaFiltro, 'Value');  % opzione filtro
D0 = get(handles.slider1,'Value');                  % freq.taglio D0

axes(handles.axes1);                                % seleziona monitor di visualizzazione
imshow(handles.imagofiltrata,[]);                   % Visualizza elaborazione

if handles.lprimaelab < 1                           % esegue x la prima elaborazione e switch monitor 2 e 3
   
   axes(handles.axes2);                             % visualizza immagine originale
   imshow(handles.imago,[]);
   axes(handles.axes3);                             % visualizza Spettro di Fourier
   imshow(handles.imagospettro,[]);
   
   if handles.lprimaelab == 0                       % esegue solo la prima elaborazione
      set(handles.togglebutton2,'Enable','on');     % abilita bottoni swapmonitor
      set(handles.togglebutton3,'Enable','on');
      set(handles.togglebutton4,'Enable','on');
      set(handles.togglebutton5,'Enable','on');
   end 
   handles.lprimaelab = 1;
   guidata(handles.figure1, handles);               % setta e salva in struttura handles
end 

axes(handles.axes4);                                % visualizza filtro come immagine 
if nindex < 11 
    imshow(real(handles.imagofiltro),[]);
else                                                % Sobel e Prewitt
    imshow(abs(handles.imagofiltro),[]);
end     

axes(handles.axes5);                                % visualizza filtro 3-D
if nindex < 11 
    k=int16(D0/4);
    mesh(handles.imagofiltro(1:k:end,1:k:end));
    axis  tight ;
else                                                % Sobel e Prewitt
    freqz2(handles.imagofiltro2,[32 32]); 
end
% --------------------------------------------------------------------

function PQ = paddedsize(AB,CD,PARAM)
% PADDEDSIZE compute padded sizes
% PQ = PADDEDSIZE(AB), where AB is a two-element size vector, 
% computes the two-element  size vector PQ=2*AB  
% 
% PQ = PADDEDSIZE(AB,CD) , where AB and CD is a two-element size vector, 
% computes the two-element size vector PQ. The element of PQ are the
% smallest even integers greater than or equal to  AB+CD-1

if nargin == 1
   PQ = 2*AB;
   %elseif nargin == 2 & -ischar(CD)
   %PQ = AB + CD - 1;
   %PQ = 2* ceil(PQ/2);
elseif nargin == 2
   m = max(AB); 		                            % max dimensione 	
   P = 2^nextpow2(2*m);  	                        % trova potenza del 2 almeno doppia di m
   PQ= [P,P];
elseif nargin == 3
   m=max([AB CD]);                                  % max dimensione
   P = 2^nextpow2(2*m);  	                        % trova potenza del 2 almeno doppia di m
   PQ= [P,P];
else
   error('troppi argomenti in input');
end		

% --------------------------------------------------------------------
function abilita(handle_slider,handle_textslider,colore,abilita);
  set(handle_textslider,'ForegroundColor',colore);
  set(handle_textslider,'Enable',abilita);
  set(handle_slider,'Enable',abilita);

% --------------------------------------------------------------------


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


% --- Executes during object creation, after setting all properties.
function editfiltro_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfiltro (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 pb_zoom.
function pb_zoom_Callback(hObject, eventdata, handles)
% hObject    handle to pb_zoom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
switch handles.video
   case 1     
    imtool(handles.imagofiltrata,[]);
    case 2
    imtool(handles.imago,[]);
   case 3 
    imtool(handles.imagospettro,[]);
   case 4     
    imtool(handles.imagofiltro,[]);
   %case 5     
end


% *******************           end                ************************
% *************************************************************************




