gpt4 book ai didi

python - 使用 numpy/scipy 转换和重采样 3D 体积

转载 作者:太空狗 更新时间:2023-10-30 01:12:45 25 4
gpt4 key购买 nike

更新:

我创建了一个文档齐全的 ipython 笔记本。如果您只想要代码,请查看第一个答案。

问题

我有一个 40x40x40 体积的灰度值。这需要旋转/移动/剪切。

这是一组有用的齐次变换:http://www.lfd.uci.edu/~gohlke/code/transformations.py.html

我需要像对待一对(位置向量,值)一样对待体积中的每个体素。然后我会转换位置并从转换后的向量集中为每个坐标采样新值。

采样似乎相当困难,我很高兴发现这个: https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.ndimage.affine_transform.html#scipy.ndimage.affine_transform

The given matrix and offset are used to find for each point in the output the corresponding coordinates in the input by an affine transformation. The value of the input at those coordinates is determined by spline interpolation of the requested order. Points outside the boundaries of the input are filled according to the given mode.

听起来很完美。

但是用法很棘手。 Here有人正在使用该代码来旋转图像。他的旋转矩阵是 2x2,所以这不是齐次坐标。我尝试将齐次坐标 (2D) 中的平移矩阵传递给函数:

dim =10
arr=np.zeros((dim,dim))
arr[0,0]=1
mat=np.array([[1,0,1],[0,1,0],[0,0,1]])
out3=scipy.ndimage.affine_transform(arr,mat)
print("out3: ",out3)

产生错误:

Traceback (most recent call last):
File "C:/Users/212590884/PycharmProjects/3DAugmentation/main.py", line 32, in <module>
out3=scipy.ndimage.affine_transform(arr,mat)
File "C:\Users\212590884\AppData\Local\Continuum\Anaconda2\lib\site-packages\scipy\ndimage\interpolation.py", line 417, in affine_transform
raise RuntimeError('affine matrix has wrong number of rows')
RuntimeError: affine matrix has wrong number of rows

显然这不适用于齐次坐标。我如何使用它来移动数据?

这只是在 2D 中,在 3D 中我什至无法旋转体积:

dim =10
arr=np.zeros((dim,dim,dim))
arr[0,0]=1

angle=10/180*np.pi
c=np.cos(angle)
s=np.sin(angle)
mat=np.array([[c,-s,0,0],[s,c,0,0],[0,0,1,0],[0,0,0,1]])
out3=scipy.ndimage.affine_transform(arr,mat)
print("out3: ",out3)

错误信息是一样的:仿射矩阵的行数错误

是否可以使用这种方法来转换我的体积?

我找到了一组辅助方法,它们提供移位和旋转但不提供剪切: https://docs.scipy.org/doc/scipy-0.14.0/reference/ndimage.html

但我更愿意使用自定义转换矩阵。

最佳答案

我找到了另一个选择:map_coordinates

使用 numpy 可以生成坐标网格,然后 reshape /堆叠它们以形成位置向量。这些向量被转换并转换回 meshgrid 坐标格式。最后用 map_coordinates 解决了采样问题。

我认为这是一个常见问题,并创建了一个 ipython notebook 来逐步解释所有内容:

http://nbviewer.jupyter.org/gist/lhk/f05ee20b5a826e4c8b9bb3e528348688

还有一个问题:坐标的顺序很奇怪。您需要以一种不直观的方式重新排序网格。可能是我的代码中的错误。

请注意,坐标的这种重新排序会影响变换的轴。如果要绕x轴旋转某个东西,对应的向量不是(1,0,0)而是(0,1,0),真是奇怪。

但它有效,我认为原理很清楚。

关于python - 使用 numpy/scipy 转换和重采样 3D 体积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40524490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com