function varargout = KLIP(varargin)
% KLIP M-file for KLIP.fig
%      KLIP, by itself, creates a new KLIP or raises the existing
%      singleton*.
%
%      H = KLIP returns the handle to a new KLIP or the handle to
%      the existing singleton*.
%
%      KLIP('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in KLIP.M with the given input arguments.
%
%      KLIP('Property','Value',...) creates a new KLIP or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before KLIP_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to KLIP_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 KLIP

% Last Modified by GUIDE v2.5 09-Jan-2009 22:33:44

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @KLIP_OpeningFcn, ...
                   'gui_OutputFcn',  @KLIP_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 KLIP is made visible.
function KLIP_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 KLIP (see VARARGIN)


% Read the images to show
white = imread('white.jpg'); % Not for choose, just a start image
Baseball01 = imread('Baseball01.jpg');
Baseball02 = imread('Baseball02.jpg');
DogCar01 = imread('DogCar.jpg');
Fussball01 = imread('Fussball01.jpg');
Keyboard01 = imread('Keyboard01.jpg');
Pout = imread('pout.tif');
Blurred01 = imread('Blurred01.png');
Blurred02 = imread('Blurred02.jpg');

handles.white = white;
handles.Baseball01 = Baseball01;
handles.Baseball02 = Baseball02;
handles.DogCar01 = DogCar01;
handles.Fussball01 = Fussball01;
handles.Keyboard01 = Keyboard01;
handles.Pout = Pout;
handles.Blurred01 = Blurred01;
handles.Blurred02 = Blurred02;

% Set the current image
handles.current_data = handles.white;
axes(handles.axes2);
imshow(handles.current_data);
axes(handles.axes1);
imshow(handles.current_data);

% Choose default command line output for KLIP
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes KLIP wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = KLIP_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 closebutton.
function closebutton_Callback(hObject, eventdata, handles)
% hObject    handle to closebutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
display Goodbye; % Displays 'Goodbye' on the Workspace
close(handles.figure1); % Closes the GUI



% --- Executes on selection change in selectimage.
function selectimage_Callback(hObject, eventdata, handles)
% hObject    handle to selectimage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Determine the selected data set
str = get(hObject, 'String');
val = get(hObject, 'Value');
% Set current data to the selected data set
switch str{val};
    case 'Baseball01'   % User selects Baseball01
        handles.current_data = handles.Baseball01;
        axes(handles.axes2);
    case 'Baseball02'   % User selects Baseball02
        handles.current_data = handles.Baseball02;
        axes(handles.axes2);
    case 'DogCar01'     % User selects DogCar01
        handles.current_data = handles.DogCar01;
        axes(handles.axes2);
    case 'Fussball01'   % User selects Fussbal01
        handles.current_data = handles.Fussball01;
        axes(handles.axes2);
    case 'Keyboard01'   % User selects Keyboard01
        handles.current_data = handles.Keyboard01;
        axes(handles.axes2);
    case 'Pout';        % User selects Pout
        handles.current_data = handles.Pout;
        axes(handles.axes2);
    case 'Blurred01';   % User selects Blurred01
        handles.current_data = handles.Blurred01;
        axes(handles.axes2);
    case 'Blurred02';   % User selects Blurred02
        handles.current_data = handles.Blurred02;
        axes(handles.axes2);
end
% Save the handles structure
guidata(hObject,handles)

% Hints: contents = get(hObject,'String') returns selectimage contents as cell array
%        contents{get(hObject,'Value')} returns selected item from selectimage


% --- Executes during object creation, after setting all properties.
function selectimage_CreateFcn(hObject, eventdata, handles)
% hObject    handle to selectimage (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 OKforCHOOSEaIMAGE.
function OKforCHOOSEaIMAGE_Callback(hObject, eventdata, handles)
% hObject    handle to OKforCHOOSEaIMAGE (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

axes(handles.axes2);
imshow(handles.current_data);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%Radio Buttons%%%%Radio Buttons%%%%Radio Buttons%%%%Radio Buttons%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%Radio Buttons%%%%Radio Buttons%%%%Radio Buttons%%%%Radio Buttons%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%Contrast Buttons%%%%Contrast Buttons%%%%Contrast Buttons%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in histeqradiobutton.
function histeqradiobutton_Callback(hObject, eventdata, handles)
% hObject    handle to histeqradiobutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Check if the array is 2D or 3D
sz2=size(handles.current_data);
sz1=size(sz2);
if sz1(1,2)==3;
    handles.current_data= rgb2gray(handles.current_data);
end;

handles.current_data = histeq(handles.current_data);
axes(handles.axes1);
imshow(handles.current_data);

% Hint: get(hObject,'Value') returns toggle state of histeqradiobutton



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%Edge Detection%%%%Edge Detection%%%%Edge Detection%%%%Edge Detection%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in logradiobutton.
function logradiobutton_Callback(hObject, eventdata, handles)
% hObject    handle to logradiobutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

sz2=size(handles.current_data);
sz1=size(sz2);
if sz1(1,2)==3;
    handles.current_data= rgb2gray(handles.current_data);
end;
handles.current_data = edge(handles.current_data,'log');
axes(handles.axes1);
imshow(handles.current_data);

% Hint: get(hObject,'Value') returns toggle state of logradiobutton


% --- Executes on button press in cannyradiobutton.
function cannyradiobutton_Callback(hObject, eventdata, handles)
% hObject    handle to cannyradiobutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

sz2=size(handles.current_data);
sz1=size(sz2);
if sz1(1,2)==3;
    handles.current_data= rgb2gray(handles.current_data);
end;
%if exist('a') & exist('b') == 0;
%    handles.current_data = edge(handles.current_data,'canny');
%else
%    handles.current_data = edge(handles.current_data,'canny',[a b]);
%end
handles.current_data = edge(handles.current_data,'canny',[]);
axes(handles.axes1);
imshow(handles.current_data);

% Hint: get(hObject,'Value') returns toggle state of cannyradiobutton


% --- Executes on button press in sobelradiobutton.
function sobelradiobutton_Callback(hObject, eventdata, handles)
% hObject    handle to sobelradiobutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

sz2=size(handles.current_data);
sz1=size(sz2);
if sz1(1,2)==3;
    handles.current_data= rgb2gray(handles.current_data);
end;
handles.current_data = edge(handles.current_data,'sobel');
axes(handles.axes1);
imshow(handles.current_data);

% Hint: get(hObject,'Value') returns toggle state of sobelradiobutto


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%Deblurring%%%%Deblurring%%%%Deblurring%%%%Deblurring%%%%Deblurring%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in wienerradiobutton.
function wienerradiobutton_Callback(hObject, eventdata, handles)
% hObject    handle to wienerradiobutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%handles.current_data;
% Create PSF
%LEN = 31;
LEN = 31;
%THETA = 11;
THETA = 20;
PSF = fspecial('motion',LEN,THETA);
% Wiener Filter
handles.current_data = deconvwnr(handles.current_data,PSF);
axes(handles.axes1);
imshow(handles.current_data);

% Hint: get(hObject,'Value') returns toggle state of wienerradiobutton


% --- Executes on button press in regularizedradiobutton.
function regularizedradiobutton_Callback(hObject, eventdata, handles)
% hObject    handle to regularizedradiobutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Create PSF
PSF = fspecial('gaussian',5,5);
Blurred = imfilter(handles.current_data,PSF,'conv');
V=.005;
BlurredNoisy = imnoise(Blurred,'gaussian',0,V);
% Create Noise Power and Regularized Filter
NP = V*prod(size(handles.current_data)); 
[reg1 LAGRA] = deconvreg(BlurredNoisy,PSF,NP);
axes(handles.axes1);
imshow(reg1);

% Hint: get(hObject,'Value') returns toggle state of regularizedradiobutton
