# The direction of vtkImageData make something wrong.

**URL:** https://discourse.vtk.org/t/the-direction-of-vtkimagedata-make-something-wrong/4997
**Category:** Support
**Created:** [January 13, 2021, 3:34pm UTC](https://discourse.vtk.org/t/the-direction-of-vtkimagedata-make-something-wrong/4997 "2021-01-13T15:34:05Z")
**Posts on this page:** 5
**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: [January 13, 2021, 3:34pm UTC](https://discourse.vtk.org/t/the-direction-of-vtkimagedata-make-something-wrong/4997/1 "2021-01-13T15:34:05Z")

</div>

I have a code to show a image:

```auto
from vtk.util.numpy_support import vtk_to_numpy, numpy_to_vtk
import vtk
import numpy as np

def numpyToVTK(data):
    flat_data_array = data.transpose(2,1,0).flatten()
    vtk_data_array = numpy_to_vtk(flat_data_array)
    vtk_data = numpy_to_vtk(num_array=vtk_data_array, deep=True, array_type=vtk.VTK_FLOAT)
    img = vtk.vtkImageData()
    img.GetPointData().SetScalars(vtk_data)
    img.SetDimensions(data.shape)
    return img

img = np.zeros(shape=[512,512,1])
img[0:300,0:100,0] = 255

vtkImg = numpyToVTK(img)
vtkImg.SetOrigin(100, 100, 100)

center = vtkImg.GetCenter()

imageActor = vtk.vtkImageActor()
windowLevel = vtk.vtkImageMapToWindowLevelColors()
imageActor.GetMapper().SetInputConnection(windowLevel.GetOutputPort())
windowLevel.SetInputData(vtkImg)
windowLevel.Update()

sphere = vtk.vtkSphereSource()
sphere.SetCenter(center[0], center[1], center[2])
sphere.SetRadius(50)
sphere.Update()
sphereMapper = vtk.vtkPolyDataMapper()
sphereMapper.SetInputData(sphere.GetOutput())
sphereActor = vtk.vtkActor()
sphereActor.SetMapper(sphereMapper)

ren = vtk.vtkRenderer()
ren.AddActor(imageActor)
ren.AddActor(sphereActor)
ren.SetBackground(0.1, 0.2, 0.4)

renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(400, 400)

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
renWin.Render()
iren.Start()

```

and the result is:

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

The sphere is the center of image. Then, I need to adjust the direction of `vtkImageData` as following:

```auto
...
vtkImg = numpyToVTK(img)
vtkImg.SetOrigin(100, 100, 100)
x = [0, 0, 1]
y = [0, 1, 0]
z = [1, 0, 0]
vtkImg.SetDirectionMatrix([x[0], y[0], z[0], x[1], y[1], z[1], x[2], y[2], z[2]])
center = vtkImg.GetCenter()
...

```

Then, the result is:

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

We can see the sphere is not located on the image. But the sphere is definitely the center of the image: `center=vtkImage.GetCenter()`.

What’s wrong with my code?? Or it is a bug of vtk?

---

<div class="post-metadata">

### Author: ![yi\_zhang](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yi_zhang/32/7523_2.png) [@yi\_zhang](https://discourse.vtk.org/u/yi_zhang)
#### Post date: [December 7, 2023, 12:16pm UTC](https://discourse.vtk.org/t/the-direction-of-vtkimagedata-make-something-wrong/4997/2 "2023-12-07T12:16:50Z")

</div>

hello I have the same question. if you had solved this problem, pls email me ,I want chat with you

[email:yizhang8681@gmail.com](mailto:email:yizhang8681@gmail.com)

---

<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 10, 2024, 2:41pm UTC](https://discourse.vtk.org/t/the-direction-of-vtkimagedata-make-something-wrong/4997/3 "2024-01-10T14:41:15Z")

</div>

I have solved it.

The problem is: the direction of vtkImageData does not really work on the `vtkActor`. It is a bug of vtk. Unfortunately, it has not been solve in vtk 9.2. I don’t know if it has been solve in vtk 9.3.

---

<div class="post-metadata">

### Author: ![zhou](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/3e96dc/32.png) [@zhou](https://discourse.vtk.org/u/zhou)
#### Post date: [May 12, 2024, 12:43pm UTC](https://discourse.vtk.org/t/the-direction-of-vtkimagedata-make-something-wrong/4997/4 "2024-05-12T12:43:40Z")

</div>

> [@zhang-qiang-github](#):
>
> ```auto
> 
> ```

hello, I alse have the same question. if you had solved this problem, pls email me ,I want chat with you

---

<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: [August 15, 2024, 6:52am UTC](https://discourse.vtk.org/t/the-direction-of-vtkimagedata-make-something-wrong/4997/5 "2024-08-15T06:52:43Z")

</div>

you can say the problem here.
