
% max van kleek
% problem set zero

% problem one

original = im2double(imread('data/original.tif'));
whitefield = im2double(imread('data/whiteField.tif'));

corrected = original./whitefield;
oldcmap = colormap;
colormap(repmat([0:0.01:1]',1,3));

imagesc(corrected.^.05);


% problem two

% simulate rgb filter

bt = imread('data/brettan.tif');
bt = im2double(bt);

mask = repmat(permute(eye(3),[3 2 1]),size(bt,1),floor(size(bt,2)/3));

% cut off rounding error
bt=bt([1:size(bt,1)],[1:(3*floor(size(bt,2)/3))],:);

btrgb = sum(bt .* mask,3);

cmap = colormap;  % stow away the old cmap
colormap(repmat([0:0.001:1]',1,3));
imagesc(btrgb);

% b) linear interpolation
% 

btvr = zeros(size(btrgb));
btvg = zeros(size(btrgb));
btvb = zeros(size(btrgb));

ridx = find(mod(find(btrgb),3)==1);
ridx = ridx([1:3*floor(length(ridx)/3)]);
gidx = ridx + 1;
bidx = ridx + 2;

btvr(ridx)=btrgb(ridx);
btvr(ridx+1)=2.0/3.0*btrgb(ridx) + 1.0/3.0*btrgb(ridx+3);
btvr(ridx+2)=1.0/3.0*btrgb(ridx) + 1.0/3.0*btrgb(ridx+3);
btvg(gidx)=btrgb(gidx);
btvg(gidx+1)=2.0/3.0*btrgb(gidx) + 1.0/3.0*btrgb(gidx+3);
btvg(gidx+2)=1.0/3.0*btrgb(gidx) + 2.0/3.0*btrgb(gidx+3);
btvb(bidx)=btrgb(bidx);
btvb(bidx+1)=2.0/3.0*btrgb(bidx)+1.0/3.0*btrgb(bidx+3);
btvb(bidx+2)=1.0/3.0*btrgb(bidx)+2.0/3.0*btrgb(bidx+3);
bt = cat(3,btvr,btvg,btvb);
figure(1);
imagesc(bt);
title('brettan.tiff');

%%
clear;
bt = imread('data/brettanBlurred.tif');
bt = im2double(bt);

mask = repmat(permute(eye(3),[3 2 1]),size(bt,1),floor(size(bt,2)/3));

% cut off rounding error
bt=bt([1:size(bt,1)],[1:(3*floor(size(bt,2)/3))],:);

btrgb = sum(bt .* mask,3);

cmap = colormap;  % stow away the old cmap
colormap(repmat([0:0.001:1]',1,3));
imagesc(btrgb);

btvr = zeros(size(btrgb));
btvg = zeros(size(btrgb));
btvb = zeros(size(btrgb));

ridx = find(mod(find(btrgb),3)==1);
ridx = ridx([1:3*floor(length(ridx)/3)]);
gidx = ridx + 1;
bidx = ridx + 2;

btvr(ridx)=btrgb(ridx);
btvr(ridx+1)=2.0/3.0*btrgb(ridx) + 1.0/3.0*btrgb(ridx+3);
btvr(ridx+2)=1.0/3.0*btrgb(ridx) + 1.0/3.0*btrgb(ridx+3);
btvg(gidx)=btrgb(gidx);
btvg(gidx+1)=2.0/3.0*btrgb(gidx) + 1.0/3.0*btrgb(gidx+3);
btvg(gidx+2)=1.0/3.0*btrgb(gidx) + 2.0/3.0*btrgb(gidx+3);
btvb(bidx)=btrgb(bidx);
btvb(bidx+1)=2.0/3.0*btrgb(bidx)+1.0/3.0*btrgb(bidx+3);
btvb(bidx+2)=1.0/3.0*btrgb(bidx)+2.0/3.0*btrgb(bidx+3);
bt = cat(3,btvr,btvg,btvb);
figure(2);
imagesc(bt);
title('brettanBlurred.tiff');

% problem three : object morphing
%% flowNS:
%%   x plane vectors dx scalar 
%%   y plane vectors dy scalar
%% so new image img = oldimage(coordinates+dx,coordinates+dy)

sm = imread('data/smile.jpg');
sm = im2double(sm);
load('data/flowNS.mat');
xcoo = meshgrid([1:size(sm,2)],[1:size(sm,1)])  + 0.5*flowNS(:,:,1);
ycoo = meshgrid([1:size(sm,1)],[1:size(sm,2)])' + 0.5*flowNS(:,:,2);
xs=meshgrid([1:720],[1:480]);
ys=meshgrid([1:480],[1:720])';
for i=1:3, mario(:,:,i) = interp2(xs,ys,sm(:,:,i),xcoo,ycoo); end;
figure(6);
imagesc(mario);

% smooth transition:
sm = imread('data/smile.jpg');
sm = im2double(sm);
nt = imread('data/neutral.jpg');
nt = im2double(nt);
load('data/flowNS.mat');
load('data/flowSN.mat');
for i=1:10, 
	subplot(5,2,i); 
        imagesc(morph(nt,sm,flowNS,flowSN,1.0/10.0*i)); 
end;
edit m
%% backgrounds
% first grab some of the background pixeels
sm = imread('data/smile.jpg');
sm = im2double(sm);
bkpix = sm([1:350],[1:241],:);
bkpixp = picpermute(bkpix);
bkmean = mean(bkpixp);
bkcov=cov(bkpixp);

smp = picpermute(sm);
bkidx = find(mahanoboulis(smp,bkmean,bkcov)<200);
smp(bkidx,:,:) = repmat([0 0 0],size(bkidx,1),1);
smnew = unpicpermute(smp,sm);
imagesc(smnew);












