index0 = 'marui2'
        Abest = load([index0 '-Abest.mat']);
        Abest = Abest.Abest;
meanface=imread([index0 '-meanface.bmp']);
% meanface = imread([image_path,index]);
meanface = im2double(meanface);
[s1 s2 s3]=size(meanface);
if s1 <200 ||s2<200
    meanface= imresize(meanface,max(ceil(200/s1),ceil(200/s2)));
end

testface = imread(['syntheticface.png']);
testface0 = im2double(testface);
testface = imread(strcat('..\pred\',[index0 '-meannorm.bmp-hi28154.bmp']));
% testface = imread([index0 '-maskface.bmp']);
testface = im2double(testface);
warpmean = meanface;
warpmean(:,:,:)=0;
mask = testface0(:,:,1)>0;
meanfacemask = meanface(:,:,1)*0;
% mask(:,:)=1;
% warpnormal = warpmean;
% tic
[m1 m2]=size(mask)
[s1 s2 s3] = size(meanface);
for ii=1:s1
    for jj=1:s2
        
            T = [jj,ii,1];
            M = T*Abest
            
%             M = [jj,ii,1];
%             T = (M)*inv(Abest)
%             T = T./T(3);
% if T(1)>0&&T(2)>0
if ceil(M(2))<m1 && ceil(M(1))<m2&&ceil(M(2))>0&&ceil(M(1))>0
    if mask(ceil(M(2)),ceil(M(1))) ==1
            warpmean(ii,jj,:) = testface(ceil(M(2)),ceil(M(1)),:);
            meanfacemask(ii,jj,:)=1;
%             warpnormal(ceil(T(2)),ceil(T(1)),:) = meannormal(ii,jj,:);
end
        end
    end
end
% toc
% figure,imshow(warpmean)
 mask3 = repmat(meanfacemask,[1 1 3]);
 
 close all

  imwrite(warpmean.*mask3, [index0 '-high-res.bmp']);

img = im2double(imread([index0 '-meanface.bmp']));

 imwrite((im2double(img)-warpmean).*mask3, [index0 '-matte-res.bmp']);

close all
figure,imshow(img)
figure,imshow(img-warpmean)