In [ ]:
%matplotlib inline
http://stackoverflow.com/questions/26574303/estimate-euclidean-transformation-with-python
In [11]:
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
In [12]:
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)
Out[12]:
In [10]:
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
In [ ]: