%COMP 256 Assignment 2
%Adrian Ilie

%function epipolar(pointfile)
pointfile='points.txt';

%load point data
[p1,p2,num]=loadpointdata(pointfile);

%normalize
[p1n p2n tr1 tr2]=normalize(p1,p2,num);

%setup the linear system
%(u1,v1,1)'*F*(u2,v2,1)
u1=p1n(1,:)';
u2=p2n(1,:)';
v1=p1n(2,:)';
v2=p2n(2,:)';

%solve the linear system
%m=[u1.*u2 u1.*v2 u1 v1.*u2 v1.*v2 v1 u2 v2];
%fv=pinv(m)*(-ones(num,1));

%other solution
m=[u1.*u2 u1.*v2 u1 v1.*u2 v1.*v2 v1 u2 v2 ones(num,1)];
[u,d,v]=svd(m);
fv=v(:,9);
f=reshape(fv,3,3)';

%enforce the rank 2 constraint
fc=closest(f);
ff=tr1'*fc*tr2;

[r1,r2]=rectify(ff,'points.txt');
im1=warpimage('corner28.jpg',r1);
imwrite(im1,'corner28w.bmp','bmp');
im2=warpimage('corner29.jpg',r2);
imwrite(im2,'corner29w.bmp','bmp');
