Convert MRtrix warps and affines to ITK format

Hello,

I have generated transformations for a set of healthy subjects to a population template using the population_template command in MRtrix with the FODs. I have 5D warps taking me from the native space of each subject to the population space (located in the -warp_dir folder I specified in the command). Here is the header of one of the warps which I’ll call Subject001_Warp.mif:

************************************************
Image:               "Subject001_Warp.mif"
************************************************
  Dimensions:        109 x 134 x 104 x 3 x 4
  Voxel size:        1.91026 x 1.89794 x 1.92643 x ? x ?
  Data strides:      [ 1 2 3 4 5 ]
  Format:            MRtrix
  Data type:         32 bit float (little endian)
  Intensity scaling: offset = 0, multiplier = 1
  Transform:                    1   6.661e-16  -6.106e-16      -102.8
                       -1.138e-15           1  -1.277e-15      -140.8
                        3.886e-16   1.582e-15           1      -92.81
  fod_reorientation: 1
  linear1:           1.0579835101175998 -0.0057100130228221468 -0.0045276824586087949   -0.16094934862215396
                     0.017963484901439064     1.0234703153454938   0.010130773301195661   -0.32280480469699424
                     0.014231631646098752   -0.01248235931505115     1.0596555352451582     1.1173797021705958
  linear2:           0.94505033498786706  0.0053211298695585848  0.0039871264984566281    0.14936778790039384
                     -0.016459538461600094    0.97686131919834795 -0.0094095522586667302     0.3232004180662027
                     -0.012886321708920215   0.011435608293369136    0.94353850852177634    -1.0526733534220045
  mrtrix_version:    0.3.15-4-g4a69f806
  nl_disp_smooth:    1
  nl_gradient_step:  0.5
  nl_lmax:           [ 4 ]
  nl_niter:          [ 5 ]
  nl_scale:          [ 1 ]
  nl_update_smooth:  2

What I would like to do is take those warps and apply them to other images in native space using another software which requires ITK type of displacement fields and ITK affine transformations. I feel like working with transformconvert and warpconvert could let me accomplish this.

Because ITK works with displacement fields (instead of deformation fields), I tried converting the MRTRIX fields into displacement fields using warpconvert.

warpconvert -force -type warpfull2displacement -from 1 -template template_fod.nii.gz Subject001_Warp.mif Subject001_displacement.nii

Does this only extract the non-linear part, or does it incorporate the affine with it? If it incorporates the affine in it, I should only need to apply this warp with the other software and not combine the affine with Subject001_displacement.nii, correct? Otherwise, I will have to either combine the affine with Subject001_displacement.nii or apply them one after another to get my images in the template space.

If I need to use the affine separately, I have to convert it to ITK format as well. (On a side note, the file Subject001.txt in the corresponding -linear_transformations_dir is not the same as in the header of the warp (see below), is this normal?)

cat Subject001.txt 
1.105446728447772919e+00 -1.158590634477877669e-02 -8.317694813222968608e-03 -4.596644981732365132e-01
5.031356841812080483e-02 1.038548748515115694e+00 2.274031364493591875e-02 -6.984325658287743321e-01
2.152625712070514383e-02 -1.464040315123524803e-02 1.109894666433130395e+00 2.625905259292831140e+00
0.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00 1.000000000000000000e+00

As far as I know, transformconvert only lets users convert from ITK to MRtrix format but not the other way around. Is there a work-around for this in MRtrix, or would anyone know a tool to do this? I imagine that if warpconvert already incorporates the affine in the warp, I shouldn’t have to bother with this step.

Thanks for any input from the MRtrix community!

Eric

Any particular reason that you do not want to use mrtransform to transform those images?

Does this only extract the non-linear part, or does it incorporate the affine with it?

The displacement field contains the contributions of the linear and the non-linear transformations. To verify this, you can retrieve the linear transformations stored in the full warp with mrinfo warp.mif -property linear1 -property linear2 and edit them using mrtransform’s -set_property option. The displacement field should change using this modified warp.

On a side note, the file Subject001.txt in the corresponding -linear_transformations_dir is not the same as in the header of the warp (see below), is this normal?

Yes, the header stores the transformation relative to the halfway space, the.txt stores the full transformation.

I imagine that if warpconvert already incorporates the affine in the warp, I shouldn’t have to bother with this step.

Yes, let us know how it goes! One thing to look out for are coordinate system flips and the stride of the warps. I remember that I had to negate the x and y coordinates when I tried to use ITK generated displacement fields.

Thanks for your quick answer.

Apologies, but I just want to make sure I understand your answer.

Does this mean that the affine transformation is already incorporated in Subject001_displacement.nii, which is the output of warpconvert -force -type warpfull2displacement -from 1 -template template_fod.nii.gz Subject001_Warp.mif Subject001_displacement.nii ? If so, I can stop at this step, right?

Or do I have to do ```mrtransform -set_property`? I don’t see that option in the help menu of the command in my verison of MRtrix.

MRtrix 3.0_RC2-65-g5d1eb1bb        mrtransform                       Oct 11 2017

If I can get this to work, I will post a solution here.

Thanks again!

Yes, the affine is incorporated, no need to add it to the displacement field.

If so, I can stop at this step, right?

Yes, if ITK uses the same coordinate system. We use scanner coordinates for all coordinates following this definition.

mrtransform -set_property

My bad, I ment mrconvert.

Cheers,
Max

Hello,

The displacement fields worked great with warpconvert.

This hint was precious! Indeed, I had to multiply the x and y elements of the displacement vector by -1 in order to get the transformation to work. Afterwards, I had to reshape the volume so that the displacement field volume had dimensions (X,Y,Z,1,3) and not (X,Y,Z,3).

Thanks for your help!

1 Like

Hello Eric and all,

Glad that I found your post in the forum. I would also like to convert the “warp_full” warp to ITK format, so that I can apply the transformation to tensor images, and do tensor reorientation .

I followed your solution and my commands are:

warpconvert -type warpfull2displacement -from 1 -template ${fodtemp} ${fod_warp_full} ${tmpdir}/fod_displacement.nii

python …/convert4Dto5D.py -i ${tmpdir}/fod_displacement.nii -o ${tmpdir}/fod_displacement_5d.nii -convert 1 -d 1

The script"…/convert4Dto5D.py" negate the x and y element of the displacement file and change the dimensions.

Then I apply “fod_displacement_5d.nii” to the tensor image using “ApplyTransformationToTensor” command from TORTOISE3.1.4. It takes ITK transformation to transform and reorient tensors.

However, after transforming, the transformed tensor image is still in the raw subjects space and the orientation is also messed up.

Is there anything I might have done wrong or is there a work-around for this in MRtrix or in any other tools you may know?

Any hint is appreciated!!

Xiaoxiao

Hi Xiaoxiao,

Sorry for the late reply. I remember I had a hard time getting the warpfield file in the correct format. I had contacted Okan Irfanoglu, the creator of DR TAMAS in TORTOISE, who gave me an executable which takes a 4D warp image and converts it to the 3D vector image that ITK requires. You can find it at this Dropbox link. This did the trick for me. I have since left the institute where I did this analysis, so I don’t have my scripts to see what I exactly did. I hope this will help you get things ready.

Please don’t hesitate to contact me if you have any questions, and I’ll try my best to remember any other details.

Thank you so much Eric! I will try it out and let you know. (why did I not think about contacting Okan Irfanoglu? Thanks!)