# Get LabelMap voxel values

**URL:** https://discourse.slicer.org/t/get-labelmap-voxel-values/10337
**Category:** Development
**Created:** [February 18, 2020, 3:20pm UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337 "2020-02-18T15:20:20Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![loubna](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@loubna](https://discourse.slicer.org/u/loubna)
#### Post date: [February 18, 2020, 3:20pm UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337/1 "2020-02-18T15:20:20Z")

</div>

how can we extract points from binary volume (labelMap). I confuse between the vertices of each voxel and points. So I need extract points from labelMap.

Thank’s 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: [February 18, 2020, 3:24pm UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337/2 "2020-02-18T15:24:37Z")

</div>

You can get voxel values of a labelmap volume as a numpy array by calling [`slicer.util.arrayFromVolume`](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.arrayFromVolume).

---

<div class="post-metadata">

### Author: ![loubna](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@loubna](https://discourse.slicer.org/u/loubna)
#### Post date: [February 18, 2020, 3:28pm UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337/3 "2020-02-18T15:28:44Z")

</div>

Is there other filters to do that in vtk and C++?

---

<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 18, 2020, 3:32pm UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337/4 "2020-02-18T15:32:56Z")

</div>

In C++ you can get access to the [vtkImageData](https://vtk.org/doc/nightly/html/classvtkImageData.html) by calling volumeNode-\>GetImageData(). To map between voxel and physical space, you can use the matrix returned by GetIJKToRAS().

---

<div class="post-metadata">

### Author: ![loubna](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@loubna](https://discourse.slicer.org/u/loubna)
#### Post date: [February 18, 2020, 3:36pm UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337/5 "2020-02-18T15:36:47Z")

</div>

I am trying to get the coordinates of 3D points from vtkImageData(); For example, given a `vtkImageData` object, called `image` , I know I can get the voxel values like this:

image = vtk.vtkImageData()  
voxels = image.GetPointData().GetScalars().

but what I want is a 3D point.

---

<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 18, 2020, 3:48pm UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337/6 "2020-02-18T15:48:52Z")

</div>

You can use [GetScalarComponentAsDouble(int x, int y, int z, int component)](https://vtk.org/doc/nightly/html/classvtkImageData.html#aa89753c6bbd8af0d6c1a599e03081714) to get value of a voxel. You can get voxel coordinates from physical coordinates using the matrix returned by `volumeNode->GetIJKToRAS()`.

Note that GetScalarComponentAsDouble is extremely slow - it is mainly for testing and debugging. You can sample millions of points at once using vtkProbeFilter.

---

<div class="post-metadata">

### Author: ![loubna](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@loubna](https://discourse.slicer.org/u/loubna)
#### Post date: [February 18, 2020, 3:56pm UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337/7 "2020-02-18T15:56:41Z")

</div>

Thank you so much for the response . as the voxel has 8 vertices, I guess obtain 8 points for each voxel?

---

<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 18, 2020, 4:19pm UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337/8 "2020-02-18T16:19:57Z")

</div>

If nearest neighbor interpolation is good enough then you could use [GetScalarComponentAsDouble](https://vtk.org/doc/nightly/html/classvtkImageData.html#aa89753c6bbd8af0d6c1a599e03081714). For higher-order interpolation, you could use any of the [vtkAbstractImageInterpolator](https://vtk.org/doc/nightly/html/classvtkAbstractImageInterpolator.html) child classes. However, you should use vtkProbeFilter, which is simpler and faster, as it samples all the points that you are interested in at once.

---

<div class="post-metadata">

### Author: ![loubna](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@loubna](https://discourse.slicer.org/u/loubna)
#### Post date: [February 18, 2020, 6:42pm UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337/9 "2020-02-18T18:42:10Z")

</div>

What about this;

image=vtk.vtkImageData();

for each voxel of the volume , to recover the corresponding 3 point, we can do:

double p[3];  
int coords={i,j,k} // i, j , k are the coordinates of each vertex of the voxel  
image-\>GetPoint(image-\>ComputePointId(coords), p);

---

<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 18, 2020, 7:15pm UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337/10 "2020-02-18T19:15:07Z")

</div>

image-\>GetPoints(image-\>ComputePointId(…)) is just a more complicated version of image-\>GetScalarComponentAsDouble or image-\>GetScalarPointer and it is suitable if nearest neighbor interpolation is acceptable.

IJK coordinates are voxel coordinates, vertex coordinates are in physical coordinates, so the coordinate values you use are incorrect. I already wrote above 2x that you need to convert using IJK to RAS matrix.

---

<div class="post-metadata">

### Author: ![loubna](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@loubna](https://discourse.slicer.org/u/loubna)
#### Post date: [February 18, 2020, 7:20pm UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337/11 "2020-02-18T19:20:49Z")

</div>

Thank you very much for your help , and i know that I must convert the IJK To RAS coordinates. I developped a non-optimized marching cubes algorithm as vtk Filter. I did all computations in ijk coordinates and once the algorithm generates the list of triangles and points , I converted the output from IJK To RAS.

---

<div class="post-metadata">

### Author: ![loubna](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@loubna](https://discourse.slicer.org/u/loubna)
#### Post date: [February 19, 2020, 12:05pm UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337/12 "2020-02-19T12:05:30Z")

</div>

Hi Mr

I have just another question. A have Applied IJKTORAS transformation to get the IJK voxel coordinates in word space, but what about normal vectors which I have computed from volume data. Normals vectors need to be transformed into word space or not. If right, what must I use?

Thank’s in advance for your response

---

<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 20, 2020, 3:55am UTC](https://discourse.slicer.org/t/get-labelmap-voxel-values/10337/13 "2020-02-20T03:55:51Z")

</div>

Yes, you need to transform vectors by multiplying with the same homogeneous transformation matrix. Note that point positions must be represented as (x, y, z, 1) while vector directions as (x, y, z, 0).
