# Mapping between coordinate position (x,y,z) and voxel index

**URL:** https://discourse.vtk.org/t/mapping-between-coordinate-position-x-y-z-and-voxel-index/5357
**Category:** Support
**Created:** [March 10, 2021, 11:36am UTC](https://discourse.vtk.org/t/mapping-between-coordinate-position-x-y-z-and-voxel-index/5357 "2021-03-10T11:36:29Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [March 10, 2021, 11:36am UTC](https://discourse.vtk.org/t/mapping-between-coordinate-position-x-y-z-and-voxel-index/5357/1 "2021-03-10T11:36:29Z")

</div>

Dear All,

Having a (x,y,z) got with picking with a displayed mhd file, exists any VTK instruction to convert to the voxel index (other (x,y,z), preferable in float) in the image in memory?

Notice that the camera was changed with SetFocalPoint and SetPosition.

In Python.

Thanks,

Luís Gonçalves

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 11, 2021, 1:16pm UTC](https://discourse.vtk.org/t/mapping-between-coordinate-position-x-y-z-and-voxel-index/5357/2 "2021-03-11T13:16:53Z")

</div>

Hello, Luis,

You can use `vtkCellPicker` to perform the picking. This class has a method called `GetCellId()` which returns the run-length cell (or voxel) index. If you need the grid address (I, J, K), you can do:

```cpp
    uint nI = /* grid's number of columns */
    uint nJ = /* grid's number of rows */
	uint val = index_value;
	uint nynx = nJ * nI;
	uint k = val / nynx;
	val -= (k*nynx);
	uint j = val / nI;
	uint i = val % nI;
    std::cout << "grid address: " << i << ", " << j << ", " << k << std::endl; 

```

cheers,

Paulo

---

<div class="post-metadata">

### Author: ![Yohann\_Bearzi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yohann_bearzi/32/671_2.png) [@Yohann\_Bearzi](https://discourse.vtk.org/u/Yohann_Bearzi)
#### Post date: [March 11, 2021, 5:10pm UTC](https://discourse.vtk.org/t/mapping-between-coordinate-position-x-y-z-and-voxel-index/5357/3 "2021-03-11T17:10:31Z")

</div>

You can also use the static method `vtkStructuredData::ComputeCellIdForExtent`. You feed in the extent (which looks like `[xmin, xmax, ymin, ymax, zmin, zmax]`) and the cell position in the grid. A similar method exists for points : `vtkStructuredData::ComputePointIdForExtent`.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [March 11, 2021, 5:45pm UTC](https://discourse.vtk.org/t/mapping-between-coordinate-position-x-y-z-and-voxel-index/5357/4 "2021-03-11T17:45:42Z")

</div>

I think the method that the OP is looking for is the following, which can also be used from Python:

```auto
vtkImageData::TransformPhysicalPointToContinuousIndex(const double xyz[3], double ijk[3])

```

There are three coordinate systems in play:

1. the VTK world coordinates, which are returned by `picker.GetPickPosition()`
2. the VTK data coordinates, which are returned by `picker.GetMapperPosition()`
3. the image data structured point coordinates, i.e. the voxel indices

Not all pickers have a `GetMapperPosition()` method. Which picker are you using?

If you can use `GetMapperPosition()`, then you can compute the voxel index like this:

```python
xyz = picker.GetMapperPosition()
XYZ = [0.0, 0.0, 0.0] # to store voxel index
image.TransformPhysicalPointToContinuousIndex(xyz, XYZ)
print(XYZ)

```

If you cannot use `GetMapperPosition()`, you must use the inverse of the actor’s matrix to convert the result of `GetPickPosition()` from the world coordinate system to the actor’s local data coordinate system.

The picker will automatically take all camera parameters into account, so you never have to worry about the camera when you use `picker.GetPickPosition()` or `picker.GetMapperPosition()`.

---

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [March 13, 2021, 9:36pm UTC](https://discourse.vtk.org/t/mapping-between-coordinate-position-x-y-z-and-voxel-index/5357/5 "2021-03-13T21:36:37Z")

</div>

Sorry for the late answer. I picked the following way.

```
        picker = vtk.vtkVolumePicker()
        picker.SetTolerance(1e-6)
        picker.SetVolumeOpacityIsovalue(0.1)
        coord=renderWindow.GetSize()
        x=event["x"]*coord[0]
        y=event["y"]*coord[1]
        if (picker.Pick(x, y, 0, renderer)==0):
            self.test=self.test+1
        self.p=picker.GetPickPosition()
        self.n=picker.GetPickNormal()

```

I already have the picked points and the display of the volume. How to:

" you must use the inverse of the actor’s matrix to convert the result of `GetPickPosition()` from the world coordinate system to the actor’s local data coordinate system."  
Before the pick I do the following transform to the volume:´

```
transform = vtk.vtkTransform()
transform.RotateWXYZ(-20,0.0,-0.7,-0.7)

```

Thanks for any help,

Luís Gonçalves

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [March 14, 2021, 3:16am UTC](https://discourse.vtk.org/t/mapping-between-coordinate-position-x-y-z-and-voxel-index/5357/6 "2021-03-14T03:16:28Z")

</div>

With vtkVolumePicker, you can get the pick in the actor’s local coordinates (i.e. mapper coordinates) with these methods:

```python
picker.GetMapperPosition()
picker.GetMapperNormal()

```

Or, if you have the transform that you applied to the volume, you can apply the inverse to the pick position:

```python
point = transform.GetInverse().TransformPoint(point)
normal = transform.GetInverse().TransformNormal(normal)

```

If you don’t have the transform, then you can create it from the prop matrix of the vtkVolume

```python
transform = vtk.vtkTransform()
transform.Concatenate(volumeActor.GetMatrix())

```

---

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [March 14, 2021, 9:31pm UTC](https://discourse.vtk.org/t/mapping-between-coordinate-position-x-y-z-and-voxel-index/5357/7 "2021-03-14T21:31:30Z")

</div>

Thanks. It worked.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [May 31, 2021, 3:37pm UTC](https://discourse.vtk.org/t/mapping-between-coordinate-position-x-y-z-and-voxel-index/5357/8 "2021-05-31T15:37:15Z")

</div>

@dgobbi Currently, I meet a problem about the world point, and this is the [detail](https://discourse.vtk.org/t/the-world-coordinate-is-changed-when-the-shown-image-is-rotated/5879/5). Could you please have a look at this question? This question really make me crazy. Any suggestion is appreciated!
