我当前的图像大小为(240、240、155),体素间距为(1、1、1)。我的最终图像应为(128,128,128),体素间距为(1.5,1.5,1.5)。有没有办法了解这种以体素间距调整大小的现象? 我尝试了以下方法,但对我来说还不够令人满意。
import SimpleITK as sitk
import numpy as np
from skimage.transform import resize
def resize_image(image, old_spacing, new_spacing, order=3):
new_shape = (int(np.round(old_spacing[0]/new_spacing[0]*float(image.shape[0]))),
int(np.round(old_spacing[1]/new_spacing[1]*float(image.shape[1]))),
int(np.round(old_spacing[2]/new_spacing[2]*float(image.shape[2]))))
return resize(image, new_shape, order=order, mode='edge', cval=0, anti_aliasing=False)
file_path = 'some_file'
itk_image = sitk.ReadImage(file_path)
spacing = np.array(itk_image.GetSpacing())[[2, 1, 0]]
spacing_target = (1.5, 1.5, 1.5)
image = sitk.GetArrayFromImage(itk_image).astype(float)
if np.any([[i != j] for i, j in zip(spacing, spacing_target)]):
new_image = resize_image(image, spacing, spacing_target).astype(np.float32)
答案 0 :(得分:1)
新的体素间距将确定新的图像尺寸。
method
您可以通过重新采样将图像调整为所需的体素间距
spacing = itk_ct_scan.GetSpacing()
size = itk_image.GetSize()
new_spacing = [1.5,1.5,1.5]
new_size = (np.round(size*(spacing/np.array(new_spacing)))).astype(int).tolist()
但是,在您的情况下,由于您希望使用所需大小的新图像,因此可以使用
resampled_img = sitk.Resample(itk_image, new_size, sitk.Transform(),
sitk.sitkNearestNeighbor, itk_image.GetOrigin(), new_spacing,
itk_image.GetDirection(), 0.0, itk_image.GetPixelID())