<jupyter_start><jupyter_text>Optical Match rotation only, no scalinghttp://stackoverflow.com/questions/26574303/estimate-euclidean-transformation-with-python<jupyter_code>from skimage import io, transform
import numpy as np
from matplotlib.pylab import *
from scipy.optimize import *

dst = np.array([[1.0,2.0],[1.0,4.0],[3.0,3.0],[3.0,7.0]])
src = np.array([[1.2,1.7],[1.1,3.8],[3.1,3.4],[2.6,7.0]])
T = transform.estimate_transform('similarity',src, dst)
print T
fig, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(10, 5), sharex=True,
                                    sharey=True)
ax0.set_title('src')
ax1.set_title('dst')
ax0.plot(src[:, 1], src[:, 0], '-r', linewidth=4)
ax1.plot(dst[:, 1], src[:, 0], '-r', linewidth=4)


def obj_fun(pars,x,src):
    theta, tx, ty = pars
    H = array([[cos(theta), -sin(theta), tx],\
         [sin(theta), cos(theta), ty],
         [0,0,1]])
    src1 = c_[src,ones(src.shape[0])]
    return sum( (x - src1.dot(H.T)[:,:2])**2 )

def apply_transform(pars, src):
    theta, tx, ty = pars
    H = array([[cos(theta), -sin(theta), tx],\
         [sin(theta), cos(theta), ty],
         [0,0,1]])
    src1 = c_[src,ones(src.shape[0])]
    return src1.dot(H.T)[:,:2]

res = minimize(obj_fun,[0,0,0],args=(dst,src), method='Nelder-Mead')
print res<jupyter_output>final_simplex: (array([[-0.14666661, -0.55942995,  0.35993965],
       [-0.1466779 , -0.55949117,  0.35998586],
       [-0.14667076, -0.55942846,  0.35997133],
       [-0.14665231, -0.55937279,  0.35992499]]), array([ 0.12548207,  0.12548207,  0.12548207,  0.12548207]))
           fun: 0.12548207150436777
       message: 'Optimization terminated successfully.'
          nfev: 194
           nit: 106
        status: 0
       success: True
             x: array([-0.14666661, -0.55942995,  0.35993965])
{'fraction_non_alphanumeric': 0.2054414214325375, 'fraction_numerical': 0.1504719600222099, 'mean_word_length': 3.152073732718894, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '255718', 'license': 'MIT', 'n_tokens_mistral': 884, 'n_tokens_neox': 754, 'n_words': 132, 'path': 'TEMCA/OpticalMatchRotationOnly.ipynb', 'repo_name': 'jaybo/Python-Notebooks', 'size': '1801'}