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

时间:2018-10-26 08:07:41

标签: python matlab interpolation dicom

我在Matlab中使用了interp2,例如以下代码,这是@rayryeng在以下内容中的答案的一部分:Three dimensional (3D) matrix interpolation in Matlab

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的语法为:(行,列,切片在第3维中),Python语法的大小为:(切片在第3个暗部,行,列中)。 但是,将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

怎么了?非常感谢。

2 个答案:

答案 0 :(得分:2)

在MATLAB中,interp2作为参数:

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

您仅使用后三个参数,假定前两个参数为input_x = 1:size(input_z,2)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)

[代码未经测试,如果有错误,请告诉我。]

答案 1 :(得分:0)

您可能对scipy.ndimage.zoom感兴趣。如果要从一个常规网格插入到另一个常规网格,则比scipy.interpolate.interp2d更快,更容易使用。

有关示例,请参见以下答案: https://stackoverflow.com/a/16984081/1295595

您可能想要类似的东西:

import scipy.ndimage as ndimage
M2D = ndimage.zoom(volume_image, (1, scaleCoeff[0], scaleCoeff[1])