Compute the affine transform that best register two point sets. TRANSFO = fitAffineTransform2d(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-2 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(distancePoints(REF, transformPoint(PTS, TRANSFO)).^2); Example % computes the transform the register two ellipses % create the reference poitn set elli = [50 50 40 20 30]; poly = resamplePolygonByLength(ellipseToPolygon(elli, 200), 5); figure; axis equal; axis([0 100 0 100]); hold on; drawPoint(poly, 'kx') % create the point set to fit on the reference trans0 = createRotation([20 60], -pi/8); poly2 = transformPoint(poly, trans0); poly2 = poly2 + randn(size(poly)) * 2; drawPoint(poly2, 'b+'); % compute the transform that project poly2 onto poly. transfo = fitAffineTransform2d(poly, poly2); poly2t = transformPoint(poly2, transfo); drawPoint(poly2t, 'mo') legend('Reference', 'Initial', 'Transformed'); See also transforms2d, transformPoint, transformVector, fitPolynomialTransform2d, registerICP, fitAffineTransform3d
0001 function trans = fitAffineTransform2d(ref, src) 0002 % Compute the affine transform that best register two point sets. 0003 % 0004 % TRANSFO = fitAffineTransform2d(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-2 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(distancePoints(REF, transformPoint(PTS, TRANSFO)).^2); 0011 % 0012 % Example 0013 % % computes the transform the register two ellipses 0014 % % create the reference poitn set 0015 % elli = [50 50 40 20 30]; 0016 % poly = resamplePolygonByLength(ellipseToPolygon(elli, 200), 5); 0017 % figure; axis equal; axis([0 100 0 100]); hold on; 0018 % drawPoint(poly, 'kx') 0019 % % create the point set to fit on the reference 0020 % trans0 = createRotation([20 60], -pi/8); 0021 % poly2 = transformPoint(poly, trans0); 0022 % poly2 = poly2 + randn(size(poly)) * 2; 0023 % drawPoint(poly2, 'b+'); 0024 % % compute the transform that project poly2 onto poly. 0025 % transfo = fitAffineTransform2d(poly, poly2); 0026 % poly2t = transformPoint(poly2, transfo); 0027 % drawPoint(poly2t, 'mo') 0028 % legend('Reference', 'Initial', 'Transformed'); 0029 % 0030 % See also 0031 % transforms2d, transformPoint, transformVector, 0032 % fitPolynomialTransform2d, registerICP, fitAffineTransform3d 0033 % 0034 0035 % ------ 0036 % Author: David Legland 0037 % e-mail: david.legland@inrae.fr 0038 % Created: 2009-07-31, using Matlab 7.7.0.471 (R2008b) 0039 % Copyright 2009 INRAE - Cepia Software Platform. 0040 0041 % check number of points are equal 0042 N = size(src, 1); 0043 if size(ref, 1) ~= N 0044 error('Requires the same number of points for both arrays'); 0045 end 0046 0047 % main matrix of the problem 0048 A = [... 0049 src(:,1) src(:,2) ones(N,1) zeros(N, 3) ; ... 0050 zeros(N, 3) src(:,1) src(:,2) ones(N,1) ]; 0051 0052 % conditions initialisations 0053 B = [ref(:,1) ; ref(:,2)]; 0054 0055 % compute coefficients using least square 0056 coefs = A\B; 0057 0058 % format to a matrix 0059 trans = [coefs(1:3)' ; coefs(4:6)'; 0 0 1];