%COMP 256 Assignment 1
%Adrian Ilie

clear;

Nimages = 64;
startx = 190;
width = 300;
starty = 1;
height = 410;
%load images and crop them
[ambimage,imarray,lightdirs] = myLoadFaceImages(Nimages,startx,starty,width,height);

% %save the indices of the points that were white in a lot of images
% vx=[];
% vy=[];
% vv=[];
% for x = 1:height
%     for y = 1:width
%         vals=imarray(x,y,:);
%         num=size(find((vals>230)));
%         if num(1)>=Nimages*0.5
%             v=imarray(x,y,:);
%             vx=[vx x];
%             vy=[vy y];
%             vv=[vv v];
%         end
%     end
% end

%eliminate specularities (but also white points...)
imarray(find((imarray(:)>230)))=0;

% %restore the points that were white in all images
% for i = 1:size(vx)
%     imarray(vx(i),vy(i),:)=vv(:,i);
% end

%subtract the ambient image, clamp to 0..255, divide by 255, and blur with a
%Gaussian
gs=2;%Gaussian size
sigma=0.5;
f=fspecial('gaussian',[2*gs+1 2*gs+1],sigma);
for i = 1:(Nimages)
    img = imarray(:,:,i)-ambimage;
    ind=find(img<0);
    img(ind)=0;%clamp
%    img(ind)=-img(ind);%negate
%    img(ind)=rand(size(ind))*50;%put some white noise
    img=conv2(img,f,'full');
    imarray(:,:,i)=img(1+gs:height+gs,1+gs:width+gs)/255;
%    imarray(:,:,i)=img/255;
%    imshow(img);
end

process;

pause;

finalize;

