%COMP 256 Assignment 2
%Adrian Ilie

function [p1n,p2n,tr1,tr2]=normalize(p1,p2,num)
%normalizes the correspondences

%find the centroids
c1=mean(p1')';
c2=mean(p2')';

%compute total distance
td1=0;
td2=0;

for i=1:num
    td1=td1+norm(p1(1:2,i)-c1(1:2));
    td2=td2+norm(p2(1:2,i)-c2(1:2));
end

%average distance
ad1=td1/num;
ad2=td2/num;

%scale
s1=sqrt(2)/ad1;
s2=sqrt(2)/ad2;

%setup transforms
tr1=eye(3);
tr1(1,1)=s1;
tr1(2,2)=s1;
tr1(1:2,3)=-c1(1:2)*s1;
tr2=eye(3);
tr2(1,1)=s2;
tr2(2,2)=s2;
tr2(1:2,3)=-c2(1:2)*s2;

%normalize
p1n=tr1*p1;
p2n=tr2*p2;
