title: Python get transformation matrix from two sets of points
body: <p>I have to images, one simulation, one real data, with bright spots.</p>
  
  <p>Simulation:
  <a href="https://i.stack.imgur.com/fmW0H.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fmW0H.png" alt="Simulation"></a></p>
  
  <p>Reality:
  <a href="https://i.stack.imgur.com/H5D92.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H5D92.jpg" alt="reality"></a></p>
  
  <p>I can detect the spots just fine and get the coordinates. Now I need to compute transformation matrix (scale, rotation, translation, maybe shear) between the two coordinate systems. If needed, I can pick some (5-10) corresponding points by hand to give to the algorithm</p>
  
  <p>I tried a lot of approaches already, including:
  2 implementations of ICP:
  <a href="https://engineering.purdue.edu/kak/distICP/ICP-2.0.html#ICP" rel="nofollow noreferrer">https://engineering.purdue.edu/kak/distICP/ICP-2.0.html#ICP</a></p>
  
  <p><a href="https://github.com/KojiKobayashi/iterative_closest_point_2d" rel="nofollow noreferrer">https://github.com/KojiKobayashi/iterative_closest_point_2d</a></p>
  
  <p>Implementing affine transformations:
  <a href="https://math.stackexchange.com/questions/222113/given-3-points-of-a-rigid-body-in-space-how-do-i-find-the-corresponding-orienta/222170#222170">https://math.stackexchange.com/questions/222113/given-3-points-of-a-rigid-body-in-space-how-do-i-find-the-corresponding-orienta/222170#222170</a></p>
  
  <p>Implementations of affine transformations:
  <a href="https://stackoverflow.com/questions/27546081/determining-a-homogeneous-affine-transformation-matrix-from-six-points-in-3d-usi?answertab=votes#tab-top">Determining a homogeneous affine transformation matrix from six points in 3D using Python</a></p>
  
  <p><a href="https://stackoverflow.com/questions/20546182/how-to-perform-coordinates-affine-transformation-using-python-part-2?answertab=votes#tab-top">how to perform coordinates affine transformation using python? part 2</a></p>
  
  <p>Most of them simply fail somehow like this:
  <a href="https://i.stack.imgur.com/LSDZ7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LSDZ7.png" alt="enter image description here"></a>
  The red points are the spots from the simulation transformed into the reality - coordinate system.</p>
  
  <p>The best approach so far is this one <a href="https://stackoverflow.com/questions/20546182/how-to-perform-coordinates-affine-transformation-using-python-part-2?answertab=votes#tab-top">how to perform coordinates affine transformation using python? part 2</a> yielding this:
  <a href="https://i.stack.imgur.com/wz9is.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wz9is.jpg" alt="enter image description here"></a></p>
  
  <p>As you see, the scaling and translating mostly works, but the image still needs to be rotated / mirrored.</p>
  
  <p>Any ideas on how to get a working algorithm? If neccessary, I can provide my current non-working implementations, but they are basically as linked.</p>