%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    icaimage: Apply ICA on images                       %
%    show the procedure to compute the Ics in imageseries%
%    CIS601 Course Project                               %
%    Bo Han                                              %
%    Nov. 21, 2004                                       %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

im1=imread('.\img\t21.bmp');
im2=imread('.\img\t22.bmp');
im3=imread('.\img\t23.bmp');
im4=imread('.\img\t24.bmp');

im1=rgb2gray(im1);
im2=rgb2gray(im2);
im3=rgb2gray(im3);
im4=rgb2gray(im4);

%show source images
figure;
 subplot(2,2,1);
 imshow(im1);
 subplot(2,2,2);
 imshow(im2);
 subplot(2,2,3);
 imshow(im3);
 subplot(2,2,4);
 imshow(im4);
title('source images');

len=size(im1,1);
width=size(im1,2);
tem1=reshape(im1,1,len*width);
tem2=reshape(im2,1,len*width);
tem3=reshape(im3,1,len*width);
tem4=reshape(im4,1,len*width);

matrix=double([tem1;tem2;tem3;tem4]);

%apply ICA
figure;
A=ones(4,4)/4;
%[icasig, A, W] = image_ica (matrix, 'g','tanh','initGuess',A,'epsilon',0.0051,'maxFinetune',3);
[icasig, A, W] = image_ica (matrix, 'g','tanh','initGuess',A,'epsilon',0.0051,'maxFinetune',3);
title('ica');
sim1=reshape(icasig(1,:),len,width);
sim2=reshape(icasig(2,:),len,width);
sim3=reshape(icasig(3,:),len,width);
sim4=reshape(icasig(4,:),len,width);

 figure;
 subplot(2,2,1);
 imshow(sim1);
 subplot(2,2,2);
 imshow(sim2);
 subplot(2,2,3);
 imshow(sim3);
 subplot(2,2,4);
 imshow(sim4);
 title('independent components');
 
 figure;
 subplot(4,1,1);
 bar(A(1,:));
 subplot(4,1,2);
 bar(A(2,:));
 subplot(4,1,3);
 bar(A(3,:));
 subplot(4,1,4);
 bar(A(4,:));
 
 