This is a reprinted article , Link to the original text :https://blog.csdn.net/weixin_40640335/article/details/115798249
remember : About image resampling (resample) The simple implementation method finally found
demand : There are already registered CT as well as PET Images , And the gold standard label Is in CT Outlined on , So there are some simple requirements , One is the PET Image resampling to and CT The same size as the image ( For instance from 192×192×371 To 512×512×484), Or put the gold standard Mask Down to the same PET Size ( That is, the reverse ).
How to find the way :ITK-SNAP(3.8 edition ) The function of reading pictures is to support different sizes 、spacing、origin、direction Together with the images of , The software will be equivalent to resample Images added later ; therefore , It is known that ITK There is a solution , The rest is to find the corresponding code .
import SimpleITK as sitk
def resize_image_itk(ori_img, target_img, resamplemethod=sitk.sitkNearestNeighbor):
""" use itk Method to convert the original image resample To be consistent with the target image :param ori_img: The original needs to be aligned itk Images :param target_img: Target to align itk Images :param resamplemethod: itk Interpolation method : sitk.sitkLinear- linear sitk.sitkNearestNeighbor- Nearest neighbor :return:img_res_itk: Resampling ok itk Images Use demonstration : import SimpleITK as sitk target_img = sitk.ReadImage(target_img_file) ori_img = sitk.ReadImage(ori_img_file) img_r = resize_image_itk(ori_img, target_img, resamplemethod=sitk.sitkLinear) """
target_Size = target_img.GetSize() # Target image size [x,y,z]
target_Spacing = target_img.GetSpacing() # Voxel block size of the target [x,y,z]
target_origin = target_img.GetOrigin() # The starting point of the goal [x,y,z]
target_direction = target_img.GetDirection() # The direction of the goal [ crown , Arrow , cross ]=[z,y,x]
# itk Method resample
resampler = sitk.ResampleImageFilter()
resampler.SetReferenceImage(ori_img) # The target image that needs to be resampled
# Set the information of the target image
resampler.SetSize(target_Size) # Target image size
resampler.SetOutputOrigin(target_origin)
resampler.SetOutputDirection(target_direction)
resampler.SetOutputSpacing(target_Spacing)
# Set different settings according to the need to resample the image dype
if resamplemethod == sitk.sitkNearestNeighbor:
resampler.SetOutputPixelType(sitk.sitkUInt8) # Nearest neighbor interpolation is used for mask Of , preservation uint8
else:
resampler.SetOutputPixelType(sitk.sitkFloat32) # Linear interpolation is used for PET/CT/MRI And so on. , preservation float32
resampler.SetTransform(sitk.Transform(3, sitk.sitkIdentity))
resampler.SetInterpolator(resamplemethod)
itk_img_resampled = resampler.Execute(ori_img) # Get the resampled image
return itk_img_resampled
I found such a code at the beginning , Through two images spacing To calculate the size of the resampled image :
# The initial release ,from online , Apply to only spacing Two different graphs
def resize_image(itkimage, newSize, resamplemethod=sitk.sitkNearestNeighbor):
print('--resize ing--')
resampler = sitk.ResampleImageFilter()
originSize = itkimage.GetSize() # The original voxel block size
originSpacing = itkimage.GetSpacing()
newSize = np.array(newSize, float)
factor = originSize / newSize
newSpacing = originSpacing * factor
newSize = newSize.astype(np.int) # spacing It must not be an integer
resampler.SetReferenceImage(itkimage) # The target image that needs to be resampled
resampler.SetSize(newSize.tolist())
resampler.SetOutputSpacing(newSpacing.tolist())
resampler.SetTransform(sitk.Transform(3, sitk.sitkIdentity))
resampler.SetInterpolator(resamplemethod)
itk_img_res = resampler.Execute(itkimage) # Get the resampled image
print('--resize finish--')
return itk_img_res
Practical use found , If by calculation newsize Come on resampler.SetSize(new_size) Words ,new_size Not necessarily with the target image size Agreement , The reason is that the two images are origin、direction Aspects are likely to be inconsistent ( Especially for medical imaging ), Therefore, the use of this code will lead to the need for resample Various post-processing operations are performed on the post-processing image , such as pad Empty matrix or cut Images or something , More complex space coordinate transformation will be involved .
Finally, check sitk.ResampleImageFilter() Each function of , Suddenly want to try if setsize Is the size of the target image , Should we give the right results directly like the software , Results found , exactly ...
reminder :
Read in ori and target Of sitk In the picture , Make sure spacing This information is not lost , If there is from sitk turn array After treatment, it will be transferred back sitk If you want to operate it , Remember to use sitk.CopyInformation( ) Copy the original information resample, Otherwise, the output image will be empty matrix .