%%
close all;
clc;
clear all;

win = ones(5);

folder = 'G:\sbsBarrier4201_3Qp10';
files = dir(fullfile(folder, 'sbsBarrier4201_3Qp10*'));
files = cellfun(@(filename)fullfile(folder, filename), {files.name}, 'uni', 0);


folderref = 'G:\sbsBarrier';
filesref = dir(fullfile(folderref, 'sbsBarrier*'));
filesref = cellfun(@(filename)fullfile(folderref, filename), {filesref.name}, 'uni', 0);


imagesref = cell(size(filesref));
images = cell(size(files));

l=numel(filesref);
%MSE=zeros(1,l,'single');
%PSNR=zeros(1,l,'single');
SSIMr=zeros(1,l);
SSIMl=zeros(1,l);
Q=zeros(1,l);

for k = 1:2:numel(filesref)
    i=((k+1)/2);
    imagesref{k} = imread(filesref{k});
    images{k} = imread(files{k});
    
    imref=imagesref{k};
   im=images{k};
   IRref=imref(:,1:960,:);
   ILref=imref(:,961:1920,:);
   IR=im(:,1:960,:);
   IL=im(:,961:1920,:);
       

imagesref_grayR=rgb2gray(IRref);
imagesref_grayL=rgb2gray(ILref);

images_grayR=rgb2gray(IR);
images_grayL=rgb2gray(IL);





%
%{
%picg=picg[964,1295];
nul=zeros(1080,1920);
figure()
subplot 221
imshow(imagesref_gray)
subplot 222
imshow(images_gray)
subplot 223
imhist(imagesref_gray)
subplot 224
imhist(images_gray)
%}

%roz=imagesref_gray-images_gray;
%[m,n]=size(imagesref_gray);
%mse=(1/(m*n))*sum(sum(roz.^2));
%MSE(k)=mse;
%PSNR(k)=single(psnr(images{k},imagesref{k}));

%psnr=20*log10(255/sqrt(mse));
%PSNR(k)=psnr;
mssimR = single(ssim(imagesref_grayR, images_grayR));
mssimL = single(ssim(imagesref_grayL, images_grayL));
SSIMr(i)=mssimR;
SSIMl(i)=mssimL;





%I=rgb2gray(I);
%I=I(:,:,2);
ERref = stdfilt(rgb2gray(IRref), win); 
ELref = stdfilt(rgb2gray(ILref), win); 
EL = stdfilt(rgb2gray(IL), win); 
ER = stdfilt(rgb2gray(IR), win); 
%imshow(IR);
%figure, imshow(ER,[]); 

Ril = uint16(EL./ELref);
Rir = uint16(ER./ERref);
%figure, imshow(Ril,[]);

EL=uint16(EL);
ER=uint16(ER);

gil=(sum(sum(EL.*Ril)))/(sum(sum(EL)));
gir=sum(sum(ER.*Rir))/sum(sum(ER));

wil=(gil^2)/(gil^2+gir^2);
wir=(gir^2)/(gil^2+gir^2);

q=wil*mssimL+wir*mssimR;
Q(i)=q;


%figure()
%imshow(ssim_map);
end
%a=sum(PSNR);
%c=a/133
a=[SSIMr;SSIMl;Q];

%xlswrite('sbsBarrier.xlsx',a,'barrier','B31')
%figure()
%subplot 311
%plot(MSE)
%subplot 312
%plot(PSNR)
%subplot 313
%plot(SSIM)


