# How to export voxel intensity

**URL:** https://discourse.slicer.org/t/how-to-export-voxel-intensity/951
**Category:** Support
**Created:** [August 26, 2017, 2:49am UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951 "2017-08-26T02:49:12Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![happyle123456](https://avatars.discourse-cdn.com/v4/letter/h/e99b99/32.png) [@happyle123456](https://discourse.slicer.org/u/happyle123456)
#### Post date: [August 26, 2017, 2:49am UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/1 "2017-08-26T02:49:12Z")

</div>

Operating system:windows 7  
Slicer version:both slicer 4.6 and 4.7  
I made a segmentation and clip a volume with module,then how can i get the intensity value of each voxel list in a spreadsheet?the coordinate is not necessary.thanks

---

<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, 2017, 3:03am UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/2 "2017-08-26T03:03:03Z")

</div>

Probably Segment statistics module provides this information. If not, then please describe in more detail what exactly you would need.

---

<div class="post-metadata">

### Author: ![happyle123456](https://avatars.discourse-cdn.com/v4/letter/h/e99b99/32.png) [@happyle123456](https://discourse.slicer.org/u/happyle123456)
#### Post date: [August 26, 2017, 3:22am UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/3 "2017-08-26T03:22:11Z")

</div>

I need the data of every voxel which have been shown at data probe,just simply a list of voxel value,coordinate included is fine,but not the statistical parametre of them.  
maybe it would be like:  
x,y,z,value  
…  
…  
…

---

<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, 2017, 4:26am UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/4 "2017-08-26T04:26:08Z")

</div>

Probably the simplest would be to get the volume as a numpy array and then write out the voxel indices and values you are interested in. Something like this (this outputs all voxels that has value \>260):

```
import numpy as np
voxelArray = slicer.util.arrayFromVolume(getNode('MRHead'))
indices = np.where(voxelArray>260)
numberOfVoxels = len(indices[0])
for pointIndex in range(numberOfVoxels):
  i = indices[0][pointIndex]
  j = indices[1][pointIndex]
  k = indices[2][pointIndex]
  print("%d %d %d %d" % (i, j, k, voxelArray[i,j,k]))
```

See some more examples in the script repository: [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Modify\_voxels\_in\_a\_volume](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Modify_voxels_in_a_volume)

If you need physical (RAS) coordinates instead of voxel coordinates (IJK) then you have to multiply the (x,y,z,1) vector from the left with the volume’s IJKToRAS matrix.

---

<div class="post-metadata">

### Author: ![happyle123456](https://avatars.discourse-cdn.com/v4/letter/h/e99b99/32.png) [@happyle123456](https://discourse.slicer.org/u/happyle123456)
#### Post date: [August 26, 2017, 6:58am UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/5 "2017-08-26T06:58:27Z")

</div>

it’s awesome,thank you very much,it’a great help:smiley:😃

---

<div class="post-metadata">

### Author: ![CostasP](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@CostasP](https://discourse.slicer.org/u/CostasP)
#### Post date: [March 12, 2019, 3:30pm UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/6 "2019-03-12T15:30:02Z")

</div>

> [@lassoan](#):
>
> If you need physical (RAS) coordinates instead of voxel coordinates (IJK) then you have to multiply the (x,y,z,1) vector from the left with the volume’s IJKToRAS matrix.

I still try to convert ijk to LPS? Is it possible to provide an example for the vector multiplication? Thanks in advance

---

<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 13, 2019, 1:43pm UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/7 "2019-03-13T13:43:09Z")

</div>

Search for “ijktoras.multiplypoint” in [Slicer script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository) for examples.

---

<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: [April 23, 2020, 11:57pm UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/8 "2020-04-23T23:57:56Z")

</div>

A post was split to a new topic: [Get image intensity histogram of a segment](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277)

---

<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: [May 1, 2020, 8:38am UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/9 "2020-05-01T08:38:46Z")

</div>

Hi Andras,  
I used the arrayfromvolume to get the voxel intensities for an mri. but when i use the same mri to get the voxel intensities outside slicer using python and reading nrrd file I get different intensities.

Do you have any idea why its happening?

thank you

Regards,  
Saima safdar

---

<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: [May 1, 2020, 1:56pm UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/10 "2020-05-01T13:56:13Z")

</div>

NRRD file format is very simple, so it is unlikely that different values are decoded from the file. Probably you just read the value from a different location. There are two conventions for voxel indexing (IJK vs. KJI), so try reversing order of voxel coordinates. Also note that internally Slicer uses RAS physical coordinate system, while LPS coordinate system is used in files, so if you want to use physical coordinate values that are displayed in Slicer then you need to invert the first two coordinates.

---

<div class="post-metadata">

### Author: ![Michele\_Bailo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/michele_bailo/32/67234_2.png) [@Michele\_Bailo](https://discourse.slicer.org/u/Michele_Bailo)
#### Post date: [February 25, 2021, 4:39pm UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/11 "2021-02-25T16:39:20Z")

