Least Squares Solution for Simultaneous Equations

I am trying to fine-tune a transformation from one set of coordinates to another.

x' = R + Px + Qy
y' = S - Qx + Py
Where P,Q,R,S are constants, P = scale*cos(rotation). Q=scale*sin(rotation)

There is a well-known “manual” formula for fitting P, Q, R, S to a set of corresponding points. But I need to have a fit error estimate - so I need a least squares solution.

Read the Numerical Recipes, but I am having trouble developing how to do this for datasets with x and y in them.

Can someone point out an example / tutorial / sample code on how to do this?
Not too worried about the language.
But - just use the built-in Matlab / Lapack / numpy / R function, probably not useful!

edit: I have a large set of old (x, y) new (x, y) to match. The problem is overridden (there are more data points than unknowns), so a simple matrix inversion is not enough - and, as I said, I really need an error when fitting.

+5
source share
6 answers

The following code should do the trick. I used the following formula for leftovers:

residual[i] =   (computed_x[i] - actual_x[i])^2
              + (computed_y[i] - actual_y[i])^2

And then he derived the least squares formulas based on the general procedure

+3
source

P, Q, R S, . , , x y, x y . . x y, x ' y', - P, Q, R S. ( , , .)

, P, Q, R S, scale = sqrt (P ^ 2 + Q ^ 2), sin = Q/scale cos rotation = P/.

+4

3x3 T (P, Q, R, S) , (x',y',1) = T (x,y,1).

A = \sum_i |(T (x_i,y_i,1)) - (x'_i,y'_i,1)|^2

A (P, Q, R, S).

- , , , . , , .

minuit CERNLIB ( fortran77) ROOT ( ​​++, , python). , .

, .

+1

levmar . , . GPL, , ( )

+1

eJames, , . , " ", 100 ! ( N E , x/y)

- , .

FindTransformation(vector<Point2D> known,vector<Point2D> unknown) {
{
    // sums
    for (unsigned int ii=0;ii<known.size();ii++) {
       sum_e += unknown[ii].x;
       sum_n += unknown[ii].y;
       sum_E += known[ii].x;
       sum_N += known[ii].y;                            
       ++n;         
    }

    // mean position
    me = sum_e/(double)n;
    mn = sum_n/(double)n;
    mE = sum_E/(double)n;
    mN = sum_N/(double)n;

    // differences
    for (unsigned int ii=0;ii<known.size();ii++) {

       de = unknown[ii].x - me;
       dn = unknown[ii].y - mn;

       // for P
       sum_deE += (de*known[ii].x);
       sum_dnN += (dn*known[ii].y);
       sum_dee += (de*unknown[ii].x);
       sum_dnn += (dn*unknown[ii].y);

       // for Q
       sum_dnE += (dn*known[ii].x);
       sum_deN += (de*known[ii].y);                     
   }

double P = (sum_deE + sum_dnN) / (sum_dee + sum_dnn);
double Q = (sum_dnE - sum_deN) / (sum_dee + sum_dnn);

double R = mE - (P*me) - (Q*mn);
double S = mN + (Q*me) - (P*mn);
}
+1

One problem is that such numerical elements are often complex. Even when the algorithms are simple, problems often arise in the actual calculation.

For this reason, if there is a system that you can easily get that has a built-in function, it is best to use it.

0
source

All Articles