我有代表人类乳房 3D 扫描的体积数据,体素大小为(0.2 毫米、0.073 毫米、0.47 毫米)。我需要对这些数据重新采样,以便所有体素大小都相同。是否有可用于插值重采样的 Python 函数?
或者,我希望编写一个自定义函数来实现以下目标:
volume, mask, label = dataset[0]
def resample_volume(volume, pixel_sizes={'x': 0.2, 'y': 0.073, 'z': 0.475}):
# Implementation goes here
return resampled_volume
我发现有两种选择:
def resample(x, pixel_sizes = {'x': 0.2, 'y': 0.073, 'z': 0.475674}):
# Determine the smallest pixel size
smallest_pixel_size = min(pixel_sizes.values())
# Calculate the scaling factors
scaling_factors = {dim: pixel_sizes[dim] / smallest_pixel_size for dim in pixel_sizes}
# Resample the dataset using zoom function from scipy
return zoom(x, zoom=[scaling_factors['x'], scaling_factors['y'], scaling_factors['z']], mode='nearest')
def resample_volume(volume, pixel_sizes={'x': 0.2, 'y': 0.073, 'z': 0.475}):
steps = list(pixel_sizes.values()) # original step sizes
x, y, z = [steps[k] * np.arange(volume.shape[k]) for k in range(3)] # original grid
f = RegularGridInterpolator((x, y, z), volume) # interpolator
step = min(steps)
dx, dy, dz = [step for i in range(3)] # new step sizes
new_grid = np.mgrid[0:x[-1]:dx, 0:y[-1]:dy, 0:z[-1]:dz] # new grid
new_grid = np.moveaxis(new_grid, (0, 1, 2, 3), (3, 0, 1, 2)) # reorder axes for evaluation
return f(new_grid)
第二种方法对于图像掩模产生的结果不令人满意。下图分别说明了使用
zoom
和 RegularGridInterpolator
获得的结果:
现在的问题是,有没有更好的解决方案?