FITPOLYNOMIALTRANSFORM2D Coefficients of polynomial transform between two point sets. COEFFS = fitPolynomialTransform2d(PTS, PTSREF, DEGREE) Example See also polynomialTransform2d, fitAffineTransform2d
0001 function coeffs = fitPolynomialTransform2d(pts, ptsRef, degree) 0002 %FITPOLYNOMIALTRANSFORM2D Coefficients of polynomial transform between two point sets. 0003 % 0004 % COEFFS = fitPolynomialTransform2d(PTS, PTSREF, DEGREE) 0005 % 0006 % Example 0007 % 0008 % See also 0009 % polynomialTransform2d, fitAffineTransform2d 0010 0011 % ------ 0012 % Author: David Legland 0013 % e-mail: david.legland@nantes.inra.fr 0014 % Created: 2013-11-05, using Matlab 7.9.0.529 (R2009b) 0015 % Copyright 2013 INRA - Cepia Software Platform. 0016 0017 0018 %% Extract data 0019 0020 % ensure degree is valid 0021 if nargin < 3 0022 degree = 3; 0023 end 0024 0025 % polygon coordinates 0026 xi = pts(:,1); 0027 yi = pts(:,2); 0028 nCoords = size(pts, 1); 0029 0030 % check inputs have same size 0031 if size(ptsRef, 1) ~= nCoords 0032 error('fitPolynomialTransform2d:sizeError', ... 0033 'input arrays must have same number of points'); 0034 end 0035 0036 0037 %% compute coefficient matrix 0038 0039 % number of coefficients of polynomial transform 0040 nCoeffs = prod(degree + [1 2]) / 2; 0041 0042 % initialize matrix 0043 A1 = zeros(nCoords, nCoeffs); 0044 0045 % iterate over degrees 0046 iCoeff = 0; 0047 for iDegree = 0:degree 0048 0049 % iterate over binomial coefficients of a given degree 0050 for k = 0:iDegree 0051 iCoeff = iCoeff + 1; 0052 A1(:, iCoeff) = ones(nCoords, 1) .* power(xi, iDegree-k) .* power(yi, k); 0053 end 0054 end 0055 0056 % concatenate matrix for both coordinates 0057 A = kron(A1, [1 0;0 1]); 0058 0059 0060 %% solve linear system that minimizes least squares 0061 0062 % create the vector of expected values 0063 b = ptsRef'; 0064 b = b(:); 0065 0066 % solve the system 0067 coeffs = (A \ b)'; 0068 0069