close all
clear all
clc

% Parameters for our own database
Image_Settings;

% Prefix name for different retargeting algorothms
original_sal = [prefix 'src_sal/'];
original_path = [prefix type6 '/'];
type1_path = [prefix type1 '/'];
type2_path = [prefix type2 '/'];
type3_path = [prefix type3 '/'];
type4_path = [prefix type4 '/'];
type5_path = [prefix type5 '/'];


img_list = dir([original_path ext_type]);
img_list2 = dir([type1_path ext_type]);
img_list3 = dir([type2_path ext_type]);
img_list4 = dir([type3_path ext_type]);
img_list5 = dir([type4_path ext_type]);
img_list6 = dir([type5_path ext_type]);

% Initialize the variables we need in the further computations
img_length = length(img_list);
type1_score  =[]; type2_score = []; type3_score=[]; type4_score=[]; type5_score=[];
type1_scoree  =[]; type2_scoree = []; type3_scoree=[]; type4_scoree=[]; type5_scoree=[];
res1=[]; res2=[]; res3=[]; res4=[]; res5=[];

ours_energy_score = zeros(img_length, 1);
scaling_energy_score = zeros(img_length, 1);
seam_energy_score = zeros(img_length, 1);
shift_energy_score = zeros(img_length, 1);
warp_energy_score = zeros(img_length, 1);


corr1 = [];
recore_score=[];
paraout1 = [];
all_dod = [];
all_il = [];
cnt1 = 0;
cv2 = 0;
w1=[];


