%Lecture_052112.m

%waldo = imread('Waldo.bmp');

waldo = imread('../images/waldo_people.jpg');

figure(1)
clf
image(waldo);

w = [1,-.5,-.5];

foo =.5;


%waldo2D = double(waldo(:,:,1)-.5*waldo(:,:,2)-.5*waldo(:,:,3));

%waldo2D  = w(1)*waldo(:,:,1)+w(2)*waldo(:,:,2) +w(3)*waldo(:,:,3);

waldo2D  = double(1*waldo(:,:,1)-.5*waldo(:,:,2) -.5*waldo(:,:,3));

waldo2D = waldo2D - mean(waldo2D(:));

%beware of uint8 math problems!

figure(2)
clf
imagesc(waldo2D);
colormap(gray)
axis equal
axis tight


%% User input

[x,y] = ginput(1);
x = round(x);y=round(y);

windowSize = 32;  %size of sub image

subImage = waldo2D(y-windowSize/2:y+windowSize/2,...
               x-windowSize/2:x+windowSize/2);


figure(3)
clf
imagesc(subImage)
colormap(gray)
axis equal
axis tight

%% FFT of subImage

[xx,yy] = meshgrid(-windowSize/2:windowSize/2);

plotFFT2(subImage,xx,yy,5,.2)

subplot(1,2,2)
[sx,sy] = ginput(1);

sf = sqrt(sx^2+sy^2);

%% make our Gabor filters

sig = 15;
Gauss = exp(-((xx.^2+yy.^2)/(2*sig^2)));
Sine= sin(2*pi*yy*sf);
Cosine= cos(2*pi*yy*sf);

GaborSine =Gauss.*Sine;
GaborCosine =Gauss.*Cosine;



figure(4)
imagesc(GaborSine);
colormap(gray)
axis equal
axis tight

%% Convole our filter with our image

filtImgSine = conv2(waldo2D,GaborSine,'same');
filtImgCosine= conv2(waldo2D,GaborCosine,'same');

filtImg = sqrt(filtImgSine.^2+filtImgCosine.^2);

figure(5)
imagesc(filtImg);
colormap(gray)
axis equal
axis tight

%% multiply original image with filtered map
filtImg = filtImg/max(filtImg(:));  %scale it so it maxes at 1
filtWaldo = uint8(double(waldo).* repmat(filtImg,[1,1,3]));
figure(6)
image(filtWaldo);
colormap(gray)
axis equal
axis tight

%% look for his face

shiftFilt = zeros(size(xx));

shiftFilt(1,windowSize/2) = 1;

shiftFilt = exp(-((xx.^2+(yy+windowSize/4).^2)/(2*5^2)));
figure(7)
imagesc(shiftFilt)
colormap(gray)
axis equal
axis tight

return


%%
filtImgShift = filtImg;

for i=1:5  
    filtImgShift = conv2(filtImgShift,shiftFilt,'same');
end


figure(8)
imagesc(filtImgShift);
colormap(gray)
axis equal
axis tight

%% multiply original image with filtered shifted burred map
filtImgShift = filtImgShift/max(filtImgShift(:));  %scale it so it maxes at 1
filtWaldo = uint8(double(waldo).* repmat(filtImgShift,[1,1,3]));
figure(9)
image(filtWaldo);
colormap(gray)
axis equal
axis tight

