# Extract values at voxels within a segment

**URL:** <https://discourse.slicer.org/t/extract-values-at-voxels-within-a-segment/28077>\
**Category:** Support\
**Created:** [February 27, 2023, 9:24pm UTC](https://discourse.slicer.org/t/extract-values-at-voxels-within-a-segment/28077 "2023-02-27T21:24:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZSoltani](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/zsoltani/32/17166_2.png) [@ZSoltani](https://discourse.slicer.org/u/ZSoltani)\
**Post date:** [February 27, 2023, 9:24pm UTC](https://discourse.slicer.org/t/extract-values-at-voxels-within-a-segment/28077/1 "2023-02-27T21:24:04Z")

</div>

Hello everyone,

I want to extract HU values at each voxel from my CT scan files, which for I use the following command:

HU\_Array = slicer.util.array(‘myfile.nrrd’)

However, using “nrrd” file I get the values at all voxels. I was wondering if there is a way that I can extract values in a specific area after segmenting?

Thank you,  
Zahra

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [February 28, 2023, 1:22am UTC](https://discourse.slicer.org/t/extract-values-at-voxels-within-a-segment/28077/2 "2023-02-28T01:22:45Z")

</div>

Run either the maskvolume or Split Volume effect with your segmentaiton and then run your one liner.

MaskVolume will set all voxels out of segmentation are to 0 (or your value of your choosing). While SplitVolume will crop out everything outside of the segmentation area.

---

<div class="post-metadata">

**Author:** ![ZSoltani](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/zsoltani/32/17166_2.png) [@ZSoltani](https://discourse.slicer.org/u/ZSoltani)\
**Post date:** [March 15, 2023, 2:15am UTC](https://discourse.slicer.org/t/extract-values-at-voxels-within-a-segment/28077/4 "2023-03-15T02:15:25Z")

</div>

Thank you @muratmaga for your reply. Do you know how I can get " the coordinates of voxels" inside a specific segmentation, instead of the whole nrrd file?

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [March 15, 2023, 4:35am UTC](https://discourse.slicer.org/t/extract-values-at-voxels-within-a-segment/28077/5 "2023-03-15T04:35:34Z")

</div>

If you mean the physical (RAS) coordinates, I think you can rework this example:

> [@Convert between RAS and numpy array indices](https://discourse.slicer.org/t/convert-between-ras-and-numpy-array-indices/19408/2):
>
> First, download MRHead data from Sample Data module import numpy as np volumeArray = slicer.util.array("MRHead") # Get the image data in numpy array format volumeArray[60, 127, 150] = 0 # Make one voxel black, numpy array is in KJI, not IJK. volumeNode = slicer.mrmlScene.GetFirstNodeByName("MRHead") volumeNode.Modified() # I think this is needed to let Slicer know the image was modified. ijkToRasMatrix = vtk.vtkMatrix4x4() volumeNode.GetIJKToRASMatrix(ijkToRasMatrix) ijkPoint = np.array([150…

---

<div class="post-metadata">

**Author:** ![ZSoltani](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/zsoltani/32/17166_2.png) [@ZSoltani](https://discourse.slicer.org/u/ZSoltani)\
**Post date:** [March 15, 2023, 4:38pm UTC](https://discourse.slicer.org/t/extract-values-at-voxels-within-a-segment/28077/6 "2023-03-15T16:38:42Z")

</div>

Thank you @muratmaga for your help.

---

<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:** [March 15, 2023, 4:47pm UTC](https://discourse.slicer.org/t/extract-values-at-voxels-within-a-segment/28077/7 "2023-03-15T16:47:06Z")

</div>

If you want to know the coordinates of all voxels with a specific value you can work from this example: [https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html#get-the-values-of-all-voxels-for-a-label-value](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-the-values-of-all-voxels-for-a-label-value)

---

<div class="post-metadata">

**Author:** ![ZSoltani](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/zsoltani/32/17166_2.png) [@ZSoltani](https://discourse.slicer.org/u/ZSoltani)\
**Post date:** [March 15, 2023, 5:32pm UTC](https://discourse.slicer.org/t/extract-values-at-voxels-within-a-segment/28077/8 "2023-03-15T17:32:35Z")

</div>

That’s very useful, I appreciate that.