ii=1;
global ca1;
% Do the quality assessment for each retargeted image with various
% retargeting algorithms
for i=1:35
    
    % Get the filename from dataset

    type6_img = [original_path  img_list(i).name];
    type1_img = [type1_path  img_list2(i).name];
    type2_img = [type2_path  img_list3(i).name];
    type3_img = [type3_path  img_list4(i).name];
    type4_img = [type4_path  img_list5(i).name];
    type5_img = [type5_path  img_list6(i).name];
    
    % Groundtruth salient
    type6_sal = [original_sal  img_list(i).name];
    sal1 = [];
    if (para.usedSaliencyType==3), sal1 = type6_sal; end
    
    % compute the score for algorithms
    n1 = [type1 img_list(i).name '_spatial.mat'];
    n2 = [type2 img_list2(i).name '_spatial.mat'];
    n3 = [type3 img_list3(i).name '_spatial.mat'];
    n4 = [type4 img_list4(i).name '_spatial.mat'];
    n5 = [type5 img_list5(i).name '_spatial.mat'];
    
    mt3=[];
    maptmp = [];
    % compute the score for algorithms
    % Spatial_score: The PGD value
    % energy_score_warp: The SLR value
    parfor z=1:10
        switch(z)
            case 1
                [t1 type1_res_img po1] = spatial_score(type6_img, type1_img, n1, sal1, para);
                type1_score = [type1_score; t1];
                mt3 = [mt3 po1]; 
            case 2
                [t1 type2_res_img po1] = spatial_score(type6_img, type2_img, n2, sal1, para);
                type2_score = [type2_score; t1];
                mt3 = [mt3 po1]; 
            case 3
                [t1 type3_res_img po1] = spatial_score(type6_img, type3_img, n3, sal1, para);
                type3_score = [type3_score; t1];
                mt3 = [mt3 po1]; 
            case 4
                [t1 type4_res_img po1] = spatial_score(type6_img, type4_img, n4, sal1, para);
                type4_score = [type4_score; t1];
                mt3 = [mt3 po1]; 
            case 5
                [t1 type5_res_img po1] = spatial_score(type6_img, type5_img, n5, sal1, para);
                type5_score = [type5_score; t1];
                mt3 = [mt3 po1]; 
            case 6
                [t1,~,~,re1] = energy_score_warp(type6_img, type1_img, n1, sal1, para);
                type1_scoree = [type1_scoree; t1];
                imwrite(uint8(re1), ['../data/res_img_temp/' img_list2(i).name]);
            case 7

                [t1,~,~,re1] = energy_score_warp(type6_img, type2_img, n2, sal1, para);
                type2_scoree = [type2_scoree; t1];
                imwrite(uint8(re1), ['../data/res_img_temp/' img_list3(i).name]);
            case 8

                [t1 ,~,~,re1] = energy_score_warp(type6_img, type3_img, n3, sal1, para);
                type3_scoree = [type3_scoree; t1];
                imwrite(uint8(re1), ['../data/res_img_temp/' img_list4(i).name]);
            case 9

                [t1,~,~,re1] = energy_score_warp(type6_img, type4_img, n4, sal1, para);
                type4_scoree = [type4_scoree; t1];
                imwrite(uint8(re1), ['../data/res_img_temp/' img_list5(i).name]);
            case 10
                [t1, map1,~,re1] = energy_score_warp(type6_img, type5_img, n5, sal1, para);
                maptmp = [maptmp map1];
                type5_scoree = [type5_scoree; t1];

                imwrite(uint8(re1), ['../data/res_img_temp/' img_list6(i).name]);
        end
    end

    % Obtain the index value
    mt1 = 1./[type1_score(ii)  type2_score(ii)  type3_score(ii)   type4_score(ii)   type5_score(ii)];  % 1 / DOD value
    et1 = 1./[type1_scoree(ii) type2_scoree(ii) type3_scoree(ii)  type4_scoree(ii)  type5_scoree(ii)];  % 1 / Energy loss values
    
    map1 = maptmp;
    all_salmap{ii} = map1;
    mt2 = mt1./max((mt1));

    
    % Adaptive weight
    w = calWeight(all_salmap{ii}, 5, 270, 10);
    w1 = [w1; w];
    
    all_dod = [all_dod; mt1];  % Local score 
    all_il = [all_il;   et1];  % Energy score
    
    mt1 = normlz(mt1)/255; 
    et1 = normlz(et1)/255;
    
    mtall = mt1*(1-w1(ii)) + et1*(w1(ii)); % Adaptive weigtht
    mtal2 = mt1*0.5 + et1*0.5; % Equal weight

    mt2  = gr_score(i,:);
    mt2e = gr_scoree(i,:);
    [mtallx, idx1] = sort(mtall, 'descend'); 
    [mtal2x, idx22] = sort(mtal2, 'descend'); 
    [mt1x, idx1x]  = sort(mt1,   'descend'); 
    [et1x, idx2x]  = sort(et1,   'descend'); 
    [mt2x, idx2]   = sort(mt2,   'descend');
    [mt2xe, idx2e] = sort(mt2e,  'descend');
    
    % find the rank
    for k=1:length(mt2)
        mtallx(idx1(k))=k;
        mtal2x(idx22(k))=k;
        mt2x(idx2(k))=k;
        mt2xe(idx2e(k))=k;
        mt1x(idx1x(k))=k;
        et1x(idx2x(k))=k;
    end
    corr1(ii) = getKLCorr(mtallx, mt2x);  %  % Calcuate K tau L distance (Rank-correlation) with adaptive weight
    corr_norm(ii) = getKLCorr(mtal2x, mt2x);  %  % Calcuate K tau L distance (Rank-correlation) with equal weight
    corr_en(ii) = getKLCorr(et1x, mt2x);  % for energy loss
    corr_dod(ii) = getKLCorr(mt1x, mt2x); % for dod

    paraout1=[paraout1; mt3];
    
    fprintf('The ROC of PGD, SLR, ALL1, ALL_norm at %dth is %f, %f, %f, %f\n', i, corr_dod(ii), corr_en(ii), corr1(ii), corr_norm(ii));
    ii = ii+1;
    
end


fprintf('The error value for %s, %s, %s, %s and %s are \n%f, %f, %f, %f, and %f\n', type1,type2,type3,type4,type5,mean(type1_score), mean(type2_score), mean(type3_score), mean(type4_score), mean(type5_score));
% 


mean(corr1)
std(corr1);
fprintf('The total corr=%f and std=%f, corr_norm=%f\n',mean(corr1), std(corr1), mean(corr_norm));
fprintf('The dod corr=%f and std=%f\n',mean(corr_dod), std(corr_dod));
fprintf('The il corr=%f and std=%f\n',mean(corr_en), std(corr_en));

eval(['save ' date '_OurDB_spatial.mat']);

analizeParaout


