% converts the 8 bilinear parameters to the 8 pchirp parameters using the
% 4 corners of a presumed image as the common points to define the relation
% between the bilinear parameters and the pchirp parameters

function p2=approximationatcorners(p1)

if nargin==0
  error('bilinear2p: you must have an input argument')
end%if

if length(p1) ~= 8
  error('bilinear2p: input vector, p_bilinear, must have length 8')
end%if

if min(size(p1)) ~= 1
  error('bilinear2p: input must be a vector and not a matrix')
end%if

x1=0; y1=0;
x2=0; y2=1;
x3=1; y3=0;
x4=1; y4=1;

x=[x1;x2;x3;x4];
y=[y1;y2;y3;y4];

u=p1(1)*(x.*y) + p1(2)*x + p1(3)*y +p1(4)*ones(4,1);
v=p1(5)*(x.*y) + p1(6)*x + p1(7)*y +p1(8)*ones(4,1);

p2=corners2r([u(1) v(1) u(2) v(2) u(3) v(3) u(4) v(4)]);

% now p2 would map the first image into the second but we wish to map
% the second to the first

%p2=reshape([p2(:);1],3,3).';   % .' because want across rows
%p2=reshape(inv(p2),9,1).';     % .' because want across rows
%p2=p2(1:8);

p2=pinverse(p2);  % same as 3 commented lines above, but neater

