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

% Last Modified by GUIDE v2.5 26-Jan-2006 21:53:40

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = gui_ica_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global S
global r1 c1
global ims1 ims2

set(handles.axes1, 'Units', 'pixels');
set(handles.axes2, 'Units', 'pixels');

ims1 = dyaddown(im2double(rgb2gray(imread('C:\MATLAB7\toolbox\images\imdemos\football.jpg'))),'m');
ims2 = dyaddown(im2double(rgb2gray(imread('C:\MATLAB7\toolbox\images\imdemos\greens.jpg'  ))),'m');

[r1,c1] = size(ims1); [r2,c2] = size(ims2);
ims2(r1+1:r2,:) = []; ims2(:,c1+1:c2) = []; clear r2 c2

datalen = r1*c1;
s1 = reshape(ims1, 1, datalen); 
s2 = reshape(ims2, 1, datalen); 
S = [s1; s2];

info = imfinfo('C:\MATLAB7\toolbox\images\imdemos\football.jpg');
position1 = get(handles.axes1, 'Position');
set(handles.axes1, 'Position', [position1(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes1);
handles.axes1 = imshow(ims1, [min(s1), max(s1)]);
set(handles.axes1);

position2 = get(handles.axes2, 'Position');
set(handles.axes2, 'Position', [position2(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes2);
handles.axes2 = imshow(ims2, [min(s2), max(s2)]);
set(handles.axes2);

set(handles.edit1, 'String', '  orginalna slika 1');
set(handles.edit2, 'String', '  orginalna slika 2');
set(handles.edit3, 'String', '');
set(handles.edit4, 'String', '');

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global S X
global r1 c1
global s1 s2 ims1 ims2
global x1 x2 imx1 imx2

set(handles.axes1, 'Units', 'pixels');
set(handles.axes2, 'Units', 'pixels');
set(handles.axes3, 'Units', 'pixels');
set(handles.axes4, 'Units', 'pixels');

A = [1 2; 1 1]; % matrica mijesanja
X = A * S;

% jednodimenzionalni vektori
x1 = X(1,:); x2 = X(2,:); 

% formiranje i prikaz izmijesanih slika
imx1 = reshape(x1, r1, c1); 
imx2 = reshape(x2, r1, c1);

info = imfinfo('C:\MATLAB7\toolbox\images\imdemos\football.jpg');
position1 = get(handles.axes1, 'Position');
set(handles.axes1, 'Position', [position1(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes1);
handles.axes1 = imshow(ims1, [min(s1), max(s1)]);
set(handles.axes1);

position2 = get(handles.axes2, 'Position');
set(handles.axes2, 'Position', [position2(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes2);
handles.axes2 = imshow(ims2, [min(s2), max(s2)]);
set(handles.axes2);

position3 = get(handles.axes3, 'Position');
set(handles.axes3, 'Position', [position3(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes3);
handles.axes3 = imshow(imx1, [min(x1), max(x1)]);
set(handles.axes3);

position4 = get(handles.axes4, 'Position');
set(handles.axes4, 'Position', [position4(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes4);
handles.axes4 = imshow(imx2, [min(x2), max(x2)]);
set(handles.axes4);

set(handles.edit1, 'String', '  orginalna slika 1');
set(handles.edit2, 'String', '  orginalna slika 2');
set(handles.edit3, 'String', '  senzor 1');
set(handles.edit4, 'String', '  senzor 2');

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global X Z
global r1 c1
global x1 x2 imx1 imx2
global z1 z2 imz1 imz2

set(handles.axes1, 'Units', 'pixels');
set(handles.axes2, 'Units', 'pixels');
set(handles.axes3, 'Units', 'pixels');
set(handles.axes4, 'Units', 'pixels');

Xcov = cov(X');             % kovarijancijska matrica 2 x 2
[Xv, Xd] = eig(Xcov);       % eigen dekompozicija
Vx = -diag(diag(Xd).^(-1/2)) * Xv';    % dekorelacija i skaliranje signala (var = 1)

Z = Vx * X;
z1 = Z(1,:); z2 = Z(2,:);

imz1 = reshape(z1, r1, c1); 
imz2 = reshape(z2, r1, c1);

info = imfinfo('C:\MATLAB7\toolbox\images\imdemos\football.jpg');
position1 = get(handles.axes1, 'Position');
set(handles.axes1, 'Position', [position1(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes1);
handles.axes1 = imshow(imx1, [min(x1), max(x1)]);
set(handles.axes1);

position2 = get(handles.axes2, 'Position');
set(handles.axes2, 'Position', [position2(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes2);
handles.axes2 = imshow(imx2, [min(x2), max(x2)]);
set(handles.axes2);

position3 = get(handles.axes3, 'Position');
set(handles.axes3, 'Position', [position3(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes3);
handles.axes3 = imshow(imz1, [min(z1), max(z1)]);
set(handles.axes3);

position4 = get(handles.axes4, 'Position');
set(handles.axes4, 'Position', [position4(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes4);
handles.axes4 = imshow(imz2, [min(z2), max(z2)]);
set(handles.axes4);

set(handles.edit1, 'String', '  senzor 1');
set(handles.edit2, 'String', '  senzor 2');
set(handles.edit3, 'String', '  senzor 1 nakon PCA');
set(handles.edit4, 'String', '  senzor 2 nakon PCA');

% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Z Zr
global r1 c1
global z1 z2 imz1 imz2
global zr1 zr2 imzr1 imzr2
global z1kurt z2kurt sumAbsKurt
global thetar

set(handles.axes1, 'Units', 'pixels');
set(handles.axes2, 'Units', 'pixels');
set(handles.axes3, 'Units', 'pixels');
set(handles.axes4, 'Units', 'pixels');

z1kurt = zeros(360,1); 
z2kurt = zeros(360,1); 
theta = [-pi: 2*pi/360: pi-2*pi/360];

for i = [1:360]
    R = [cos(theta(i)) -sin(theta(i)); sin(theta(i)) cos(theta(i))];
    Zr = R * Z;
    z1kurt(i) = kurtosis(Zr(1,:))-3;
    z2kurt(i) = kurtosis(Zr(2,:))-3;
end

sumAbsKurt = abs(z1kurt)+abs(z2kurt);

[maksimum, ind] = max(sumAbsKurt);
thetar = theta(ind(1));
R = [cos(thetar) -sin(thetar); sin(thetar) cos(thetar)];
Zr = R * Z;
zr1 = -Zr(1,:); 
zr2 = -Zr(2,:);
imzr1 = reshape(zr1, r1, c1); 
imzr2 = reshape(zr2, r1, c1);

info = imfinfo('C:\MATLAB7\toolbox\images\imdemos\football.jpg');
position1 = get(handles.axes1, 'Position');
set(handles.axes1, 'Position', [position1(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes1);
handles.axes1 = imshow(imz1, [min(z1), max(z1)]);
set(handles.axes1);

position2 = get(handles.axes2, 'Position');
set(handles.axes2, 'Position', [position2(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes2);
handles.axes2 = imshow(imz2, [min(z2), max(z2)]);
set(handles.axes2);

position3 = get(handles.axes3, 'Position');
set(handles.axes3, 'Position', [position3(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes3);
handles.axes3 = imshow(imzr1, [min(zr1), max(zr1)]);
set(handles.axes3);

position4 = get(handles.axes4, 'Position');
set(handles.axes4, 'Position', [position4(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes4);
handles.axes4 = imshow(imzr2, [min(zr2), max(zr2)]);
set(handles.axes4);

set(handles.edit1, 'String', '  senzor 1 nakon PCA');
set(handles.edit2, 'String', '  senzor 2 nakon PCA');
set(handles.edit3, 'String', '  senzor 1 nakon PCA i ICA');
set(handles.edit4, 'String', '  senzor 2 nakon PCA i ICA');

% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global r1 c1
global zr1 zr2 imzr1 imzr2

set(handles.axes1, 'Units', 'pixels');
set(handles.axes2, 'Units', 'pixels');
set(handles.axes3, 'Units', 'pixels');
set(handles.axes4, 'Units', 'pixels');

info = imfinfo('C:\MATLAB7\toolbox\images\imdemos\football.jpg');
position1 = get(handles.axes1, 'Position');
set(handles.axes1, 'Position', [position1(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes1);
handles.axes1 = imshow(imzr1, [min(zr1), max(zr1)]);
set(handles.axes1);

position2 = get(handles.axes2, 'Position');
set(handles.axes2, 'Position', [position2(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes2);
handles.axes2 = imshow(imzr2, [min(zr2), max(zr2)]);
set(handles.axes2);

position3 = get(handles.axes3, 'Position');
set(handles.axes3, 'Position', [position3(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes3);
handles.axes3 = imshow(-imzr1, [min(-zr1), max(-zr1)]);
set(handles.axes3);

position4 = get(handles.axes4, 'Position');
set(handles.axes4, 'Position', [position4(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes4);
handles.axes4 = imshow(-imzr2, [min(-zr2), max(-zr2)]);
set(handles.axes4);

set(handles.edit1, 'String', '  senzor 1 nakon PCA i ICA - pozitiv');
set(handles.edit2, 'String', '  senzor 2 nakon PCA i ICA - pozitiv');
set(handles.edit3, 'String', '  senzor 1 nakon PCA i ICA - negativ');
set(handles.edit4, 'String', '  senzor 2 nakon PCA i ICA - negativ');

% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Z Zr
global z1kurt z2kurt sumAbsKurt
global thetar

set(handles.axes1, 'Units', 'pixels');
set(handles.axes2, 'Units', 'pixels');
set(handles.axes3, 'Units', 'pixels');
set(handles.axes4, 'Units', 'pixels');

theta = [-pi: 2*pi/360: pi-2*pi/360];

info = imfinfo('C:\MATLAB7\toolbox\images\imdemos\football.jpg');
position1 = get(handles.axes1, 'Position');
set(handles.axes1, 'Position', [position1(1:2) 2.1*0.7*info.Width 0.7*info.Height]);
axes(handles.axes1);
handles.axes1 = plot(theta, [z1kurt z2kurt sumAbsKurt]);

position3 = get(handles.axes3, 'Position');
set(handles.axes3, 'Position', [position3(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes3);
handles.axes3 = scatter(Z(1,:), Z(2,:));
set(handles.axes3);

position4 = get(handles.axes4, 'Position');
set(handles.axes4, 'Position', [position4(1:2) 0.7*info.Width 0.7*info.Height]);
axes(handles.axes4);
handles.axes4 = scatter(Zr(1,:), Zr(2,:));
set(handles.axes4);

set(handles.edit1, 'String', '  ovisnost kurtosis o theta');
set(handles.edit2, 'String', num2str(thetar));
set(handles.edit3, 'String', '  scatter PCA');
set(handles.edit4, 'String', '  scatter ICA');

% --- 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


% --- 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


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


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