简体   繁体   English

numpy.linalg.lstsq 的向量化

[英]Vectorisation of numpy.linalg.lstsq

I have two sets of points (A1, A2, B1, B2) for which I want to calculate the affine transformation (from A1 to B1, from A2 to B2).我有两组点(A1、A2、B1、B2),我想为其计算仿射变换(从 A1 到 B1,从 A2 到 B2)。 Using numpy.linalg.lstsq this is very straightforward for a single case:使用 numpy.linalg.lstsq 这对于单个案例非常简单:

A1 = [[100   0   0]
      [  0 100   0]
      [100 100   0]
      [  0   0 100]
      [100 100 100]]

B1 = [[160   0   0]
      [  0 160   0]
      [160 160   0]
      [  0   0 160]
      [160 160 160]]

A1 = np.hstack([A1, np.ones((A1.shape[0], 1))])
B1 = np.hstack([B1, np.ones((B2.shape[0], 1))])
affine_transformation = np.linalg.lstsq(A1, B1, rcond=None)[0].transpose()

I want to vectorise this to calculate it for multiple sets of points without a loop.我想对它进行矢量化以在没有循环的情况下为多组点计算它。 I would like to end up with something like this:我想以这样的方式结束:

pts_a = np.array([A1, A2])
pts_a = np.pad(pts_a, ((0, 0), (0, 0), (0,1)), mode='constant', constant_values=1)
pts_b = np.array([B1, B2])
pts_b = np.pad(pts_b, ((0, 0), (0, 0), (0,1)), mode='constant', constant_values=1)
affine_transformation = np.linalg.lstsq(pts_a, pts_b, rcond=None)[0].transpose()

Any help is much appreciated!任何帮助深表感谢!

I sorted it by using this:我用这个对它进行了排序:

 affine_transformation = np.asarray([np.linalg.lstsq(pts_a[i,:,:], pts_b[i,:,:], rcond=None)[0].transpose() for i in range(pts_a.shape[0])])

Is there a more efficient way?有没有更有效的方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM