Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 图像大小调整和空间更改之间的区别_Python_Image_Image Processing_Deep Learning_Simpleitk - Fatal编程技术网

Python 图像大小调整和空间更改之间的区别

Python 图像大小调整和空间更改之间的区别,python,image,image-processing,deep-learning,simpleitk,Python,Image,Image Processing,Deep Learning,Simpleitk,我的当前图像大小为(240240155),体素间距为(1,1,1)。我的最终图像应该是(128128128),体素间距为(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_spacin

我的当前图像大小为(240240155),体素间距为(1,1,1)。我的最终图像应该是(128128128),体素间距为(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)

新的体素间距将决定新的图像尺寸

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())
但是,在您的情况下,因为您需要所需大小的新图像,所以可以使用

resampled_img = sitk.Resample(itk_image, [128, 128, 128], sitk.Transform(),
sitk.sitkNearestNeighbor, itk_image.GetOrigin(), new_spacing,
                              itk_image.GetDirection(), 0.0, itk_image.GetPixelID())

但这将使新的间距为[1.875,1.875,1.2175]?我需要某种大小为(128,128,128)的各向同性采样。重采样的图像=sitk。重采样(itk_图像[128,128,128],sitk.Transform(),sitk.sitkNearestNeighbor,itk_图像.GetOrigin(),[1,1,1],itk_图像.GetDirection(),0.0,itk_图像.GetPixelID())将返回[128128]大小的重采样图像[1,1,1]间距,并使用最近邻进行插值。