通过在Python中使用插值来统一ct扫描体素大小 [英] Unification ct scan voxel size by using interpolation in Python
问题描述
我在Matlab中使用了interp2
,例如以下代码,它是@rayryeng的答案的一部分:
I have used interp2
in Matlab, such as the following code, that is part of @rayryeng's answer in: 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
:
Aslo, Matlabs syntax for size: (rows, columns, slices in 3rd dimenson) and Python syntax for size: (slices in 3rd dim, rows, columns).
However, after convert the Matlab code to Python code occurred an error, 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
怎么了?非常感谢.
推荐答案
In MATLAB, interp2
has as arguments:
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)
.
You are using only the latter 3 arguments, the first two are assumed to be input_x = 1:size(input_z,2)
and input_y = 1:size(input_z,1)
.
在Python中, scipy.interpolate.interp2
完全不同:它采用MATLAB函数的前3个输入参数,并返回一个您可以调用以获取内插值的对象:
In Python, scipy.interpolate.interp2
is quite different: it takes the first 3 input arguments of the MATLAB function, and returns an object that you can call to get interpolated values:
f = scipy.interpolate.interp2(input_x, input_y, input_z)
result = f(output_x, output_y)
按照文档中的示例,我得到如下信息:
Following the example from the documentation, I get to something like this:
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)
[代码未经测试,如果有错误,请告诉我.]
[Code not tested, please let me know if there are errors.]
这篇关于通过在Python中使用插值来统一ct扫描体素大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!