resample method and code notes of Python SimpleItk Library

Posted by rubing on Fri, 04 Mar 2022 11:59:14 +0100

Note: some pits explored on resample and the simple method of final discovery
Requirements: there are already registered CT and PET images, and the gold standard label is drawn on CT, so there are some simple requirements. One is to resample the PET image to the same size as the CT image (for example, from 192) × one hundred and ninety-two × 371 to 512 × five hundred and twelve × 484), or reduce the gold standard Mask to the same size as pet (i.e. vice versa).
How to find it: I have used SITK-SNAP (version 3.8). One of its image reading functions supports the display of images of different sizes, spacing, origin and direction. The software will be equivalent to giving you a resample; Therefore, it is known that there are solutions to ITK, and the rest is to find the corresponding code.

Direct to the final Python 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: Original alignment required itk image
    :param target_img: Target to align itk image
    :param resamplemethod: itk interpolation method : sitk.sitkLinear-linear  sitk.sitkNearestNeighbor-Nearest neighbor
    :return:img_res_itk: Resampling okay itk image
    """
    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()      # Starting point of target [x,y,z]
    target_direction = target_img.GetDirection()  # Target direction [crown, sagittal, transverse] = [z,y,x]

    # The method of itk is resample
    resampler = sitk.ResampleImageFilter()
    resampler.SetReferenceImage(ori_img)  # Target image to resample
    # 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 dype according to the need to resample the image
    if resamplemethod == sitk.sitkNearestNeighbor:
        resampler.SetOutputPixelType(sitk.sitkUInt16)   # Nearest neighbor interpolation is used for mask, and uint16 is saved
    else:
        resampler.SetOutputPixelType(sitk.sitkFloat32)  # Linear interpolation is used for PET/CT/MRI and the like, and float32 is saved
    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

Stepped pit:

When I looked for the code on the Internet before, I saw such a code. This is to calculate a new_size, that is, the size of the resampled image is calculated through the spacing of the two images. The code is as follows:

# The initial version, from online, is applicable to two diagrams with only different spacing
def resize_image(itkimage, newSize, resamplemethod=sitk.sitkNearestNeighbor):
    print('--resize ing--')
    resampler = sitk.ResampleImageFilter()
    originSize = itkimage.GetSize()  # Original voxel block size
    originSpacing = itkimage.GetSpacing()

    newSize = np.array(newSize, float)
    factor = originSize / newSize
    newSpacing = originSpacing * factor
    newSize = newSize.astype(np.int)    # spacing must not be an integer

    resampler.SetReferenceImage(itkimage)  # Target image to resample
    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

In actual use, it is found that if you use the calculated newsize to resampler SetSize (new_size)_ The size is not necessarily the same as the size of the target image, because the two images are likely to be inconsistent in origin and direction (especially for medical images). Therefore, using this code will lead to various post-processing operations for the image after resample.. pad or cut or something (I thought a lot of code, but I couldn't use it in the end, tears)

Finally, through the sitk of this code Resampleimagefilter(), I went in and looked at various function functions. I suddenly wanted to try whether if the set is the size of the target image, I should give the correct results directly like the software. It was found that it is really possible....

However, I learned a lot about the attributes of itk images and the transformation of spatial coordinates. It's OK..
Jot down this episode
2021/04/17

Topics: Python Algorithm image processing