Home > matGeom > geom3d > fitAffineTransform3d.m

fitAffineTransform3d

PURPOSE ^

FITAFFINETRANSFORM3D Compute the affine transform that best register two 3D point sets.

SYNOPSIS ^

function trans = fitAffineTransform3d(ref, src)

DESCRIPTION ^

FITAFFINETRANSFORM3D Compute the affine transform that best register two 3D point sets.

   TRANS = fitAffineTransform3d(REF, SRC)
   Returns the affine transform matrix that minimizes the distance between
   the reference point set REF and the point set SRC after transformation.
   Both REF and SRC must by N-by-3 arrays with the same number of rows,
   and the points must be in correspondence.
   The function minimizes the sum of the squared distances:
   CRIT = sum(distancePoints3d(REF, transformPoint3d(PTS, TRANSFO)).^2);

   Example
     N = 50;
     pts = rand(N, 3)*10;
     trans = createRotationOx([5 4 3], pi/4);
     pts2 = transformPoint3d(pts, trans);
     pts3 = pts2 + randn(N, 3)*2;
     fitted = fitAffineTransform3d(pts, pts2);
   

   See also
     transforms3d, transformPoint3d, transformVector3d,
     fitAffineTransform2d, registerPoints3d_affine

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function trans = fitAffineTransform3d(ref, src)
0002 %FITAFFINETRANSFORM3D Compute the affine transform that best register two 3D point sets.
0003 %
0004 %   TRANS = fitAffineTransform3d(REF, SRC)
0005 %   Returns the affine transform matrix that minimizes the distance between
0006 %   the reference point set REF and the point set SRC after transformation.
0007 %   Both REF and SRC must by N-by-3 arrays with the same number of rows,
0008 %   and the points must be in correspondence.
0009 %   The function minimizes the sum of the squared distances:
0010 %   CRIT = sum(distancePoints3d(REF, transformPoint3d(PTS, TRANSFO)).^2);
0011 %
0012 %   Example
0013 %     N = 50;
0014 %     pts = rand(N, 3)*10;
0015 %     trans = createRotationOx([5 4 3], pi/4);
0016 %     pts2 = transformPoint3d(pts, trans);
0017 %     pts3 = pts2 + randn(N, 3)*2;
0018 %     fitted = fitAffineTransform3d(pts, pts2);
0019 %
0020 %
0021 %   See also
0022 %     transforms3d, transformPoint3d, transformVector3d,
0023 %     fitAffineTransform2d, registerPoints3d_affine
0024 %
0025 
0026 % ------
0027 % Author: David Legland
0028 % E-mail: david.legland@inrae.fr
0029 % Created: 2009-07-31, using Matlab 7.7.0.471 (R2008b)
0030 % Copyright 2009-2024 INRAE - Cepia Software Platform
0031 
0032 % number of points
0033 N = size(src, 1);
0034 if size(ref, 1) ~= N
0035     error('Requires the same number of points for both arrays');
0036 end
0037 
0038 % main matrix of the problem
0039 tmp = [src(:,1) src(:,2) src(:,3) ones(N,1)];
0040 A = [...
0041     tmp zeros(N, 8) ; ...
0042     zeros(N, 4) tmp zeros(N, 4) ; ...
0043     zeros(N, 8) tmp ];
0044 
0045 % conditions initialisations
0046 B = [ref(:,1) ; ref(:,2) ; ref(:,3)];
0047 
0048 % compute coefficients using least square
0049 coefs = A\B;
0050 
0051 % format to a matrix
0052 trans = [coefs(1:4)' ; coefs(5:8)'; coefs(9:12)'; 0 0 0 1];

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022