title: Transformation between two set of points
body: <p>I have object let's say on model image. I want to compute transformation (displacement, scale, rotation) between object on model image and object on target image. I want to make assumption that object's can be treated as 2D so only 2D transformations should be computed.</p>
  
  <p>First I want to to it in manually assisted way. The user selects base point on model image and then target point on target image. The number of points should be defined by user (but no less than some minimum 2-3 points). When points gives different information, the transformation should be averaged and for example from this the quality of matching can be computed.</p>
  
  <p>So the questions is rather about computing transformation of two sets of points, but as I want to do it on image I've added image processing tag.</p>
  
  <p>Especially welcomed are references and advices with some pieces of code or pseudocode.</p>
  
  <p>With two points it's very easy issue, only rotation, scale and displacement of line should be taken, but how to do it with more points, and with averaging it and computing some quality factors.</p>
  
  <p><strong>Current solution is:</strong></p>
  
  <pre><code>void transformFnc(std::vector&lt;PointF&gt; basePoints, std::vector&lt;PointF&gt; targetPoints,
                    PointF&amp; offset, double rotation, double scale)
  {
      std::vector&lt;Line&gt; basePointsLines;
      std::vector&lt;Line&gt; targetPointsLines;
      assert(basePoints.size() == targetPoints.size());
      int pointsNumber = basePoints.size();
  
      for(int i = 0; i &lt; pointsNumber; i++)
      {
           for(int j = i + 1; j &lt; pointsNumber; j++)
           {
               basePointsLines.push_back(Line(basePoints[i], basePoints[j]));
               targetPointsLines.push_back(Line(targetPoints[i], targetPoints[j]));
           }
      }
      std::vector&lt;double&gt; scalesVector;
      std::vector&lt;double&gt; rotationsVector;
      double baseCenterX = 0, baseCenterY = 0, targetCenterX = 0, targetCenterY = 0;
      for(std::vector&lt;Line&gt;::iterator it = basePointsLines.begin(), i = targetPointsLines.begin();
          it != basePointsLines.end(), i != targetPointsLines.end(); it++, i++)
      {
          scalesVector.push_back((*i).length()/(*it).length());
          baseCenterX += (*it).pointAt(0.5).x(); 
          baseCenterY += (*it).pointAt(0.5).y();
          targetCenterX += (*i).pointAt(0.5).x();
          targetCenterY += (*i).pointAt(0.5).y();
          double rotation;
          rotation = (*i).angleTo((*it));
          rotationsVector.push_back(rotation);
      }
      baseCenterX = baseCenterX / pointsNumber;
      baseCenterY = baseCenterY / pointsNumber;
      targetCenterX = targetCenterX / pointsNumber;
      targetCenterY = targetCenterY / pointsNumber;
  
      offset = PointF(targetCenterX - baseCenterX, targetCenterY - baseCenterY);
      scale = sum(scalesVector) / scalesVector.size();
      rotation = sum(rotationsVector) / rotationsVector.size();
  }
  </code></pre>
  
  <p>Only optimization I can find in this code is to eliminate from scales and rotations those values which differs too much from the rest.</p>
  
  <p><strong>I'm looking for codes or pseudocodes of solution propositions. It can also be references to some codes.</strong></p>
  
  <p>So far from answers I know that:</p>
  
  <ul>
  <li>RANSAC algorithm can be used</li>
  <li>I need to look for some affine transform computing algorithm in the least square sense</li>
  </ul>