<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <p>Hello</p>
    <p>I also notice that if you just apply a zoom you end up with an
      affine  with the exact same translation.</p>
    <p>If I now compare with the reslice done by mrtrix ( mrresize
      img1.nii -vox 2 img2.nii) I get almost the same except it changes
      translation of the affine by 1mm. I tried to understand what exact
      shift one need to add but I can find the solution. (affter testing
      different resampling 2 3 4 mn, I do not get what is exactly done)<br>
    </p>
    <p>It may have something to do with the 'world space coordinate'
      which refer to the center of the voxel (not sure what the
      convention is). if this is the center it makes sens that on need
      to apply a shift to keep the data align. however I can not figure
      the correct shift to apply<br>
    </p>
    <p>Here is the code I use to apply zoom and translation<br>
    </p>
    <p><br>
    </p>
    <p>    img=ni.load('img1.nii')<br>
          <br>
          translations, rotations, zooms, shears
      =transforms3d.affines.decompose44(img.affine)<br>
          <br>
          newzooms=zooms*2<br>
          shift = (newzooms-zooms)/2<br>
          #shift = (newzooms)/2</p>
    <p>    #neither of the previous 2 shifts gives the one use by mrtirx<br>
    </p>
    <p>    newtranslations=translations+shift<br>
          <br>
         
newaff=transforms3d.affines.compose(newtranslations,rotations,newzooms,shears)<br>
          <br>
          ii=nip.resample_from_to(img,(np.array(img.shape)/2,newaff))</p>
    <p>    ii.to_filename('lowres.nii')</p>
    <p><br>
    </p>
    cheers<br>
    <br>
    Romain<br>
    <br>
    <div class="moz-cite-prefix">On 08/18/2017 10:52 PM, Michael Waskom
      wrote:<br>
    </div>
    <blockquote
cite="mid:CAJ0n6yhyKWrui5KfNpFOBjCL4UmJEWcSGhGwfOy49E2MO43F3A@mail.gmail.com"
      type="cite">
      <div dir="ltr">Hi Matthew/Chris/others,
        <div><br>
        </div>
        <div>Building on my previous question, I would also like to
          implement a pure-python downsampling of my images. I have
          done:</div>
        <div><br>
        </div>
        <div><font face="monospace, monospace">import nibabel as nib</font></div>
        <div><font face="monospace, monospace">from scipy import ndimage</font></div>
        <div><font face="monospace, monospace">img =
            nib.load("brain.nii")</font></div>
        <div><font face="monospace, monospace">data = img.get_data()</font></div>
        <div><font face="monospace, monospace">lowres_data =
            ndimage.zoom(data, .5)</font></div>
        <div><font face="monospace, monospace">zoom_xfm = np.eye(4)</font></div>
        <div><font face="monospace, monospace">zoom_xfm[:3, :3] /= .5</font></div>
        <div><span style="font-family:monospace,monospace">lowres_affine
            = img.affine.dot(zoom_xfm)</span><br>
        </div>
        <div><font face="monospace, monospace">lowres_img =
            nib.Nifti1Image(lowres_data, lowres_affine, img.header)</font></div>
        <div><font face="monospace, monospace">lowres_img.to_fileame("lowres_brain.nii")</font></div>
        <div><font face="monospace, monospace"><br>
          </font></div>
        <div>This is very close, but when I view the resulting image in
          Freeview (my downsampled image is also slightly stretched
          relative to the anatomical).</div>
        <div><br>
        </div>
        <div>I can also compare to the result from mri_convert, i.e. <font
            face="monospace, monospace">mri_convert brain.nii -vs 2 2 2
            lowres_brain.nii. </font>The resulting image is not
          stretched relative to the hires brain, so it's not an artifact
          on Freeview's side. The images also don't have an identical
          affine (it is off by 0.5 in the third row of the final column.</div>
        <div><br>
        </div>
        <div>What am I missing about how to alter to affine of a
          downsampled image? Alternative, is ndimage.zoom not the right
          Python function to use here?</div>
        <div><br>
        </div>
        <div>Best,</div>
        <div>Michael</div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Neuroimaging mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Neuroimaging@python.org">Neuroimaging@python.org</a>
<a class="moz-txt-link-freetext" href="https://mail.python.org/mailman/listinfo/neuroimaging">https://mail.python.org/mailman/listinfo/neuroimaging</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>