# Deform a CT image volume using the vtkImageReslice

**URL:** https://discourse.vtk.org/t/deform-a-ct-image-volume-using-the-vtkimagereslice/4848
**Category:** Support
**Created:** [December 17, 2020, 4:19pm UTC](https://discourse.vtk.org/t/deform-a-ct-image-volume-using-the-vtkimagereslice/4848 "2020-12-17T16:19:07Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![isuruwi](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@isuruwi](https://discourse.vtk.org/u/isuruwi)
#### Post date: [December 17, 2020, 4:19pm UTC](https://discourse.vtk.org/t/deform-a-ct-image-volume-using-the-vtkimagereslice/4848/1 "2020-12-17T16:19:07Z")

</div>

I want to apply transformations such as translate, scale, rotation to a given 3D CT image volume (mhd format). For that I have used vtkImageReslice function as below. But the output seems not correctly deformed as expected.

Should I need to set resliceAxes in order to correctly deformed the image volume ? If so how ? or any mistake that I have done here ?

The image header information and the code snippet is given below for your further reference.

```
ObjectType = Image
NDims = 3
BinaryData = True
BinaryDataByteOrderMSB = False
CompressedData = False
TransformMatrix = 1 0 0 0 -1 0 0 0 1
Offset = -196.615234375 196.615234375 -250
CenterOfRotation = 0 0 0
AnatomicalOrientation = RPI
ElementSpacing = 0.76953125 0.76953125 1
DimSize = 512 512 501
ElementType = MET_FLOAT
ElementDataFile = volume-10.raw

```

The code is below.

```
vtkSmartPointer<vtkMetaImageReader> imageReader =
        vtkSmartPointer<vtkMetaImageReader>::New();
    imageReader->SetFileName(ref_imagevolume_path.c_str());
    imageReader->Update();

vtkSmartPointer<vtkTransform> transform1 =
        vtkSmartPointer<vtkTransform> ::New();
    transform1->Scale(1.2, 1.2, 1.2);

vtkSmartPointer< vtkImageReslice > reslice =
            vtkSmartPointer<vtkImageReslice >::New();
        reslice->SetInputConnection(imageReader->GetOutputPort());
        reslice->SetResliceTransform(transform1->GetInverse());
        //reslice->SetInterpolationModeToCubic();
        reslice->SetOutputDimensionality(3);
        reslice->SetInterpolationModeToNearestNeighbor();

reslice->SetOutputSpacing(
            imageReader->GetOutput()->GetSpacing()[0],
            imageReader->GetOutput()->GetSpacing()[1],
            imageReader->GetOutput()->GetSpacing()[2]);

        reslice->SetOutputOrigin(
            imageReader->GetOutput()->GetOrigin()[0],
            imageReader->GetOutput()->GetOrigin()[1],
            imageReader->GetOutput()->GetOrigin()[2]);
        reslice->SetOutputExtent(imageReader->GetOutput()->GetExtent());

        reslice->Update();

vtkSmartPointer<vtkMetaImageWriter> writer = vtkSmartPointer<vtkMetaImageWriter>::New();
        writer->SetFileName(deformed_image_volume.c_str());
        writer->SetInputData(reslice->GetOutput());
        writer->Write();
```

---

<div class="post-metadata">

### Author: ![Keyn](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@Keyn](https://discourse.vtk.org/u/Keyn)
#### Post date: [December 17, 2020, 4:51pm UTC](https://discourse.vtk.org/t/deform-a-ct-image-volume-using-the-vtkimagereslice/4848/2 "2020-12-17T16:51:26Z")

</div>

> [@isuruwi](#):
>
> ```auto
> vtkSmartPointer<vtkTransform> transform1 =
> vtkSmartPointer<vtkTransform> ::New();
> transform1->Scale(1.2, 1.2, 1.2);
> 
> ```

A vague suggestion: Could it be that you need to set the correct spacing for the output data of the reslice object according to the applied scaling in transform?

---

<div class="post-metadata">

### Author: ![isuruwi](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@isuruwi](https://discourse.vtk.org/u/isuruwi)
#### Post date: [December 17, 2020, 4:57pm UTC](https://discourse.vtk.org/t/deform-a-ct-image-volume-using-the-vtkimagereslice/4848/3 "2020-12-17T16:57:33Z")

</div>

Should I change my code as below. Or any other way ? Can you please elaborate with a simple example code if possible.

reslice-\>SetOutputSpacing(  
imageReader-\>GetOutput()-\>GetSpacing()[0] \* 1.2,  
imageReader-\>GetOutput()-\>GetSpacing()[1] \* 1.2,  
imageReader-\>GetOutput()-\>GetSpacing()[2] \* 1.2);

---

<div class="post-metadata">

### Author: ![Keyn](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/k/858c86/32.png) [@Keyn](https://discourse.vtk.org/u/Keyn)
#### Post date: [December 17, 2020, 5:17pm UTC](https://discourse.vtk.org/t/deform-a-ct-image-volume-using-the-vtkimagereslice/4848/4 "2020-12-17T17:17:11Z")

</div>

Yes, that is what I meant actually. I am not very fond with scaling volumes and images with VTK, but I can imagine that if you do so, the output parameters for the reslice object needs to fit the performed operations results.

Additionally, if this does not deliver the results you expect, maybe a translation regarding the center of the volume before scaling it is needed, like so:

```
transform1->Translate(
imageReader->GetOutput()->GetCenter[0],
imageReader->GetOutput()->GetCenter[1],
imageReader->GetOutput()->GetCenter[2]);
transform1->Scale(1.2, 1.2, 1.2);
transform1->Translate(
-(imageReader->GetOutput()->GetCenter[0]),
-(imageReader->GetOutput()->GetCenter[1]),
-(imageReader->GetOutput()->GetCenter[2]));

```

This makes sure the volume is centered before the scaling is done.  
Can you give it a try?

Also, not sure why you use

> [@isuruwi](#):
>
> `reslice->SetResliceTransform(transform1->GetInverse());`

instead of simply

```
reslice->SetRescliceTransform(transform1);

```

?

---

<div class="post-metadata">

### Author: ![isuruwi](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/c0e974/32.png) [@isuruwi](https://discourse.vtk.org/u/isuruwi)
#### Post date: [December 17, 2020, 8:03pm UTC](https://discourse.vtk.org/t/deform-a-ct-image-volume-using-the-vtkimagereslice/4848/5 "2020-12-17T20:03:24Z")

</div>

The actual tranform is not include only the scaling factor but also rotation and translation as well for example as below.

```
  vtkSmartPointer<vtkTransform> transform1 =
        vtkSmartPointer<vtkTransform> ::New();
    transform1->Translate(0.3, 0.7, 0.1);
    transform1->Scale(1.1, 0.8, 1.2);
    transform1->RotateX(5);

```

I have changed the reslice spacing and the origin by mutiply it with the scaling factor as below. Now I think the issue is gone. I assume I do not need to worry about translation and the rotation factors right ?

```
       reslice->SetOutputSpacing(
            imageReader->GetOutput()->GetSpacing()[0] * transform->GetScale()[0],
            imageReader->GetOutput()->GetSpacing()[1] * transform->GetScale()[1],
            imageReader->GetOutput()->GetSpacing()[2] * transform->GetScale()[2]);

      	reslice->SetOutputOrigin(
            imageReader->GetOutput()->GetOrigin()[0] * transform->GetScale()[0],
            imageReader->GetOutput()->GetOrigin()[1] * transform->GetScale()[1],
            imageReader->GetOutput()->GetOrigin()[2] * transform->GetScale()[2]);
        reslice->SetOutputExtent(imageReader->GetOutput()->GetExtent());
```
