% est_transrotm.m  ... adapted for Peleg                 Steve Mann, 1992,1993
%
% multi-resolution (and recursive: calls est_transrotr) 2-D image group est.
%
% estimates parameters of the rigid body motion group
%
% Use: [a,b] = est_transrotm(E1,E2);                 % complex trans; rot mag=1
% or : [rot,trans_m,trans_n] = est_transrotm(E1,E2)  % 3 real output arguments
% or : est_transrotm(E1,E2);                         % nicely formatted screen
%                                                    % output in radians + deg
%
%      est_transrotm(E1,E2,3);                       % does 3 itterations at
%                                                    % each level; default is
%                                                    % 1 itteration at each
%                                                    % level
%
%      est_transrotm(E1,E2,3,4,a0,b0);               % 4 levels and starting a,b
%      compared to est_transrotr: NOTE NEW PARAMETER IN MIDDLE OF PARAMETER LIST
%      not at end, because I still want to be able to leave off a0,b0 if unknown
%      e.g. est_transrotm(E1,E2,2,4); does 2 itt/level, 4 levels, unknown a,b

function [p1, p2, p3] = multi_parameter_function(E1,E2,K,num_levels,a0,b0);

more off

if nargin==1
  disp('est_transrotm: you must supply 2 images... exiting...')
  return
end%if

[M  N ] = size(E1);
[M2 N2] = size(E2);
if (M~=M2) ! (N~=N2)
  disp('est_transrotm: two input images are not same size... exiting...')
  return
end%if

if nargin <3
  disp1='est_transrotm: number of itterations per level not specified; ';
  disp2='defaulting to 1';
  disp([disp1 disp2])
  K=1;
end%if

if nargin == 5
  disp('est_transrotm: still have not decided what to do with 4 input ')
  disp('               parameters.  please specify a0 and b0 or neither')
  return
end%if

if nargin < 4
  num_levels=floor(min(log2(M/16),log2(N/16))); % at least 16 pixel image size
  disp(sprintf('est_transrotm: defaulting to %g levels',num_levels))
end%if

boxsizes=[1 3 5 9 17 33 65 129 257 513 1025 2049 4097 8193]; % kernel sizes
							     % not all used
level=num_levels;

if nargin < 5
  disp('est_transrotm: estimating init parameters from the two smoothed images')
  [a0,b0] = est_transrotr(smooth(E1,boxsizes(level),'nan'),...
			  smooth(E2,boxsizes(level),'nan'),...
			  K         ...
			 );
  num_levels=num_levels-1;  % if you pass initial guess, it _is_ the first level
end%if
a_comp=a0;  % | initialize composite group parameters, either first guess or
b_comp=b0;  % | else it would have been specified by user input
o=pi/180;
%disp1='est_transrotm: ';
%disp2='formatted state-variables at each itteration displayed below:';
%disp([disp1 disp2])
%disp1=sprintf('beg 1:init angle(a)=%+7.4f= %+9.4fo; ',...
%              angle(a0),angle(a0)/o');
%disp2=sprintf('init b=%+9.3f + %+9.3f*i',real(b0),imag(b0));
%disp([disp1 disp2])
for level=num_levels:-1:1
  disp(sprintf('est_transrotm: now doing level %g',level));
  [a_comp,b_comp]=est_transrotr(smooth(E1,boxsizes(level),'nan'),...
				smooth(E2,boxsizes(level),'nan'),...
				K,...
				a_comp,b_comp             ...
			       ); % update composed a,b
end%for level

trans_m = real(b_comp);
trans_n = imag(b_comp);
rot     = angle(a_comp);

if nargout == 3
  p1 = rot;
  p2 = trans_m;
  p3 = trans_n;
end%if

if nargout == 2
  p1 = exp(i*rot);             % the ``a'' in ax+b; contains zoom=1 and rot
  p2 = trans_m + i*trans_n;    % the ``b'' in the ax+b group
end%if

if nargout == 1
  disp('only 1 output argument was given; still need to decide what to do here')
  disp('i have not decided what to do in the case of 1 output argument')
  disp('please use either 3,2, or 0 output arguments')
  return
end%if

if nargout == 0
  % disp('0 output arguments were given so I am displaying result to screen:')
  o = pi/180;
  disp(sprintf('ans:rot=%g =%go;  trans_m=%g pels;  trans_n=%g pels',...
               rot, rot/o, trans_m, trans_n))
  % not that we have left p1,p2,p3 undefined so that if est_transrotm called
  % with no semicolon, ``ans'' will not appear
end%if

more on

