<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
I think the problem here is that you have a source image with a bad affine. Simply plotting the middle slice shows a head with the nose pointing directly at one of the edges of the image. However, your affine shows a 2.4 radian rotation around the z axis. When
 nibabel attempts to reslice to a [[1, 0, 0, tx], [0, 1, 0, ty], [0, 0, 1, tz], [0, 0, 0, 1]] affine, it assumes the source affine correct and creates a data array large enough to contain the bounding box of the original image. The two images are still in alignment
 and can be viewed on top of each other.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
That said, you can write something that will keep the original rotations and only update the zooms like so:</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
# Need to calculate a shape. Since we're going to isotropic 1mm, it's just the FoV.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
new_shape = np.uint8(np.array(img_nii.shape) * np.array(img_nii.header.get_zooms()))</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
new_affine = nib.affines.rescale_affine(img_nii.affine, img_nii.shape, zooms=(1, 1, 1), new_shape=new_shape)</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
img_resampled_nii = nib.processing.resample_from_to(img, (tuple(new_shape), new_aff))<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
Best,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
Chris</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Neuroimaging <neuroimaging-bounces+markiewicz=stanford.edu@python.org> on behalf of Melike Ilteralp <milteralp@gmail.com><br>
<b>Sent:</b> Wednesday, July 13, 2022 00:14<br>
<b>To:</b> neuroimaging@python.org <neuroimaging@python.org><br>
<b>Subject:</b> [Neuroimaging] How to fix the rotation change caused by resample_to_output()?</font>
<div> </div>
</div>
<div>
<div dir="ltr">Hi all, I am working on a dataset with different voxel sizes, so I need to resample the voxel sizes to (1, 1, 1). I found nibabel.processing.resample_to_output() which accomplishes this job. However, I realized that resampling changes the rotation
 of some images. <a href="https://drive.google.com/file/d/1TPC7Go3f9TzTWNZxm8j7qi72_u-rWxGx/view?usp=sharing">
Here</a> is a slice from an image that rotates when resampled and its resampled <a href="https://drive.google.com/file/d/1Jwiv_W1pz1SLP5dmrumjGeF3tVG8xkam/view?usp=sharing">
version</a>. Is there any way to make the rotation of the resampled image the same as the original image?<br>
<br>
Here is what I tried,<br>
<br>
<blockquote style="margin:0 0 0 40px; border:none; padding:0px">import nibabel as nib<br>
from nibabel.processing import resample_to_output</blockquote>
<br>
<blockquote style="margin:0 0 0 40px; border:none; padding:0px">img_nii = nib.load('original.nii.gz')<br>
img_resampled_nii = resample_to_output(img_nii, voxel_sizes=(1, 1, 1))<br>
nib.save(img_resampled_nii, 'resampled.nii.gz')</blockquote>
<br>
You can download `original.nii.gz` from <a href="https://drive.google.com/drive/folders/1-5phtVGQy5fq1W76MtMPlw0SDGa3AABc?usp=sharing">
here</a>,<br>
<br>
Thank you for your support,<br>
Melike<br>
</div>
</div>
</body>
</html>