function CLab=combined_corr(Tfilename,Csize,I,M)
%This function calculates the combined correlations, which is the sum of
%the correlation of the L components, the a components and the b
%components.

%Load template in the specified size and convert it to Lab space.

T=image_resize(['../caras/',Tfilename],Csize);
T=applycform(T,M);
[tx,ty,tz]=size(T);
[imx,imy,imz]=size(I);

%Calculate the normalized cross correlation of each component, and crop the
%result such that it has the same size as the original image.

CL=normxcorr2(T(:,:,1),I(:,:,1));
CL=CL(round(tx/2):(imx+round(tx/2)-1),round(ty/2):(imy+round(ty/2)-1));
CL(1:round(tx/2),:)=-1.*ones(size(CL(1:round(tx/2),:)));
CL((imx-round(tx/2)):imx,:)=-1.*ones(size(CL((imx-round(tx/2)):imx,:)));
CL(:,1:round(ty/2))=-1.*ones(size(CL(:,1:round(ty/2))));
CL(:,(imy-round(ty/2)):imy)=-1.*ones(size(CL(:,(imy-round(ty/2)):imy)));
Ca=normxcorr2(T(:,:,2),I(:,:,2));
Ca=Ca(round(tx/2):(imx+round(tx/2)-1),round(ty/2):(imy+round(ty/2)-1));
Ca(1:round(tx/2),:)=-1.*ones(size(CL(1:round(tx/2),:)));
Ca((imx-round(tx/2)):imx,:)=-1.*ones(size(CL((imx-round(tx/2)):imx,:)));
Ca(:,1:round(ty/2))=-1.*ones(size(CL(:,1:round(ty/2))));
Ca(:,(imy-round(ty/2)):imy)=-1.*ones(size(CL(:,(imy-round(ty/2)):imy)));
Cb=normxcorr2(T(:,:,3),I(:,:,3));
Cb=Cb(round(tx/2):(imx+round(tx/2)-1),round(ty/2):(imy+round(ty/2)-1));
Cb(1:round(tx/2),:)=-1.*ones(size(CL(1:round(tx/2),:)));
Cb((imx-round(tx/2)):imx,:)=-1.*ones(size(CL((imx-round(tx/2)):imx,:)));
Cb(:,1:round(ty/2))=-1.*ones(size(CL(:,1:round(ty/2))));
Cb(:,(imy-round(ty/2)):imy)=-1.*ones(size(CL(:,(imy-round(ty/2)):imy)));

%Compute combined correlation.

CLab=CL+Ca+Cb;

