amat() returns matrix of collination from correspondence vectors

Petr Sladek (sladep1) & Milan Kratochvil (kratom4) at CTU Prague

Contents

inputs:

function(U,V), V=T(U) T is transformation. U vector is input (primary correspondence points)

% format:
%   col_no 1      2     3 ... n
%          u_x1   u_x2  u_x3
%          u_y1   u_y2  u_y3
% U vector is input (secondary correspondence points)

%note: 'publish()' can't publish functions:
%function [A] = amat(U,V);

input size checking

sU = size(U);
if ((length(sU)==2) & (sU(1)==2))
  sV = size(V);
  if ((length(sV)==2) & (sV(1)==2) & (sV(2)==sU(2)))
Error: This statement is incomplete.

amat body

allocate mem, setup C matrix, SVD, pick result

    % dims U,V seems to be okay.
    % allocate sufficient mem for C matrix:
    l=sU(2);
    C=zeros(2*l,9);
    %matrix C generator: (see
    %http://cmp.felk.cvut.cz/cmp/courses/ZSO/cvic3/geomtransf.pdf)
    % U   000  -xU
    % U   000  -xU
    % ............
    % 000  U   -xU
    % 000  U   -xU
    %top left Us:
    C(1:l,1:2)=U(1:2,1:l)'; C(1:l,3)=1;
    %center down Us:
    C(l+1:2*l,4:5)=U(1:2,1:l)'; C(l+1:2*l,6)=1;
    %top right xUs:
    C(1:l,7)=(U(1,1:l)').*(-V(1,1:l)');
    C(1:l,8)=(U(2,1:l)').*(-V(1,1:l)');
    C(1:l,9)=-V(1,1:l)';
    %right down xUs:
    C(l+1:2*l,7)=U(1,1:l)'.*(-V(2,1:l)');
    C(l+1:2*l,8)=U(2,1:l)'.*(-V(2,1:l)');
    C(l+1:2*l,9)=-V(2,1:l)';

SVD minimization

SVD is very sensitive if pair vectors are in one line !

    % resolve h -  Singular value decomposition.
    [u,s,v]=svd(C);
    vs = size(v);
    % pick last column.
    h = v(:,vs(2));
    A = zeros(3,3);
    A(1,1:3)=h(1:3);
    A(2,1:3)=h(4:6);
    A(3,1:3)=h(7:9);
  else
    fprintf('V must be 2D vector 2 rows x n col, and lenght of V must be equal to U \n');
  end;
else
  fprintf('U must be 2D vector 2 rows x n col \n');
end;