我正在尝试使用ndi.interpolation.affine_transform
转换一个简单的矩阵,但我得到的结果是相反的。例如:
import scipy.ndimage as ndi
m = [[1, 1, 11], [2, 2, 22], [3, 3, 33]]
final_affine_matrix = [[1, 0], [0, 1]]
final_offset = [0, 1]
x = ndi.interpolation.affine_transform(
m,
final_affine_matrix,
final_offset,
order=1,
mode='nearest',
cval=0)
print(x)
[[ 1 11 11]
[ 2 22 22]
[ 3 33 33]]
鉴于偏移量为 [0, 1]
,难道不应该移动 y 轴而不是 x 轴吗?也就是说,输出不应该是:
[[ 2 2 22]
[ 3 3 33]
[ 3 3 33]]
我可以实现该结果,但只能使用偏移量[1, 0]
。难道不应该反过来吗?
最佳答案
如 documentation 中所述, scipy.ndimage.affine_transform()
通过执行以下操作来计算新位置:np.dot(matrix, o) + offset
(其中 o
是输出位置)。
这意味着以下测试应该成功:
m = np.array(m)
assert(m[0, 1] == x[0, 0])
assert(m[0, 2] == x[0, 1])
assert(m[1, 1] == x[1, 0])
assert(m[1, 2] == x[1, 1])
assert(m[2, 1] == x[2, 0])
assert(m[2, 2] == x[2, 1])
或者,更简洁地说:
assert(np.all(m[:, 1] == x[:, 0]))
assert(np.all(m[:, 2] == x[:, 1]))
正如预期的那样,他们确实做到了。因此,矩阵确实被移动了 1
索引。也许,您期望另一个矩阵作为输出,因为您对打印输出感到困惑。第零个索引表示行,第一个索引表示列。因此,按第一个索引移动将移动列。
关于python - 使用 ndimage.interpolation.affine_transform 进行矩阵平移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52631878/