function CS6640_Week8
%

% Images as surfaces
map1 = imread('map1.jpg');
map1g = rgb2gray(map1);
figure(1)
clf
surf(map1g(143:185,538:554));

figure(1)
clf
hold on

pts1 = [0,0; 2,0; 4,0; 0,1; 0,2; 2,2; 4,2; 0,4; 2,4; 4,4];
pts2 = pts1;

% Translation
pts2(:,1) = pts2(:,1) + 6;
pts = [pts1;pts2];
plot(pts1(:,1),pts1(:,2),'bo');
plot(pts2(:,1),pts2(:,2),'ko');

% cubic polynomial warping
a = [0,1,0,0,0,0,0,0,0,0]';
b = [0,0,1,0,0.4,0,0,0,0,0]';

ptsw = CS6640_poly_warp(pts,a,b);
plot(ptsw(:,1),ptsw(:,2),'ro');

ptsh = [pts,ones(20,1)]';

% Translation
T = eye(3);
T(1,3) = 10;
T(2,3) = 10;

pts_t = T*ptsh;
clf
plot(ptsh(1,:),ptsh(2,:),'ko');
hold on
plot(pts_t(1,:),pts_t(2,:),'ro');

% Rotation
clf
hold on
plot(ptsh(1,:),ptsh(2,:),'ko');
Tr = [cosd(135), -sind(135) 0; sind(135) cosd(135) 0; 0 0 1];
pts_r = Tr*ptsh;
plot(pts_r(1,:),pts_r(2,:),'ro');

% Center points at origin
xm = mean(ptsh(1,:));
ym = mean(ptsh(2,:));
pts0 = ptsh;
pts0(1,:) = pts0(1,:) - xm;
pts0(2,:) = pts0(2,:) - ym;

clf
plot(pts0(1,:),pts0(2,:),'ko');
hold on
pts_r = Tr*pts0;
plot(pts_r(1,:),pts_r(2,:),'r*');

% make an animation!
% for t = 0:359
%     Tr = [cosd(t), -sind(t) 0; sind(t) cosd(t) 0; 0 0 1];
%     pts_r = Tr*pts0;
%     clf
%     hold on
%     plot(-10,-10,'w.');
%     plot(10,10,'w.');
%     plot(pts_r(1,:),pts_r(2,:),'ko');
%     drawnow
%     M(t+1) = getframe;
% end
% movie(M,1,120);

% Convex hull
K = convhull(pts0(1,:),pts0(2,:));
clf
plot(pts0(1,:),pts0(2,:),'ko');
hold on
plot(pts0(1,K),pts0(2,K),'r*');

% Delaunay triangle
tri = delaunay(pts0(1,:)',pts0(2,:)');
triplot(tri,pts0(1,:)',pts0(2,:)');

clf
hold on
x = 20*rand(20,1);
y = 20*rand(20,1);
plot(x,y,'ro');
tri = delaunay(x,y);
triplot(tri,x,y);
clf
voronoi(x,y);

[dx,dy] = gradient(double(map1g));
mag = sqrt(dx.^2+dy.^2);
C  = mag(143:185,538:554)>55;
clf
imshow(imresize(C,[200,200]));
[rows,cols] = find(C);
x1 = cols;
y1 = 728 - rows + 1;
figure(2)
clf
voronoi(x1,y1);
[vx,vy] = voronoi(x1,y1);

% boundaries (2D)
A = zeros(101,101);
A(41:60,41:60) = 1;
figure(1)
clf
imshow(A);

[rows,cols] = find(A);
x = cols;
y = 101 - rows + 1;
K = boundary(x,y);
figure(2);
clf
plot(x,y,'ko');
hold on
plot(1,1,'w.');
plot(101,101,'w.');
plot(x(K),y(K),'r*');

% T shape
T = zeros(101,101);
T(20:40,21:80) = 1;
T(41:80,41:60) = 1;
figure(2)
clf
imshow(T);
B = bwboundaries(T);
[num_pts,dummy] = size(B{1});
mask = zeros(101,101);
for p = 1:num_pts
    mask(B{1}(p,1),B{1}(p,2)) = 1;
end
combo(T,mask);
combo(T,bwmorph(mask,'dilate',1));

corners = CS6640_Harris(T);
combo(T,bwmorph(corners,'dilate',2));

[X,lut] = imread('F34086A1.TIF');
clf
F34 = ind2rgb(X,lut);
F34g = rgb2gray(F34);
Let_E = F34g(209:250,421:451)<0.6;
Let_E(:,1) = 0;
corners = CS6640_Harris(Let_E);
combo(Let_E,corners);
tch = 0;
