POLYNOMIALTRANSFORM2D Apply a polynomial transform to a set of points. RES = polynomialTransform2d(PTS, COEFFS) Transforms the input points PTS given as a N-by-2 array of coordinates using the polynomial transform defined by PARAMS. PARAMS given as [a0 b0 a1 b1 ... an bn] Example coeffs = [0 0 1 0 0 1 0.1 0 0 0 0 0.1]; % cte x y x^2 x*y y^2 pts = rand(200, 2) * 2 - 1; pts2 = polynomialTransform2d(pts, coeffs); figure; hold on; drawPoint(pts); drawPoint(pts2, 'g'); See also transformPoint, fitPolynomialTransform2d
0001 function res = polynomialTransform2d(pts, coeffs) 0002 %POLYNOMIALTRANSFORM2D Apply a polynomial transform to a set of points. 0003 % 0004 % RES = polynomialTransform2d(PTS, COEFFS) 0005 % Transforms the input points PTS given as a N-by-2 array of coordinates 0006 % using the polynomial transform defined by PARAMS. 0007 % PARAMS given as [a0 b0 a1 b1 ... an bn] 0008 % 0009 % Example 0010 % coeffs = [0 0 1 0 0 1 0.1 0 0 0 0 0.1]; 0011 % % cte x y x^2 x*y y^2 0012 % pts = rand(200, 2) * 2 - 1; 0013 % pts2 = polynomialTransform2d(pts, coeffs); 0014 % figure; hold on; 0015 % drawPoint(pts); 0016 % drawPoint(pts2, 'g'); 0017 % 0018 % See also 0019 % transformPoint, fitPolynomialTransform2d 0020 0021 % ------ 0022 % Author: David Legland 0023 % e-mail: david.legland@grignon.inra.fr 0024 % Created: 2013-09-04, using Matlab 7.9.0.529 (R2009b) 0025 % Copyright 2013 INRA - Cepia Software Platform. 0026 0027 x = pts(:,1); 0028 y = pts(:,2); 0029 nPoints = length(x); 0030 0031 0032 xCoeffs = coeffs(1:2:end); 0033 yCoeffs = coeffs(2:2:end); 0034 nCoeffs = length(xCoeffs); 0035 0036 % allocate memory for result 0037 x2 = zeros(nPoints, 1); 0038 y2 = zeros(nPoints, 1); 0039 0040 % degree from coefficient number 0041 degree = sqrt(9/4 - 4*(1 - nCoeffs)/2) - 1.5; 0042 0043 % iterate over degrees 0044 iCoeff = 0; 0045 for iDegree = 0:degree 0046 0047 % iterate over binomial coefficients of a given degree 0048 for k = 0:iDegree 0049 iCoeff = iCoeff + 1; 0050 tmp = power(x, iDegree-k) .* power(y, k); 0051 x2 = x2 + xCoeffs(iCoeff) .* tmp; 0052 y2 = y2 + yCoeffs(iCoeff) .* tmp; 0053 end 0054 end 0055 0056 res = [x2 y2];