首页 > 解决方案 > 有人可以向我解释 map_coordinates 如何在 n 维数据集上工作吗?

问题描述

我有一个正在尝试转换的 3D 图像,具有已知的坐标映射。我正在尝试使用 map_coordinates 但 scipy 文档只讨论映射到一维向量,这让我很困惑。

转换是矢量化的,所以我可以给它一个 xyz 索引的网格网格,它会生成一个 3 x nx x ny x nz 数组,其中第一个索引越过向量场的 xyz 分量,其他索引直接对应于网格网格输入维度

现在我只需要将输出数组的数组元素映射到初始图像中的相应像素。我想使用 map_coordinates 但我不清楚坐标参数的格式。

谁能给我一个例子,说明在这种情况下我将如何创建坐标数组?

标签: pythonscipyndimage

解决方案


终于弄清楚了,所以我想我会把它留在这里

# transposed_frame is the 3d image that needs to be transformed (shape (632, 352, 35)) 
# Meshgrid of the matrix coordinates
x_grid, y_grid, z_grid = np.meshgrid(range(transposed_frame.shape[0]), range(transposed_frame.shape[1]), range(transposed_frame.shape[2]), indexing = 'ij')

# inverse transformation that needs to be applied to the image (shape (3, 632, 352, 35))
# the first dimension goes over the different components of the vector field x y z
# So transform[0, i, j, k] is the x coordinate of the vector field in the point [i, j, k]
transform = vectorized_interp_field(x_grid, y_grid, z_grid)

# Transforming the image through map_coordinates is then as simple as
inverse_transformed = map_coordinates(transposed_frame, transform)

我不了解 map_coordinates 的部分正是映射矩阵应该具有的高维数据形式。它似乎通常如下工作

B = map_coordinates(A, mapping)
B[i, j, k] = A[mapping[0, i, j, k], mapping[1, i, j, k], mapping[2, i, j, k]]

推荐阅读