在Python中使用插值统一ct扫描体素大小

2023-12-06

我用过interp2在Matlab中,例如以下代码,这是@rayryeng的答案的一部分:Matlab 中的三维 (3D) 矩阵插值:

d = size(volume_image)
[X,Y] = meshgrid(1:1/scaleCoeff(2):d(2), 1:1/scaleCoeff(1):d(1));
for ind = z
    %Interpolate each slice via interp2   
    M2D(:,:,ind) = interp2(volume_image(:,:,ind), X, Y);   
end

尺寸示例:

The image size is 512x512 and the number of slices is 133. So:
volume_image(rows, columns, slices in 3D dimenson) : 512x512x133 in 3D dimenson
X: 288x288
Y: 288x288
scaleCoeff(2): 0.5625
scaleCoeff(1): 0.5625
z = 1 up to 133 ,hence z: 1x133
ind: 1 up to 133
M2D(:,:,ind) finally is 288x288x133 in 3D dimenson

Aslo,大小的 Matlabs 语法:(第三维中的行、列、切片)和大小的 Python 语法:(第三维中的切片、行、列)。 但是,将Matlab代码转换为Python代码后出现错误,ValueError: Invalid length for input z for non rectangular grid:

for ind in range(0, len(z)+1):
    M2D[ind, :, :] = interpolate.interp2d(X, Y, volume_image[ind, :, :]) # ValueError: Invalid length for input z for non rectangular grid

怎么了?太感谢了。


在 MATLAB 中,interp2有作为参数:

result = interp2(input_x, input_y, input_z, output_x, output_y)

您仅使用后 3 个参数,前两个参数被假定为input_x = 1:size(input_z,2) and input_y = 1:size(input_z,1).

在Python中,scipy.interpolate.interp2完全不同:它采用 MATLAB 函数的前 3 个输入参数,并返回一个对象,您可以调用该对象来获取插值:

f = scipy.interpolate.interp2(input_x, input_y, input_z)
result = f(output_x, output_y)

按照文档中的示例,我得到如下结果:

from scipy import interpolate
x = np.arange(0, volume_image.shape[2])
y = np.arange(0, volume_image.shape[1])
f = interpolate.interp2d(x, y, volume_image[ind, :, :])
xnew = np.arange(0, volume_image.shape[2], 1/scaleCoeff[0])
ynew = np.arange(0, volume_image.shape[1], 1/scaleCoeff[1])
M2D[ind, :, :] = f(xnew, ynew)

【代码未经测试,如有错误请告知。】

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在Python中使用插值统一ct扫描体素大小 的相关文章

随机推荐