# Deforming with NiftyReg is different than deforming in Slicer

**URL:** https://discourse.slicer.org/t/deforming-with-niftyreg-is-different-than-deforming-in-slicer/34992
**Category:** Support
**Tags:** registration, deformation
**Created:** [March 20, 2024, 4:02pm UTC](https://discourse.slicer.org/t/deforming-with-niftyreg-is-different-than-deforming-in-slicer/34992 "2024-03-20T16:02:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![koeglfryderyk](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/koeglfryderyk/32/14638_2.png) [@koeglfryderyk](https://discourse.slicer.org/u/koeglfryderyk)
#### Post date: [March 20, 2024, 4:02pm UTC](https://discourse.slicer.org/t/deforming-with-niftyreg-is-different-than-deforming-in-slicer/34992/1 "2024-03-20T16:02:33Z")

</div>

I’m integrating [NiftyReg](http://cmictig.cs.ucl.ac.uk/wiki/index.php/NiftyReg_documentation) into an extension. (one of the goals is to get the computed transformation/deformation into Slicer)

I registered two images with [reg\_f3d](http://cmictig.cs.ucl.ac.uk/wiki/index.php/Reg_f3d) and saved the control point grid (to later get the deformation).

```auto
reg_f3d -ref fixed.nii -flo moving.nii -res warped.nii -cpp controlPointGrid.nii

```

I then used [reg\_transform](http://cmictig.cs.ucl.ac.uk/wiki/index.php/Reg_transform) to get the displacement field as a .nii

```auto
reg_transform -ref fixed.nii -disp controlPointGrid.nii displacement.nii

```

I then loaded the displacement.nii into Slicer as a Transform. I got the following warning, but I’m not sure if it is relevant:

> Warning: Loading /Desktop/volumes/niftyreg/displacement.nii - NIFTI file may not contain valid displacement field, the transform may be incorrect. Intent code is expected to be ‘1006’ (displacement vector), but the file contained: ‘1007’. Filename: ‘/Desktop/volumes/niftyreg/displacement.nii’.

Then I applied the transformation in Slicer to the moving image, but the result was very different (‘clearly wrong’) from the moving image transformed by NiftyReg.  
First screenshot is a 50/50 overlay of the NiftyReg warped image (purple) over the fixed image (green)  
First screenshot is a 50/50 overlay of the Slicer transformed image (purple) over the fixed image (green)

 ![Screenshot from 2024-03-20 16-58-02](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/a/aa987aa9b0413b6e73cd877af3dfc560aa8b4e68.jpeg)  
 ![Screenshot from 2024-03-20 16-58-43](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/1/61ce865427002982e47ac135f1df173847dfe250.jpeg)

I’m not sure if the displacement field is somehow encoded differently/incorrectly or if I’m missing some steps in between. (the author of NiftyReg mentioned that “Note that the transformation are encoded from the reference to the floating space and all is stored in physical space (mm) rather than voxel space.” - not sure if this is relevant here)

Here’s a [link to all the volumes](https://www.dropbox.com/scl/fo/wi3u01njtyzokhzehi7jg/h?rlkey=0obw9hopx99jtefbyaphn09nl&dl=0)

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [March 20, 2024, 10:58pm UTC](https://discourse.slicer.org/t/deforming-with-niftyreg-is-different-than-deforming-in-slicer/34992/2 "2024-03-20T22:58:59Z")

</div>

Due to using a wrong intent code, the ITK reader does not know that the vectors store spatial displacement and therefore the vectors are not converted to the correct coordinate system.

> <https://github.com/InsightSoftwareConsortium/ITK/commit/d007ee8e78ab367d10a4fc116fceb4ba07604174>
>
> NIFTI image containing displacement field (intent = 1006) was incorrectly read a…s scalar volume (all 3 vector components were set to the same value).
> 
> Fixed by reading NIFTI displacement vector files similarly to general-purpose vector images. For displacement vector files,
> vector components are converted between NIFTI file's RAS coordinate system and ITK's LPS coordinate system,
> unless this conversion is explicitly disabled by setting ConvertRASDisplacementVectors to false.
> 
> An ITK vector image can be written as NIFTI displacement vector file by setting "intent\_code"="1006" in the metadata dictionary of the image.

VoxelMorph had a [similar issue](https://github.com/voxelmorph/voxelmorph/issues/438). If niftyreg developers cannot fix the problem quickly then you can patch the nifti file similarly to how it is described in the voxelmorph issue.

---

<div class="post-metadata">

### Author: ![koeglfryderyk](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/koeglfryderyk/32/14638_2.png) [@koeglfryderyk](https://discourse.slicer.org/u/koeglfryderyk)
#### Post date: [March 21, 2024, 7:57am UTC](https://discourse.slicer.org/t/deforming-with-niftyreg-is-different-than-deforming-in-slicer/34992/3 "2024-03-21T07:57:36Z")

</div>

The problem was a bit simpler - the displacement field from NiftiReg already had the correct shape, I only had to change the intent code

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [March 22, 2024, 1:23am UTC](https://discourse.slicer.org/t/deforming-with-niftyreg-is-different-than-deforming-in-slicer/34992/4 "2024-03-22T01:23:37Z")

</div>

> [@koeglfryderyk](#):
>
> I only had to change the intent code

Great! Please report the issue to NiftyReg maintainers and copy the link to the issue here for future reference. Thank you!
