# Slicer transformation result is different from my vtk codes

**URL:** <https://discourse.slicer.org/t/slicer-transformation-result-is-different-from-my-vtk-codes/30683>\
**Category:** Support\
**Tags:** transforms\
**Created:** [July 19, 2023, 3:53pm UTC](https://discourse.slicer.org/t/slicer-transformation-result-is-different-from-my-vtk-codes/30683 "2023-07-19T15:53:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dzzhang](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzzhang/32/66871_2.png) [@dzzhang](https://discourse.slicer.org/u/dzzhang)\
**Post date:** [July 19, 2023, 3:53pm UTC](https://discourse.slicer.org/t/slicer-transformation-result-is-different-from-my-vtk-codes/30683/1 "2023-07-19T15:53:07Z")

</div>

Recently I used the **Transform** module in Slicer to apply a linear transformation. Let’s say the translation is [10, 20, 30]. And the model moved to the correct position.

However, when I used my own vtk codes below, the result is not correct.

```auto
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
transform->Translate(10, 20, 30);
vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
transformFilter->SetInputData(mesh);
transformFilter->SetTransform(transform);
transformFilter->Update();

```

First I was thinking it is because of the coordinate system difference (RAS and LPS), so I flipped the two axes:

```auto
translationMatrix->SetElement(0, 0, -1.0); 
translationMatrix->SetElement(1, 1, -1.0);

```

However, it is still not correct. Did I miss some steps used in Slicer? Thanks for any suggestions!

---

<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:** [July 19, 2023, 7:27pm UTC](https://discourse.slicer.org/t/slicer-transformation-result-is-different-from-my-vtk-codes/30683/2 "2023-07-19T19:27:43Z")

</div>

You would actually need to change the values of the translations, not just the main diagonal. But in general you should be applying a matrix to do the transform, something like this:

> <https://github.com/Slicer/Slicer/blob/c5724d5fd588f8de267ddd696b30c18e7c6f9a8f/Modules/Scripted/DICOMPlugins/DICOMScalarVolumePlugin.py#L788-L801>

---

<div class="post-metadata">

**Author:** ![dzzhang](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzzhang/32/66871_2.png) [@dzzhang](https://discourse.slicer.org/u/dzzhang)\
**Post date:** [July 19, 2023, 9:02pm UTC](https://discourse.slicer.org/t/slicer-transformation-result-is-different-from-my-vtk-codes/30683/3 "2023-07-19T21:02:44Z")

</div>

Hi pieper, thanks for the reply!  
Can I ask what do you mean by “not just the diagonal”?

I have tried to apply the two matrix below but the error still exists:

| 1 | 0 | 0 | 10 |  
| 0 | 1 | 0 | 20 |  
| 0 | 0 | 1 | 30 |  
| 0 | 0 | 0 | 1 |

and

| -1 | 0 | 0 | 10 |  
| 0 | -1 | 0 | 20 |  
| 0 | 0 | 1 | 30 |  
| 0 | 0 | 0 | 1 |

---

<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:** [July 19, 2023, 11:30pm UTC](https://discourse.slicer.org/t/slicer-transformation-result-is-different-from-my-vtk-codes/30683/4 "2023-07-19T23:30:54Z")

</div>

This operation from your post only changes the two elements on the diagonal:

> [@dzzhang](#):
>
> ```auto
> translationMatrix->SetElement(0, 0, -1.0); 
> translationMatrix->SetElement(1, 1, -1.0);
> 
> ```

What you need to do is multiply your translation matrix by a matrix that is identity but with -1 in the 0,0 and 1,1 locations. The effect in this case will be to negate the first two translation values.

---

<div class="post-metadata">

**Author:** ![dzzhang](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzzhang/32/66871_2.png) [@dzzhang](https://discourse.slicer.org/u/dzzhang)\
**Post date:** [July 20, 2023, 12:39am UTC](https://discourse.slicer.org/t/slicer-transformation-result-is-different-from-my-vtk-codes/30683/5 "2023-07-20T00:39:56Z")

</div>

Hi pieper, I have multiplied the two matrix:

the first is [1, 0, 0, 10; 0, 1, 0, 20; 0, 0, 1, 30; 0, 0, 0, 1]  
the second is [-1, 0, 0, 0; 0, -1, 0, 0; 0, 0, 1, 0; 0, 0, 0, 1]  
the result is [-1, 0, 0, 10; 0, -1, 0, 20; 0, 0, 1, 30; 0, 0, 0, 1]

But the error still exists.

I was thinking if it is because my mesh model is not in the center of the bounding box. So I output the center coordinates of the mesh before and after the transformation. It’s weird that the coordinates after transformation are not (-10, -20, 30).

My codes below:

```auto
vtkSmartPointer<vtkMatrix4x4> translationMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
translationMatrix->SetElement(0, 3, 10); 
translationMatrix->SetElement(1, 3, 20); 
translationMatrix->SetElement(2, 3, 30); 
translationMatrix->SetElement(0, 0, -1.0); 
translationMatrix->SetElement(1, 1, -1.0); 
transform->SetMatrix(translationMatrix);
vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
transformFilter->SetInputData(mesh);

transformFilter->GetOutput()->GetCenter(center); // the center is (0, 0, 0)

transformFilter->SetTransform(transform);
transformFilter->Update();

transformFilter->GetOutput()->GetCenter(center2); // the center is not (-10, -20, 30)

```

---

<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:** [July 20, 2023, 1:14pm UTC](https://discourse.slicer.org/t/slicer-transformation-result-is-different-from-my-vtk-codes/30683/6 "2023-07-20T13:14:06Z")

</div>

There’s more information here: [Coordinate systems - Slicer Wiki](https://www.slicer.org/wiki/Coordinate_systems)

You can also search google for LPS and RAS to get more examples.

---

<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:** [July 20, 2023, 1:53pm UTC](https://discourse.slicer.org/t/slicer-transformation-result-is-different-from-my-vtk-codes/30683/7 "2023-07-20T13:53:43Z")

</div>

You may also find this script useful that shows how to [convert ITK transform file to Slicer transform](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#convert-between-itk-and-slicer-linear-transforms).

---

<div class="post-metadata">

**Author:** ![Mehran](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mehran/32/11080_2.png) [@Mehran](https://discourse.slicer.org/u/Mehran)\
**Post date:** [July 21, 2023, 9:06am UTC](https://discourse.slicer.org/t/slicer-transformation-result-is-different-from-my-vtk-codes/30683/8 "2023-07-21T09:06:04Z")

</div>

Indeed, even in python image direction/coordinates are different. I tried this code and worked. you may have a look and find out how to convert coordinates in c++:  
image=sitk.ReadImage(image)  
dirs = np.zeros([3,3])  
image.GetIJKToRASDirections(dirs)  
dirs=np.multiply(dirs,np.array([[-1,-1,-1],[-1,-1,-1],[1,1,1]]))  
new\_dirctions=np.reshape(dirs,(1,9)).tolist()  
new\_origin=label.GetOrigin()

---

<div class="post-metadata">

**Author:** ![dzzhang](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzzhang/32/66871_2.png) [@dzzhang](https://discourse.slicer.org/u/dzzhang)\
**Post date:** [July 23, 2023, 12:03am UTC](https://discourse.slicer.org/t/slicer-transformation-result-is-different-from-my-vtk-codes/30683/9 "2023-07-23T00:03:41Z")

</div>

This worked! Thank you!
