function [ p2, s,tx,ty] = normalizeForH( p1 )
%B. Vandeportaele
%normalization of the data for homography estimation. Scale s and
%translation (tx,ty) is computed to obtain a distribution of point with 0
%mean and sqrt(2) std dev.
%as explained in Geometry in Computer Vision (2ed,OUP,2003)(T)(672s)
% Data Normalization p107 & p180
%transform p1 points to p2 such that p2 points are centered around 0:0 and mean distance to 0:0 is sqrt(2)
%eventually see page 128 for the case where some points in p1 are very far away from the origin
%it would require a complete homography instead of just a scaling and
%translation, as it involve dealing with p1 in projective space P2
%in page 109, it is explained that non isotropic scaling is not necessary.
%average distance
m=mean(p1,2)';
%p1-[m(1)*ones(1,size(p1,2)); m(2)*ones(1,size(p1,2))]
%translation to bring the centroid at 0:0
dist=[];
for i=1:size(p1,2)
pc(1:2,i)=p1(:,i)-m'; %centered points
dist(i)=norm(pc(1:2,i),2);
end
meandist=mean(dist)
s=sqrt(2)/meandist;
for i=1:size(p1,2)
p2(1:2,i)=s*pc(:,i); %scaled and centered points
end
tx=-s*m(1);
ty=-s*m(2);
end