# Does vtkRenderer consider the direction of vtkImageData?

**URL:** https://discourse.vtk.org/t/does-vtkrenderer-consider-the-direction-of-vtkimagedata/5949
**Category:** Support
**Created:** [June 7, 2021, 12:48pm UTC](https://discourse.vtk.org/t/does-vtkrenderer-consider-the-direction-of-vtkimagedata/5949 "2021-06-07T12:48:52Z")
**Posts on this page:** 9
**Page:** 1

<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: [June 7, 2021, 12:48pm UTC](https://discourse.vtk.org/t/does-vtkrenderer-consider-the-direction-of-vtkimagedata/5949/1 "2021-06-07T12:48:52Z")

</div>

I want to show a image with `vtkImageActor`, and there is no other prop. I hope the image is shown in the center of screen, so I apply `ResetCamera`. However, the shown image is strange:

![image](https://discourse.vtk.org/uploads/default/original/2X/2/2aed60a0d4a2e983ff132efbf54d1fb27cd46397.png)

But the image should be:

![image](https://discourse.vtk.org/uploads/default/original/2X/7/7c52fa47625ef0960254df341f66a068862a29e5.png)

And the code to reproduce the result is:

```auto
import vtkmodules.all as vtk
import numpy as np
from BaseModule import numpyToVTK

img = np.zeros(shape=[50, 50])
img[25:, 0:25] = 64
img[0:25, 25:] = 128
img[25:, 25:] = 255
vtkImg = numpyToVTK(img)
vtkImg.SetOrigin(0, 0, 10)

dx = [0, 1, 0]
dy = [0, 0, 1]
dz = [1, 0, 0]
axialElement = [
    dx[0], dy[0], dz[0],
    dx[1], dy[1], dz[1],
    dx[2], dy[2], dz[2]
]
vtkImg.SetDirectionMatrix(axialElement)
imgActor = vtk.vtkImageActor()
imgActor.SetInputData(vtkImg)
imgActor.Update()

render = vtk.vtkRenderer()
render.AddActor(imgActor)

renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(render)
renWin.Render()
render.ResetCamera()

print('prop bounds: ', imgActor.GetBounds())
print('image bounds: ', vtkImg.GetBounds())

focalPoint = render.GetActiveCamera().GetFocalPoint()
position = render.GetActiveCamera().GetPosition()
print('active camera focal point is: ', focalPoint)
print('active camera position is: ', position)
print('image center is: ', vtkImg.GetCenter())

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
iren.Initialize()
iren.Start()

```

The printed information is:

```auto
prop bounds: (0.0, 49.0, 0.0, 49.0, 10.0, 10.0)
image bounds: (0.0, 0.0, 0.0, 49.0, 10.0, 59.0)
active camera focal point is: (24.5, 24.5, 10.0)
active camera position is: (24.5, 24.5, 143.870489570875)
image center is: (0.0, 24.5, 34.5)

```

We can see that the focal point of camera is not the center of vtkImageData.

Then, I read the source code of `ResetCamera`. It firstly compute the bounds of all props by `this->ComputeVisiblePropBounds(allBounds);`, and then set the focal point of camera as the center of bounds.

Unfortunately, the `vtkImg.GetBounds()` is not the same as `imgActor.GetBounds()`, because the `vtkImageData.GetBounds()` do not take the direction of `vtkImageData` into consideration when calculating the bounds.

Is my understanding correct? Is it a bug of vtk?

---

<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: [June 7, 2021, 5:51pm UTC](https://discourse.vtk.org/t/does-vtkrenderer-consider-the-direction-of-vtkimagedata/5949/2 "2021-06-07T17:51:32Z")

</div>

> [@zhang-qiang-github](#):
>
> Unfortunately, the `vtkImg.GetBounds()` is not the same as `imgActor.GetBounds()`, because the `vtkImageData.GetBounds()` do not take the direction of `vtkImageData` into consideration when calculating the bounds.

It looks to me that `vtkImageData.GetBounds()` takes Direction into account, but `vtkImageActor.GetBounds()` does not. That indicates a bug in `vtkImageActor`.

Interestingly, `vtkImageSlice` and `vtkImageSliceMapper`, which can be used instead of `vtkImageActor`, appear to do the Bounds correctly (at least, based on my inspection of the code).

Can you try the following code instead of `vtkImageActor`?

```python
sliceMapper = vtk.vtkImageSliceMapper()
sliceMapper.SetInputData(vtkImg) 

sliceActor = vtk.vtkImageSlice()
sliceActor.SetMapper(sliceMapper)

render.AddActor(sliceActor)
# render.AddActor(imgActor) # comment out

```

---

<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: [June 8, 2021, 12:48am UTC](https://discourse.vtk.org/t/does-vtkrenderer-consider-the-direction-of-vtkimagedata/5949/3 "2021-06-08T00:48:25Z")

</div>

Thank you for your kindly reply.

After using `vtkImageSlice`, the output information is:

```auto
prop bounds: (0.0, 0.0, 0.0, 49.0, 10.0, 59.0)
image bounds: (0.0, 0.0, 0.0, 49.0, 10.0, 59.0)
active camera focal point is: (0.0, 24.5, 34.5)
active camera position is: (0.0, 24.5, 168.370489570875)
image center is: (0.0, 24.5, 34.5)

```

It is correct.

But, the display figure is:

![image](https://discourse.vtk.org/uploads/default/original/2X/f/f7cffe85b742d7f2f247ac458dee04f0755feddf.png)

Which is still incorrect.

---

<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: [June 8, 2021, 1:08am UTC](https://discourse.vtk.org/t/does-vtkrenderer-consider-the-direction-of-vtkimagedata/5949/4 "2021-06-08T01:08:28Z")

</div>

You rotated the image, but you didn’t rotate the camera. Try `camera.Azimuth(90.0)` or `camera.Azimuth(-90.0)` to rotate the camera by 90 degrees so that the camera faces the slice.

I’ll add some clarification. By default, the VTK camera points along the _z_ direction. Calling ResetCamera() adjusts the Position and the FocalPoint, but never the direction. The direction of the camera must be changed manually.

---

<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: [June 8, 2021, 1:04pm UTC](https://discourse.vtk.org/t/does-vtkrenderer-consider-the-direction-of-vtkimagedata/5949/5 "2021-06-08T13:04:01Z")

</div>

@dgobbi I find another stranger thing.

After using `vtkImageSliceMapper` and `vtkImageSlice`, the bounds of actor is the same as that of vtkImg.

Then, I want to add `vtkImageMapToWindowLevelColors` in my pipeline as following:

```auto
vtkImg.SetDirectionMatrix(axialElement)
windowLevel = vtk.vtkImageMapToWindowLevelColors()
windowLevel.SetInputData(vtkImg)
windowLevel.Update()
sliceMapper = vtk.vtkImageSliceMapper()
# sliceMapper.SetInputData(vtkImg)
sliceMapper.SetInputConnection(windowLevel.GetOutputPort())
sliceMapper.Update()
sliceActor = vtk.vtkImageSlice()
sliceActor.SetMapper(sliceMapper)

```

Then, the output information is different:

```auto
prop bounds: (0.0, 49.0, 0.0, 49.0, 10.0, 10.0)
image bounds: (0.0, 0.0, 0.0, 49.0, 10.0, 59.0)

```

The bounds of vtkImg and actor is different.

Thus, the `vtkImageMapToWindowLevelColors` would change the bounds of mapper?

How to fix this bug?

Maybe I should using the vtkImageData which do not including direction information ~~~

---

<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: [June 8, 2021, 3:25pm UTC](https://discourse.vtk.org/t/does-vtkrenderer-consider-the-direction-of-vtkimagedata/5949/6 "2021-06-08T15:25:11Z")

</div>

I had to take some time to confirm it for myself, but you are absolutely correct that vtkImageMapToWindowLevelColors does not pass the Direction to its output. I looked through the code for vtkImageAlgorithm (which is the base class of all VTK image filters), and it completely ignores the Direction. This means that nearly all image filters will ignore the Direction.

In my own applications, my preference is to just use Spacing for the images (no Direction or Origin). Then I account for orientation and position by setting the orientation and position of the actor (i.e. the vtkImageSlice or vtkImageActor) that is displaying the data.

---

<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: [June 9, 2021, 12:38am UTC](https://discourse.vtk.org/t/does-vtkrenderer-consider-the-direction-of-vtkimagedata/5949/7 "2021-06-09T00:38:36Z")

</div>

@dgobbi Thank you for your reply. I will remove the direction information from `vtkImageData` in my project.

Hope vtk could fix this bug.

---

<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: [January 18, 2022, 12:19pm UTC](https://discourse.vtk.org/t/does-vtkrenderer-consider-the-direction-of-vtkimagedata/5949/8 "2022-01-18T12:19:01Z")

</div>

@dgobbi Sorry to bother you again. I find that this bug still is not be fixed for vtk 9.1.0.

> [@dgobbi](#):
>
> Then I account for orientation and position by setting the orientation and position of the actor (i.e. the vtkImageSlice or vtkImageActor)

Could you please tell me how to set the orientation for `vtkImageSlice` by `x=[x1, x2, x3], y=[y1, y2, y3]`. Thank you very much~~~

---

<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: [January 19, 2022, 8:45am UTC](https://discourse.vtk.org/t/does-vtkrenderer-consider-the-direction-of-vtkimagedata/5949/9 "2022-01-19T08:45:21Z")

</div>

If the image orientation is `x=[x1, x2, x3], y=[y1, y2, y3], z=[z1, z2, z3]`, the code to display the image can be:

```auto

sliceMapper = vtk.vtkImageSliceMapper()
sliceMapper.SetInputData(vtkImg)
imgActor = vtk.vtkImageSlice()
imgActor.SetMapper(sliceMapper)
imgActor.SetUserMatrix(matrix)

userMatrix = [
    x1, y1, z1, pos1,
    x2, y2, z2, pos2,
    x3, y3, z3, pos3,
    0, 0, 0, 1
]
matrix = vtk.vtkMatrix4x4()
matrix.DeepCopy(userMatrix)
imgActor.SetUserMatrix(matrix)

```
