%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SSS_V8.2A Saliency Detection Using Both Global and Local Information
% ICA_FILTERS is from [1].
%[1] X Hou and Liqing Zhang. Dynamic visual attention: searching for coding 
%length increments. In Advances in Neural Information Processing Systems 21.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc
clear all; 
%% READ The Image %%%%%%%
x=7% Input Image index, for example x=12
for q=1:28 %batch processing style using loop, for example q=1:10
image=double(imread(['',num2str(q),'.jpg']));
image=image/max(max(max(image)));
r=double(image(:,:,1));g=double(image(:,:,2));b=double(image(:,:,3));
[q1,q2]=size(r);
Ori_imag=zeros(128,128,3);
Ori_imag(:,:,1) = imresize(r, [128,128], 'bilinear');
Ori_imag(:,:,2) = imresize(g, [128,128], 'bilinear');
Ori_imag(:,:,3) = imresize(b, [128,128], 'bilinear');
W=128;
subplot(121);imshow(Ori_imag,[])
title(['Original Image ',num2str(q)],'fontsize',16,'Color','r')
%% %%%%%%% Three CHANNELS %%%%%%%%%%%%%
I=max(max(r,g),b);I=mat2gray(I);
R=r-(g+b)/2;G=g-(r+b)/2;B=b-(g+r)/2;Y=(r+g)/2-abs(r-g)/2-b;Y=Y.*(Y>0);
rr=(R-G);gg=(B-Y);bb=I;
rr = imresize(rr, [128,128]);gg = imresize(gg, [128,128]);bb = imresize(bb, [128,128]);
Kl=min(max(rr(:)),1);Ka=min(max(gg(:)),1);Kb=min(max(bb(:)),1);
L=(rr);A=(gg);B=(bb);
L=L-min(min(L));A=A-min(min(A));B=B-min(min(B));
L=L/(max(max(L))+.000001);A=A/(max(max(A))+.000001);B=B/(max(max(B))+.000001);

D1=zeros(128,128,3);D2=zeros(128,128,3);D3=zeros(128,128,3);

for c=1:3
    D1(:,:,c)=L;
    D2(:,:,c)=A;
    D3(:,:,c)=B;
end

[m,n]=size(L);

%% Compute Saliency map series for each channel%%
SL=MSSM(L);SA=MSSM(A);SB=MSSM(B);
%% %%%%%%% Find the best saliency map for each channel%%%%%%%%%%
for k=1:7
    entro_l(k)=entropy2((SL(:,:,k)));
    entro_a(k)=entropy2((SA(:,:,k)));
    entro_b(k)=entropy2((SB(:,:,k)));
end
entro_l_seq=sort(entro_l);
entro_a_seq=sort(entro_a);
entro_b_seq=sort(entro_b);

c_l=find(entro_l==entro_l_seq(1));
c_a=find(entro_a==entro_a_seq(1));
c_b=find(entro_b==entro_b_seq(1));
%%  Saliency Map %%%%%%%%%%%%%%%%%%%%
if entro_l(c_l(1))==0;
entro_l(c_l(1))=100;
end

if entro_a(c_a(1))==0;
entro_a(c_a(1))=100;
end
wL=1/(entro_l(c_l(1)));wA=1/(entro_a(c_a(1)));wB=2/(entro_b(c_b(1)));
ActiveMapL=SL(:,:,c_l(1));ActiveMapA=SA(:,:,c_a(1));ActiveMapB=SB(:,:,c_b(1));
Tol=wL+wA+wB;wL=(wL/Tol)^1;wA=(wA/Tol)^1;wB=(wB/Tol)^1;


%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Saliency Map Computed using global information
S1=Kl*wL*ActiveMapL+Ka*wA*ActiveMapA+Kb*wB*ActiveMapB;
%% Saliency Map Computed using Local information
S2=Spatial_saliency(Ori_imag);
%% Combining
S1=mat2gray(S1);S2=mat2gray(S2);
w1=1/entropy2(S1);w2=1/entropy2(S2);
S1=.9*w1*S1+.1*w2*S2;

%% Smoothing for a better visual effect
S1 = imfilter(S1, fspecial('gaussian',[64,64], 128*.04));
%S1 = imfilter(S1, fspecial('gaussian',[64,64], 128*.03));
S1=mat2gray(S1);

 subplot(122)
    imshow(S1)
 title('Final Saliency Image','fontsize',16,'Color','r')  
%% 
S1 = imresize(S1, [q1,q2], 'bilinear');
%imwrite(S1,['SaMap_Psych_',num2str(q),'.jpg']);
%colormap(jet)
pause(.1)
end