How to export voxel intensity

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

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

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:

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:

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.

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

1 Like

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

Search for “ijktoras.multiplypoint” in Slicer script repository for examples.

A post was split to a new topic: Get image intensity histogram of a segment

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

Saima safdar

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.

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

thank you very much

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. Note that apart from the slicer.util.arrayFromVolume function to get the numpy array, everything else is plain Python and numpy.

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’)

Hi Andreas,

I want to use the histogram data (Get image intensity histogram of a segment) 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?

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.

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?

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

@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\”, line 1657, in arrayFromVolume
vimage = volumeNode.GetImageData()
AttributeError: ‘MRMLCore.vtkMRMLFolderDisplayNode’ object has no attribute ‘GetImageData’.
Please anyone can help

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:

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

@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?