%BBM415 Image Processing Practicum
%Dr. Erkut Erdem
%TAs. Levent Karacan

imgA = im2double(imread('mona-lisa.jpg'));
imgB = im2double(imread('mona-lisa.jpg')); 
imgA = imresize(imgA,[size(imgB,1) size(imgB,2)]);


[h,w,~] = size(imgA);
[yim,xim]=meshgrid(1:w,1:h); % pixel coordinates



figure, imshow(imgA);
h = imfreehand;    % select a region from A image
maskA = createMask(h);%Region Mask
maskA3=repmat(maskA,[1,1,3]);
ImA=imdilate(maskA3,ones(30)).*imgA; % Region Image
centY=sum(sum(maskA.*xim))./sum(maskA(:)); % Center coordinates of region
centX=sum(sum(maskA.*yim))./sum(maskA(:));
hold on
plot(centX,centY,'r+');
figure,imshow(imgB) 
[x,y,button]=ginput(1); % Where will you paste selected region in Image B?
hold on
plot(x,y,'r+');
shiftx=round(x)-round(centX);
shifty=round(y)-round(centY);
shiftedIm=circshift(ImA,[shifty,shiftx]);
shiftedMask=circshift(maskA3,[shifty,shiftx]);
maskb = 1-shiftMask;
BlendedImage = shiftIm.*shiftMask + imgB.*(1-shiftMask);
figure,imshow(BlendedImage)

