我正在寻找一个函数,它可以使用 PiecewiseAffineTransform 的所有变换矩阵应用于源数据,以便一次运行即可获取目标数据。我可以使用分段变换,但找不到同时使用所有变换矩阵的函数,此代码可以在循环中使用所有变换矩阵,这不是正确的方法,我还介绍了skimage包中的估计变换矩阵
import numpy as np
from skimage import transform as tf
from sklearn.metrics import mean_squared_error
from skimage.transform import PiecewiseAffineTransform
src = np.array([0,0 , 1,0 , 1,1 , 0,1]).reshape((4, 2))
dst = np.array([3,1 , 3,2 , 2,2 , 2,1]).reshape((4, 2))
tform = tf.estimate_transform('piecewise-affine', src, dst)
print(src)
print(dst)
print(tform.affines[0].params)
print(tform.affines[1].params)
mt = tf.matrix_transform(src, tform.affines[0].params)
print(mt)
>>> dir(tform)
['__add__', '__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_inverse_tesselation', '_tesselation', 'affines', 'estimate', 'inverse', 'inverse_affines', 'residuals']
最佳答案
只需使用源数据作为参数调用 tform
对象(请参阅 https://github.com/scikit-image/scikit-image/blob/master/skimage/transform/_geometric.py#L871 ):
In []: src
Out[]:
array([[0, 0],
[1, 0],
[1, 1],
[0, 1]])
In []: tform(src)
Out[]:
array([[ 3., 1.],
[ 3., 2.],
[ 2., 2.],
[ 2., 1.]])
关于python - 分段变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47229017/