Resampling the 3D Image to required shape


I want to resample the 3D image to (256, 256, 166) if their shapes are different.

Example: (192, 192, 160) to (256, 256, 166)
and (256, 256, 170) to (256, 256, 166) etc

I used the below code to do that(code referred from

    target_img_shape = (256, 256, 166)

    if img_shape != target_img_shape:
        resampled_nii = resample_img(image, target_affine=np.eye(4) * 2, target_shape=target_img_shape,
        resampled_img_data = resampled_nii.get_fdata()
        resampled_img_data = image.get_fdata()

I am getting the required shape but the resultant image is too small. I have the following questions:

  1. Is the above code is correct
  2. Is there any other ways to resample the 3D image to the required size ie., (256, 256, 166)

Thank you.

Doesn’t the original interpolate function work fine for your case ?
The input dimensions are interpreted in the form: mini-batch x channels x [optional depth] x [optional height] x width.

The modes available for resizing are: nearest, linear (3D-only), bilinear, bicubic (4D-only), trilinear (5D-only), however, you can only adopt trilinear mode.