2

Given set of points in 3D ( X = (x1, x2, x3), Y = (y1, y2, y3) ), how can I fit transformation from X to Y?

As far as I know this is called projective transformation.
Here is example of X and Y.

Blue and red lines in X are parallel, but they are not parallel in Y.

alt text

alt text

qutron
  • 1,710
  • 4
  • 18
  • 30

3 Answers3

2

Projective transformations in 3d have an associated 4x4 matrix (modulo a constant multiplication). You can find the matrix with least square fitting.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
1

Well. I found some useful information:

This transformation is non-linear and it is not possible to represent non-linear transformation with a matrix. There are some tricks such as using homogenous coordinates. but it doesn't make all non-linear transformations representable using matrices. However, approximating a nonlinear function by a linear function is possible.

qutron
  • 1,710
  • 4
  • 18
  • 30
  • The transformation is projective. Projective transformations are represented with matrices defined up to a multiplicative constant. – Alexandre C. Sep 15 '11 at 21:56
1

So, the task is to find best fitting linear transformation, right?

There is a simple solution using linear regression.

Say the transformation matrix is named A and has dimensions 3x3. And say you have N vectors (points) in 3D before and after the transformation - so you have matrices X and Y of 3 rows and N columns. Then the transformation is:

Y = A X + B

where B is a vector of length 3 and specifies the shift. You can rewrite the matrix multiplication using indices:

y[i,j] = sum(k=1..3)(a[i,k] * x[k,j])  + b[i]

for i = 1..3 and j = 1 .. N. So, you have 12 unknown variables (a, b), and 3 * N equations. For N >= 4, you simply find the best solution using linear regression.

For example, in R it is very easy:

# input data
X = matrix(c(c(0, 0, 0), c(1, 0, 0), c(0, 1, 0), c(0, 1, 1)), nrow = 3)
Y = matrix(c(c(1, 0, 1), c(2, 0, 1), c(1, 1, 1), c(1, 1, 2)), nrow = 3)
# expected transformation: A is identity matrix, b is [1, 0, 1]
N = dim(Y)[2]

# transform data for regression
a1 = rbind(t(X), matrix(rep(0, 3*2*N), ncol = 3))
a2 = rbind(matrix(rep(0, 3*N), ncol = 3), t(X), matrix(rep(0, 3*N), ncol = 3))
a3 = rbind(matrix(rep(0, 3*2*N), ncol = 3), t(X))
b1 = rep(1:0, c(N, 2*N))
b2 = rep(c(0, 1, 0), each = N)
b3 = rep(0:1, c(2*N, N))
y = as.vector(t(Y))

# do the regression
summary(lm(y ~ 0 + a1 + a2 + a3 + b1 + b2 + b3))

And the output is:

[...]

Coefficients:
      Estimate Std. Error t value Pr(>|t|)
a11  1.000e+00         NA      NA       NA
a12 -2.220e-16         NA      NA       NA
a13 -3.612e-32         NA      NA       NA
a21  7.850e-17         NA      NA       NA
a22  1.000e+00         NA      NA       NA
a23 -1.743e-32         NA      NA       NA
a31  0.000e+00         NA      NA       NA
a32  0.000e+00         NA      NA       NA
a33  1.000e+00         NA      NA       NA
b1   1.000e+00         NA      NA       NA
b2  -7.850e-17         NA      NA       NA
b3   1.000e+00         NA      NA       NA

Residual standard error: NaN on 0 degrees of freedom
Multiple R-squared:     1,      Adjusted R-squared:   NaN 
F-statistic:   NaN on 12 and 0 DF,  p-value: NA 

as expected.

Tomas
  • 57,621
  • 49
  • 238
  • 373
  • Not linear transformation. Projective transformation. But this should be easy to adapt (projective transformations are roughly speaking 4x4 matrices whose entries sum to one). – Alexandre C. Sep 15 '11 at 21:55
  • The OP's request is indeed projective, but a linear projection would satisfy me. I'm afraid I'm not familiar with R. Could you possibly provide code (using EMGU+OpenCV if necessary) that I could translate to C# or VB.Net? Thanks. – smirkingman Sep 18 '11 at 17:32
  • @smirkingman, just use any library for linear regression to estimate matrices A and B as parameters, as I sketched above (probably use the second equation with indices). Or, if you don't find any library for C#, you can either [interface R from C#](http://stackoverflow.com/questions/5377070/c-r-interface), or look at how linear regression works and write your own expressions for parameter estimates. – Tomas Sep 18 '11 at 22:03