【发布时间】:2016-01-27 10:11:05
【问题描述】:
我有一个简单的 2x2 变换矩阵,s,它对坐标的一些线性变换进行编码,使得 X' = sX。
我使用 np.meshgrid() 函数在网格上生成了一组均匀分布的坐标,此时我遍历每个坐标并在坐标级别的坐标上应用变换。不幸的是,这对于大型阵列来说非常慢。有没有快速的方法来做到这一点?谢谢!
import numpy as np
image_dimension = 1024
image_index = np.arange(0,image_dimension,1)
xx, yy = np.meshgrid(image_index,image_index)
# Pre-calculated Transformation Matrix.
s = np.array([[ -2.45963439e+04, -2.54997726e-01], [ 3.55680731e-02, -2.48005486e+04]])
xx_f = xx.flatten()
yy_f = yy.flatten()
for x_t in range(0, image_dimension*image_dimension):
# Get the current (x,y) coordinate.
x_y_in = np.matrix([[xx_f[x_t]],[yy_f[x_t]]])
# Perform the transformation with x.
optout = s * x_y_in
# Store the new coordinate.
xx_f[x_t] = np.array(optout)[0][0]
yy_f[x_t] = np.array(optout)[1][0]
# Reshape Output
xx_t = xx_f.reshape((image_dimension, image_dimension))
yy_t = yy_f.reshape((image_dimension, image_dimension))
【问题讨论】:
标签: python numpy grid transform coordinate