close all
clear
clc
Image_Settings_MIT;

% Parameters for RetargetMe database
para.Lscaling = 0.35;
para.Sscaling = 0.65;

para.SIFTflowpara.wsize=4;
para.SIFTflowpara.Lwsize=4;

para.cellsize = 8;
para.gridspacing = 1;
para.bksize = 10;
para.overlap = 5;

% Prefix name for different retargeting algorothms
prefix = '../data/mit/';
type1 = '_cr.png';
type2 = '_sv.png';
type3 = '_multiop.png';
type4 = '_sc.png';
type5 = '_scl.png';
type6 = '_sm.png';
type7 = '_sns.png';
type8 = '_warp.png';
type0 = '.png';

% subjData.datasetNames{2,1}
load subjData-ref_37.mat
gr_score = subjData.data;
img_length = size(gr_score,1);

% Initialize the variables we need in the further computations
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);

type1_score = [];
type2_score = [];
type3_score = [];
type4_score = [];
type5_score = [];
type6_score = [];
type7_score = [];
type8_score = [];

type1_scoree = [];
type2_scoree = [];
type3_scoree = [];
type4_scoree = [];
type5_scoree = [];
type6_scoree = [];
type7_scoree = [];
type8_scoree = [];

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

% Do the quality assessment for each retargeted image with various
% retargeting algorithms
for i=1:img_length
    
    
    % Get the filename from dataset
    re1x = subjData.datasetNames{i,1};
    re2 = re1x;
    fn1=[];
    re1=[];
    cc=0;
    for p=1:100
        [fn1x, re1x] = strtok(re1x, '_');
        if (~isempty(re1x))
            if cc>0
                fn1 = [fn1 '_' fn1x];
            else
                fn1 = [fn1 fn1x];
            end
            cc=cc+1;
        else
            re1 = ['_' fn1x];
            break;
        end
    end
    
    original_path = [prefix fn1 '/' fn1 type0];
    type1_path = [prefix fn1 '/' fn1 re1 type1];
    type2_path = [prefix fn1 '/' fn1 re1 type2];
    type3_path = [prefix fn1 '/' fn1 re1 type3];
    type4_path = [prefix fn1 '/' fn1 re1 type4];
    type5_path = [prefix fn1 '/' fn1 re1 type5];
    type6_path = [prefix fn1 '/' fn1 re1 type6];
    type7_path = [prefix fn1 '/' fn1 re1 type7];
    type8_path = [prefix fn1 '/' fn1 re1 type8];
    
    % compute the score for algorithms
    n1 = ['MITg' fn1 re1 type1 '_spatial.mat'];
    n2 = ['MITg' fn1 re1 type2 '_spatial.mat'];
    n3 = ['MITg' fn1 re1 type3 '_spatial.mat'];
    n4 = ['MITg' fn1 re1 type4 '_spatial.mat'];
    n5 = ['MITg' fn1 re1 type5 '_spatial.mat'];
    n6 = ['MITg' fn1 re1 type6 '_spatial.mat'];
    n7 = ['MITg' fn1 re1 type7 '_spatial.mat'];
    n8 = ['MITg' fn1 re1 type8 '_spatial.mat'];
    
    
    sp0 = [prefix 'SaliencyMap/' fn1 type0];
    sp1 = [prefix 'SaliencyMap/' fn1 re1 type1];
    sp2 = [prefix 'SaliencyMap/' fn1 re1 type2];
    sp3 = [prefix 'SaliencyMap/' fn1 re1 type3];
    sp4 = [prefix 'SaliencyMap/' fn1 re1 type4];
    sp5 = [prefix 'SaliencyMap/' fn1 re1 type5];
    sp6 = [prefix 'SaliencyMap/' fn1 re1 type6];
    sp7 = [prefix 'SaliencyMap/' fn1 re1 type7];
    sp8 = [prefix 'SaliencyMap/' fn1 re1 type8];
    
    maptmp=[];
    % compute the score for algorithms
    % Spatial_score: The PGD value
    % energy_score_warp: The SLR value
    mt3=[];
    parfor z=1:8
        switch(z)
            case 1
                [tt type1_res_img po1] = spatial_score(original_path, type1_path, n1, [], para);
                type1_score=[type1_score tt];
                [tt] = energy_score_warp(original_path, type1_path, n1, [], para);
                type1_scoree = [type1_scoree tt];
                mt3 = [mt3 po1];
            case 2
                [tt type2_res_img po2] = spatial_score(original_path, type2_path, n2, [], para);
                 type2_score=[type2_score tt];
                [tt] = energy_score_warp(original_path, type2_path, n2, [], para);
                type2_scoree = [type2_scoree tt];
                mt3 = [mt3 po2];
            case 3
                [tt type3_res_img po3] = spatial_score(original_path, type3_path, n3, [], para);
                 type3_score=[type3_score tt];
                [tt] = energy_score_warp(original_path, type3_path, n3, [], para);
                type3_scoree = [type3_scoree tt];
                mt3 = [mt3 po3];
            case 4
                [tt type4_res_img po4] = spatial_score(original_path, type4_path, n4, [], para);
                 type4_score=[type4_score tt];
                [tt] = energy_score_warp(original_path, type4_path, n4, [], para);
                type4_scoree = [type4_scoree tt];
                mt3 = [mt3 po4];
            case 5
                [tt type5_res_img po5] = spatial_score(original_path, type5_path, n5, [], para);
                 type5_score=[type5_score tt];
                [tt] = energy_score_warp(original_path, type5_path, n5, [], para);
                type5_scoree = [type5_scoree tt];
                mt3 = [mt3 po5];
            case 6
                [tt type6_res_img po6] = spatial_score(original_path, type6_path, n6, [], para);
                 type6_score=[type6_score tt];
                [tt] = energy_score_warp(original_path, type6_path, n6, [], para);
                type6_scoree = [type6_scoree tt];
                mt3 = [mt3 po6];
            case 7
                [tt type7_res_img po7] = spatial_score(original_path, type7_path, n7, [], para);
                 type7_score=[type7_score tt];
                [tt] = energy_score_warp(original_path, type7_path, n7, [], para);
                type7_scoree = [type7_scoree tt];
                mt3 = [mt3 po7];
            case 8
                [tt type8_res_img po8] = spatial_score(original_path, type8_path, n8, [], para);
                 type8_score=[type8_score tt];
                [tt, map1] = energy_score_warp(original_path, type8_path, n8, [], para);
                type8_scoree = [type8_scoree tt];
                mt3 = [mt3 po8];
                maptmp = [maptmp map1];
        end
       
        
    end
    paraout1=[paraout1; mt3];
    
    % Obtain the index value
    mt1 = 1./[type1_score(i) type2_score(i) type3_score(i)  type4_score(i)  type5_score(i)  type6_score(i)  type7_score(i)  type8_score(i)];  % 1 / DOD value
    et1 = 1./[type1_scoree(i) type2_scoree(i) type3_scoree(i)  type4_scoree(i)  type5_scoree(i)  type6_scoree(i)  type7_scoree(i)  type8_scoree(i)];  % 1 / Energy loss values
    
    map1 = maptmp;
    all_salmap{i} = map1;
    mt2 = mt1./max((mt1));    
    
    % Adaptive weight
    w = calWeight(all_salmap{i}, 3, 350, 12);
    w1 = [w1; w];
    
    mt1 = normlz(mt1)/255;  
    et1 = normlz(et1)/255;
    
    all_dod = [all_dod; mt1];  % Local score 
    all_il =  [all_il;  et1];  % Energy score
    
    mtall = mt1*(1-w1(i)) + et1*(w1(i)); % 
    mtal2 = mt1*0.5 + et1*0.5; % Equal weight
    

    mt2  = gr_score(i,:);
    mt2e = gr_score(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(i) = getKLCorr(mtallx, mt2x);  % Calcuate K tau L distance (Rank-correlation) with adaptive weight
    corr_norm(i) = getKLCorr(mtal2x, mt2x);  %  Calcuate K tau L distance (Rank-correlation) with equal weight
    corr_en(i) = getKLCorr(et1x, mt2x);  % for energy loss
    corr_dod(i) = 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(i), corr_en(i), corr1(i), corr_norm(i));
    

end


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

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

% Show the rank correlation values with categorized images (Refer to RetargetMe).
showCorr
showCorrK3