</div>

Dear all,  
how should I write the same script in order to print the result in an external txt file?

thank you very much

---

<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: [February 25, 2021, 4:54pm UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/12 "2021-02-25T16:54:27Z")

</div>

Since you get the voxels in a numpy array, you can use standard Python and numpy functions to write it to file.

An example is provided [above](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/4). Note that apart from the `slicer.util.arrayFromVolume` function to get the numpy array, everything else is plain Python and numpy.

---

<div class="post-metadata">

### Author: ![Michele\_Bailo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/michele_bailo/32/67234_2.png) [@Michele\_Bailo](https://discourse.slicer.org/u/Michele_Bailo)
#### Post date: [February 25, 2021, 6:36pm UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/13 "2021-02-25T18:36:15Z")

</div>

I tried with the following script but it only export one intensity value in the txt and not the whole list (as I would get with the print function). I don’t understand what I’m doing wrong:

import numpy as np  
voxelArray = slicer.util.arrayFromVolume(getNode(‘HImask-ADC’))  
indices = np.where(voxelArray\>0)  
numberOfVoxels = len(indices[0])  
for pointIndex in range(numberOfVoxels):  
i = indices[0][pointIndex]  
j = indices[1][pointIndex]  
k = indices[2][pointIndex]  
array\_1d = np.array([voxelArray[i,j,k]])  
np.savetxt(“array\_1d.txt”, array\_1d, delimiter="," , fmt=’%.4f’)

---

<div class="post-metadata">

### Author: ![Lisa\_Hung](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lisa_hung/32/10908_2.png) [@Lisa\_Hung](https://discourse.slicer.org/u/Lisa_Hung)
#### Post date: [May 10, 2021, 3:39pm UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/14 "2021-05-10T15:39:01Z")

</div>

Hi Andreas,

I want to use the histogram data ([Get image intensity histogram of a segment](https://discourse.slicer.org/t/get-image-intensity-histogram-of-a-segment/11277)) to create a combined histogram of each variable in Graphpad, how would you suggest doing that? I have tried using the TSV file data but I just wanted to confirm that the table created in the TSV file creates the X column to be Hounsfield units and the Y to be the number of voxels, is this correct?

---

<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: [May 10, 2021, 8:48pm UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/15 "2021-05-10T20:48:44Z")

</div>

I confirm that if the input volume voxel values are in Hounsfield units then the values along X axis of the image histogram are in Hounsfield units, too.

---

<div class="post-metadata">

### Author: ![Lisa\_Hung](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lisa_hung/32/10908_2.png) [@Lisa\_Hung](https://discourse.slicer.org/u/Lisa_Hung)
#### Post date: [May 13, 2021, 9:38am UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/16 "2021-05-13T09:38:01Z")

</div>

Hi Andreas,

Thank you for your reply. Yes the input is CT data. Could I also confirm that the Y axis is the raw number of voxels?

---

<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: [May 13, 2021, 2:07pm UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/17 "2021-05-13T14:07:57Z")

</div>

Yes, Y value is the number of voxels in that histogram bin.

---

<div class="post-metadata">

### Author: ![Nag](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/nag/32/15911_2.png) [@Nag](https://discourse.slicer.org/u/Nag)
#### Post date: [December 5, 2022, 8:04pm UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/18 "2022-12-05T20:04:06Z")

</div>

@lassoan @Michele_Bailo I am trying to extract voxel data using the above code, but I am getting this error.  
Traceback (most recent call last):  
File “”, line 2, in   
File “D:\3D sclier\Slicer 5.3.0-2022-12-03\bin\Python\slicer\util.py”, line 1657, in arrayFromVolume  
vimage = volumeNode.GetImageData()  
AttributeError: ‘MRMLCore.vtkMRMLFolderDisplayNode’ object has no attribute ‘GetImageData’.  
Please anyone can help

---

<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: [December 5, 2022, 8:21pm UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/19 "2022-12-05T20:21:56Z")

</div>

It seems that there are several nodes by the same name in the scene (the one you got was a subject hierarchy folder display node). Therefore, you either need to provide a more specific node name (you can rename the volume to give a more distinguishable name) or need to be more specific when you retrieve the node, for example by using this method:

```python
volumeNode = getFirstNodeByClassByName('vtkMRMLScalarVolumeNode', 'HImask-ADC')

```

---

<div class="post-metadata">

### Author: ![Nag](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/nag/32/15911_2.png) [@Nag](https://discourse.slicer.org/u/Nag)
#### Post date: [December 5, 2022, 8:34pm UTC](https://discourse.slicer.org/t/how-to-export-voxel-intensity/951/20 "2022-12-05T20:34:01Z")

</div>

@lassoan Thank you! I am new to python in the 3D slicer; I don’t know how to use it. Where can I learn more details about the voxel data export and the python syntax?
