% Second attempt at inpainting, now using average of
% pixel values around the inpainting domain
%
% Samuli Siltanen Nov 2014

% Parameters for plotting
lwidth = 3;

% Import the photo
im = imread('Photobomb.jpg','jpg');
im = double(im(:,:,2));
[row,col] = size(im);

% Crop the photo to square shape
im = im(:,end/5+[1:row]);
[row,col] = size(im);

% Normalize the image
im = im-min(im(:));
im = im/max(im(:));

% Create coordinates for the image
D = 1/col;
t = D/2 + [0:(col-1)]*D;
[X,Y] = meshgrid(t);

% Create a version of the image with black middle disc
index      = sqrt((X-0.5).^2+(Y-0.5).^2)<1/3; 
im2        = im;
im2(index) = 0;

% Construct angular variable for plotting the circle
Nfii = 256;
fii  = [0:(Nfii-1)]/Nfii*2*pi; 

% Choose a circle surrounding the inpainting area,
% here in Matlab's image plotting coordinates
% determined by command "imagesc"
cenx = 470;
ceny = 340;
R    = 260;
% here the same in unit square coordinates
% 0<=x<=1 and 0<=y<=1:
cenx01 = cenx/col;
ceny01 = ceny/row;
R01    = R/row;

% Calculate average pixel value at the boundary of the
% inpainting domain
% boundarypixels = zeros(size(fii));
% for iii = 1:Nfii
%     boundarypixels(iii) = interp2(X,Y,im,cenx01+R01*cos(fii(iii)),ceny01+R01*sin(fii(iii)));
% end
boundarypixels = interp2(X,Y,im,cenx01+R01*cos(fii),ceny01+R01*sin(fii));
ave_pixel = mean(boundarypixels);

% Perform a naive inpainting
im2          = im;
discind      = sqrt((X-cenx01).^2+(Y-ceny01).^2)<R01;
im2(discind) = ave_pixel;

% Take a look at the image and the two windows
figure(1)
clf
subplot(1,2,1)
imagesc(im)
hold on
plot(cenx+R*cos(fii),ceny+R*sin(fii),'r','linewidth',lwidth)
colormap gray
axis equal
axis off
subplot(1,2,2)
imagesc(im2)
%hold on
%plot(cenx+R*cos(fii),ceny+R*sin(fii),'r','linewidth',lwidth)
colormap gray
axis equal
axis off


