% est_affine2                                          Steve Mann, 1991-1993
%
% 2D estimate the 6 affine parameters from a pair of images
%
% The model is:   u = p1 + p2x + p3y
%                 v = p4 + p5x + p6y
%
% Use: p = est_affine(E1,E2);
%  or: [p1,p2,p3,p4,p5,p6] = est_affine(E1,E2);
%  or : est_affine(E1,E2);      % nicely formatted screen output
%
% sign convention: m (or X) down, n (or Y) to right
% properly handles situation when one or both images contain some NaN points

function [p1,p2,p3,p4,p5,p6]=f(E1,E2); % 1 or 6 return args

%%%%mask = isnan(E1).*isnan(E2);  % NaN mask: regions where either is undefined
% doesnt work because nanmask makes it still nan

[El,Em,En] = derivatives(E1,E2);
[M,N]= size(El);                    % size of any of the 3 derivative matrices
[n m]=meshdom(1:N,M:-1:1);          % variables ``x'' and ``y''

DERIVATIVES = ... % must indent; if you do not indent then the )...sum2nan will not work, you need ) ...sum2nan (e.g. the space)
 [...
  [sum2nan(Em       .*Em) sum2nan(m.*Em       .*Em) sum2nan(n.*Em       .*Em) sum2nan(En       .*Em) sum2nan(m.*En       .*Em) sum2nan(n.*En       .*Em)];...
  [sum2nan(Em    .*m.*Em) sum2nan(m.*Em    .*m.*Em) sum2nan(n.*Em    .*m.*Em) sum2nan(En    .*m.*Em) sum2nan(m.*En    .*m.*Em) sum2nan(n.*En    .*m.*Em)];...
  [sum2nan(Em    .*n.*Em) sum2nan(m.*Em    .*n.*Em) sum2nan(n.*Em    .*n.*Em) sum2nan(En    .*n.*Em) sum2nan(m.*En    .*n.*Em) sum2nan(n.*En    .*n.*Em)];...
  [sum2nan(Em       .*En) sum2nan(m.*Em       .*En) sum2nan(n.*Em       .*En) sum2nan(En       .*En) sum2nan(m.*En       .*En) sum2nan(n.*En       .*En)];...
  [sum2nan(Em    .*m.*En) sum2nan(m.*Em    .*m.*En) sum2nan(n.*Em    .*m.*En) sum2nan(En    .*m.*En) sum2nan(m.*En    .*m.*En) sum2nan(n.*En    .*m.*En)];...
  [sum2nan(Em    .*n.*En) sum2nan(m.*Em    .*n.*En) sum2nan(n.*Em    .*n.*En) sum2nan(En    .*n.*En) sum2nan(m.*En    .*n.*En) sum2nan(n.*En    .*n.*En)];...
 ];

Derivatives = ...
 - [sum2nan(El       .*Em); ...
    sum2nan(El    .*m.*Em); ...
    sum2nan(El    .*n.*Em); ...
    sum2nan(El       .*En); ...
    sum2nan(El    .*m.*En); ...
    sum2nan(El    .*n.*En) ...
   ];                         % vector of derivatives

parameters = DERIVATIVES\Derivatives;

disp('est_affine: adding identity')
parameters(:) = parameters(:) + [0;1;0; 0;0;1];  % convert relative change like u=func(x+deltax) to absolute like u=func(x)

if (nargout == 6) | (nargout == 0)
  % p1 = parameters(1);           % if nargin==0 must never make ref to p1
  p2 = parameters(2);             % or you end up with ans=[], when called with
  p3 = parameters(3);             % no semicolon
  p4 = parameters(4);
  p5 = parameters(5);
  p6 = parameters(6);
end%if
if nargout == 6
  p1 = parameters(1);
end%if

if nargout == 1
  p1 = parameters;
end%if

if nargout == 0   % no output argument
  disp('est_affine: 0 output arguments given so I am displaying to screen:')
  %disp(sprintf('%g %g %g %g %g %g',p1,p2,p3,p4,p5,p6))
  disp(sprintf('%g %g %g %g %g %g',parameters(1),p2,p3,p4,p5,p6))
end%if

if (nargout ~= 0) & (nargout ~= 1) & (nargout ~= 6)
  error('est_dozen: you must have 0, 1, or 6 output arguments')
end%if

