Unification ct scan voxel size by using interpolation in Python

后端 未结 2 806
盖世英雄少女心
盖世英雄少女心 2021-01-24 08:29

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:



        
相关标签:
2条回答
  • 2021-01-24 09:07

    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])
    
    0 讨论(0)
  • 2021-01-24 09:32

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

    0 讨论(0)
提交回复
热议问题