Image Rotation with resample_img


I’m trying to perform a simple rotation around the x-axis of a 3-d image. I want the image array size (512,512,512) to stay the same before and after the transform. If I’m not mistaken, I can use resample_img to do so, correct?

cos_gamma = np.cos(0.349)
sin_gamma = np.sin(0.349)
affine_mat = np.array([[1, 0, 0, 0],
                       [0, cos_gamma, -sin_gamma, 0],
                       [0, sin_gamma, cos_gamma, 0],
                       [0, 0, 0, 1]])

before_rot = image.load_img(rsync_dir + str(subject) + "/anat/T2_0pt5_masked.nii.gz")

after_rot = image.resample_img(before_rot, 

However, this code is not working for me. Is there something I do not understand here? Is there an entirely separate function that I should be running?



Note that the field of view is changing so I don’t remeber exactly what is going to happen with the missing values
I don’t see anything weird. What happens ? The result is poor or you get an error ?

The after_rot image looks exactly the same as the before_rot image. The default fill value is 0.

Sorry if I minunderstand, but If I’m not mistaken, you should see the same thing. If you want to see a difference, take the data of the rotated image with the affine of the non-rotated image:

import nibabel as nib
chimera = nib.Nifti1Image(after_rot.get_data(), before_rot.affine)

This code worked. Thanks (once again) @bthirion ! I’m starting to understand the affine better, thanks for your patience.