Unification ct scan voxel size by using interpolation in Python

二次信任 提交于 2019-12-02 12:59:41

问题


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

Example of Dimensions:

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 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

What is wrong? Thank you so much.


回答1:


In MATLAB, interp2 has as arguments:

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

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).

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.]




回答2:


You might be interested in scipy.ndimage.zoom. If you are interpolating from one regular grid to another, it is much faster and easier to use than scipy.interpolate.interp2d.

See this answer for an example: https://stackoverflow.com/a/16984081/1295595

You'd probably want something like:

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


来源:https://stackoverflow.com/questions/53004228/unification-ct-scan-voxel-size-by-using-interpolation-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!