# Access the 3x3 diffusion tensor matrix of dti images

**URL:** <https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185>\
**Category:** Support\
**Created:** [August 26, 2019, 8:16pm UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185 "2019-08-26T20:16:06Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![mshah](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@mshah](https://discourse.slicer.org/u/mshah)\
**Post date:** [August 26, 2019, 8:16pm UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/1 "2019-08-26T20:16:06Z")

</div>

Hi all,

I have a set of patients’ dti images. How do I extract the 3x3 diffusion tensor matrices per each voxel? Additionally how do I convert the DTI image to nrrd format, can I still use the DWIconvert module even though its not a DWI image?

Thank you

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 26, 2019, 8:34pm UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/2 "2019-08-26T20:34:12Z")

</div>

> [@mshah](#):
>
> How do I extract the 3x3 diffusion tensor matrices per each voxel?

There are a couple ways, via vtk and numpy:

[https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python\_scripting#Accessing\_Volume\_data\_as\_numpy\_array](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#Accessing_Volume_data_as_numpy_array)

[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Access\_values\_in\_a\_DTI\_tensor\_volume](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Access_values_in_a_DTI_tensor_volume)

> [@mshah](#):
>
> Additionally how do I convert the DTI image to nrrd format,

If you have the data loaded in Slicer as a diffusion tensor, then saving in nrrd format should just work.

---

<div class="post-metadata">

**Author:** ![mshah](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@mshah](https://discourse.slicer.org/u/mshah)\
**Post date:** [August 26, 2019, 8:54pm UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/3 "2019-08-26T20:54:25Z")

</div>

Hi,  
Thanks for the response, how do these methods to get the diffusion tensors translate to the slice depth. Per patient I have around 26 images, will the methods give me the tensors for a specified slice depth or for all of them? Additionally, when you run the code given in the links in the interactive python window, where does the information containing the diffusion tensors go? What format is it saved in?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 26, 2019, 9:14pm UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/4 "2019-08-26T21:14:36Z")

</div>

Hi -

> [@mshah](#):
>
> how do these methods to get the diffusion tensors translate to the slice depth. Per patient I have around 26 images, will the methods give me the tensors for a specified slice depth or for all of them?

You can get all the tensors. The mapping between slices, rows, and columns (IJK) and tensor samples is by the 3D array where each element is a 3x3 tensor. The mapping to patient space is defined by the [IJKToRAS transform](https://www.slicer.org/wiki/Coordinate_systems) (and any other applied transforms). The tensors themselves are [measured with respect to the `Measurement Frame`](https://na-mic.org/wiki/NAMIC_Wiki:DTI:Nrrd_format), a 3x3 matrix you can get from the node.

> [@mshah](#):
>
> Additionally, when you run the code given in the links in the interactive python window, where does the information containing the diffusion tensors go? What format is it saved in?

The values are stored in python variables so you can use them to perform further calculations or save them out to a file if you want.

> [@mshah](#):
>
> Thanks for the response,

You’re very welcome. Feel free to ask follow ups if needed.

---

<div class="post-metadata">

**Author:** ![mshah](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@mshah](https://discourse.slicer.org/u/mshah)\
**Post date:** [August 26, 2019, 9:30pm UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/5 "2019-08-26T21:30:14Z")

</div>

Hi,  
So if they save as variables in python would I be able to access them through a jupyter notebook after they saved? How would I save them out as a file (sorry I’m new to this). Additionally, in the links you posted contain nested forloops, how does this work in the python window (since isnt the window just a line by line execution?)

Additionally, if I create a brain mask of each image and then extract the tensors, will this prevent me from getting the skull tensor data (which I dont need).

How can I create brain masks of all my patients and save them to the program so that whenever I come back to continue work I only work on the updated imaged? Is there a simple way I can save this, for example the format given in the DCM window that separates the data per patient and series?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 26, 2019, 10:13pm UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/6 "2019-08-26T22:13:58Z")

</div>

You can have a look at some of the [Slicer python tutorials](https://www.slicer.org/wiki/Documentation/4.10/Training#Tutorials_for_software_developers) to get an idea of generally how python variables work to represent, save, and restore data.

Yes, you can do loops in the python interactor in Slicer. Accessing a volume that way is shown as an example but usually not efficient.

The numpy array approach is usually better. E.g. if you’ve downloaded `DTIBrain` from the SampleData module, then you can do this:

```auto
>>> a = array('DTIBrain')
>>> a.shape
(85, 144, 144, 3, 3)

```

If you have a mask [like from SlicerDMRI](http://dmri.slicer.org/) you can also access it as a numpy array.

---

<div class="post-metadata">

**Author:** ![mshah](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@mshah](https://discourse.slicer.org/u/mshah)\
**Post date:** [August 26, 2019, 11:08pm UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/7 "2019-08-26T23:08:19Z")

</div>

Again, thank you for the response!  
The tutorials helped a lot, but I still couldnt figure out how to save the diffusion tensors as a file. Specifically, I am trying to export all the diffusion tensors to matlab and work with them there. Is there a way to do this?

---

<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:** [August 26, 2019, 11:23pm UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/8 "2019-08-26T23:23:50Z")

</div>

`slicer.util.saveNode` should work for diffusion images. You can load the generated .nrrd file into Matlab.

There are great Python packages for all kinds of data processing, including diffusion images. Why don’t you process the data directly in Python?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [August 26, 2019, 11:34pm UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/9 "2019-08-26T23:34:09Z")

</div>

I agree with @lassoan, you should try to stick with Python if you can.

But if you need to work with matlab, you [this might be another option](https://docs.scipy.org/doc/scipy/reference/tutorial/io.html).

---

<div class="post-metadata">

**Author:** ![mshah](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@mshah](https://discourse.slicer.org/u/mshah)\
**Post date:** [August 28, 2019, 5:53am UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/10 "2019-08-28T05:53:13Z")

</div>

Hi,

Thank you again for the replies. I am trying to use matlab to compare to another analysis done in that software. I have been trying save the image data however I keep getting this error. What am I doing wrong? Additionally, if I wanted to save as a .mat file how would i do so since scipy isnt built into slicer? Please let, I am really just trying to extract this data so I can work with it easier in Matlab.  
 ![13%20PM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/4/64904c17ceff8743d7572669a6b051b5d80f8006.png)

---

<div class="post-metadata">

**Author:** ![ljod](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ljod/32/652_2.png) [@ljod](https://discourse.slicer.org/u/ljod)\
**Post date:** [August 28, 2019, 7:20am UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/11 "2019-08-28T07:20:00Z")

</div>

Hi! I’m not sure what your goal is in the code? The code loops through the tensors but doesn’t do anything with the data. The last error is because the save function expects a node, but the code gives it an image data.

---

<div class="post-metadata">

**Author:** ![mshah](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@mshah](https://discourse.slicer.org/u/mshah)\
**Post date:** [August 28, 2019, 7:51am UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/12 "2019-08-28T07:51:39Z")

</div>

> [@ljod](#):
>
> he code? The code loops through the tensors but doesn’t do anything with the data. The last error is because the save function expects a node, but the code gives it an image data.

hi i am trying to extract the 3x3 diffusion tensors per voxel and save as a .mat file

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [October 30, 2020, 2:56am UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/13 "2020-10-30T02:56:54Z")

</div>

Hi Andras,  
How can I iterate or see values inside the tensor variable which is a vtkfloatarray. is there a way to convert to numpy and then iterate over the array.

Thank you

Regards,  
Saima Safdar

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [October 30, 2020, 6:27am UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/14 "2020-10-30T06:27:12Z")

</div>

Hi Andras,  
I have another question. How can I get position values of all the voxels greater than 0 from a volume image.

Thank you

Regards,  
Saima Safdar

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [January 21, 2021, 8:20am UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/15 "2021-01-21T08:20:30Z")

</div>

Hi,  
I need to get the tensor values from DTI image. I got the values using the following code but all the values are same. I think the values should be different for each voxel. Could you please tell me if there is something wrong with the following code:

```python
    tensors = slicer.util.array(inputVolume.GetID())
    volumeNode=slicer.util.getNode(inputVolume.GetID())
    imageData=volumeNode.GetImageData()
    tensors = imageData.GetPointData().GetTensors()
    extent = imageData.GetExtent()
    idx = 0
    for k in range(extent[4], extent[5]+1):
      for j in range(extent[2], extent[3]+1):
        for i in range(extent[0], extent[1]+1):
          tensors.GetTuple9(idx)
          idx += 1
    import json
    f = open('/home/saima/Downloads/Damon/Damon/Case131/tensors.txt', 'w')
    for x in range(5300):
        print(tensors.GetTuple(x))
        f.write(json.dumps(tensors.GetTuple(x)))

```

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [January 21, 2021, 11:18pm UTC](https://discourse.slicer.org/t/access-the-3x3-diffusion-tensor-matrix-of-dti-images/8185/16 "2021-01-21T23:18:02Z")

</div>

> [@Saima](#):
>
> ` tensors = slicer.util.array(inputVolume.GetID())`

This was the right way to get the tensors as a numpy array. You can just index it to get the tensor values. You don’t need to use the VTK api.
