SimpleITK wrong Resampling

Guys, I need help reviewing this code. I am trying to resample a MRI FLAIR image in to a 1mm x 1mm x 1mm space, and then resample that image’s label according to the resampled image.
Everything sound ok, BUT the result is a TOTAL zero label image.

Please take a look:

 interpolator_image = sitk.sitkBSplineResamplerOrder3
    interpolator_label = sitk.sitkNearestNeighbor
    
    pixelType_image = sitk.sitkFloat32
    pixelType_label = sitk.sitkUInt16

    transform = sitk.Transform(3, sitk.sitkIdentity)
    spacing = (1.0, 1.0, 1.0)
    origin = image.GetOrigin()
    direction = image.GetDirection()
    size = image.GetSize()
    defaultPixelValue = 0.0
    
    resampler = sitk.ResampleImageFilter()
    resampler.SetDefaultPixelValue(defaultPixelValue)
    resampler.SetInterpolator(interpolator_image)
    resampler.SetOutputDirection(direction)
    resampler.SetOutputOrigin(origin)
    resampler.SetOutputPixelType(pixelType_image)
    resampler.SetOutputSpacing(spacing)
    resampler.SetSize(size)
    resampler.SetTransform(transform)
    resampler.SetUseNearestNeighborExtrapolator(False)
    
    resampled_flair = resampler.Execute(image)

    resampled_label = sitk.Resample(label_image, resampled_flair);

Any help is welcomed.

Hello @machadoL,

When performing resampling we define a physical grid in space. In the code above the grid spacing is set to an arbitrary spacing, [1,1,1], which is fine. The issue is that the grid size is kept to the original, which means that this grid occupies a different region in space from that occupied by the original image. That is, the resampled MRI flair is likely also not what you expected.

When defining a new grid over the same physical space we change both the spacing and the size:

new_size = [int(round(osz*ospc/spacing)) for osz,ospc in zip(original_size, original_spacing)]

For further insight, take a look at the make_isotropic function in this Jupyter notebook.

2 Likes