# Apply transformation to nifiti image

**URL:** https://discourse.vtk.org/t/apply-transformation-to-nifiti-image/4017
**Category:** Support
**Created:** [August 19, 2020, 10:31am UTC](https://discourse.vtk.org/t/apply-transformation-to-nifiti-image/4017 "2020-08-19T10:31:45Z")
**Posts on this page:** 6
**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: [August 19, 2020, 10:31am UTC](https://discourse.vtk.org/t/apply-transformation-to-nifiti-image/4017/1 "2020-08-19T10:31:45Z")

</div>

I want to apply transformations to a given nifti image. How can I apply such as translate, scale, rotation to that? See below example for further reference.

// read nifti image  
vtkSmartPointer niftiReader = vtkSmartPointer::New();  
niftiReader-\>SetFileName(ref\_volumetric\_mesh\_voxelized\_path.c\_str());  
niftiReader-\>Update();

//define transformation  
vtkSmartPointer transform = vtkSmartPointer::New();  
transform-\>Translate(0.6, 0.3, 0.8);  
transform-\>Scale(1.2, 1.0, 1.3);  
transform-\>RotateY(2);

And now I want to apply above transformation to the 3D image ? Can you please elaborate with an example please ?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [August 19, 2020, 1:55pm UTC](https://discourse.vtk.org/t/apply-transformation-to-nifiti-image/4017/2 "2020-08-19T13:55:40Z")

</div>

You can use vtkImageReslice filter for resampling a transformed image to a different geometry.

---

<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: [August 19, 2020, 4:05pm UTC](https://discourse.vtk.org/t/apply-transformation-to-nifiti-image/4017/3 "2020-08-19T16:05:41Z")

</div>

I just applied the vtkImageReslice filter as you can see below and then pass the output nifti image through the DRR algorithm. I have noticed that the output DRR image doesn’t scale-up the ellipsoid (see the attached images) even I passing the transformations as **transform-\>Scale(1.7, 1.8, 1.8)**.

Did I perform something wrong when applying the vtkImageReslice filter or is this the normal behaviour since the scaling part applies to every voxel in the 3D image ?

```
vtkSmartPointer<vtkNIFTIImageReader> niftiReader =
    vtkSmartPointer<vtkNIFTIImageReader>::New();
niftiReader->SetFileName(ref_volumetric_mesh_voxelized_path.c_str());
niftiReader->Update();

vtkSmartPointer<vtkUnstructuredGridReader> unstructuredGridReader =
    vtkSmartPointer<vtkUnstructuredGridReader>::New();
unstructuredGridReader->SetFileName(ref_volumetric_mesh_path.c_str());
unstructuredGridReader->Update();

// Rotate about the center of the image
vtkSmartPointer<vtkTransform> transform =
    vtkSmartPointer<vtkTransform>::New();

transform->Scale(1.7, 1.8, 1.8);

vtkSmartPointer<vtkTransformFilter> transformFilterForUSG =
    vtkSmartPointer<vtkTransformFilter>::New();

transformFilterForUSG->SetInputConnection(unstructuredGridReader->GetOutputPort());
transformFilterForUSG->SetTransform(transform);

//vtkSmartPointer<vtkPointSet> pd = transformFilter->GetOutput();
vtkSmartPointer<vtkUnstructuredGridWriter> unstructuredGridWriter =
    vtkSmartPointer<vtkUnstructuredGridWriter>::New();

unstructuredGridWriter->SetFileName(deformed_volumetric_mesh_path.c_str());
unstructuredGridWriter->SetInputConnection(transformFilterForUSG->GetOutputPort());
unstructuredGridWriter->Write();

// Reslice does all of the work
vtkSmartPointer<vtkImageReslice> reslice =
    vtkSmartPointer<vtkImageReslice>::New();
reslice->SetInputConnection(niftiReader->GetOutputPort());
reslice->SetResliceTransform(transform);
reslice->SetInterpolationModeToCubic();
reslice->SetOutputSpacing(
    niftiReader->GetOutput()->GetSpacing()[0],
    niftiReader->GetOutput()->GetSpacing()[1],
    niftiReader->GetOutput()->GetSpacing()[2]);
reslice->SetOutputOrigin(
    niftiReader->GetOutput()->GetOrigin()[0],
    niftiReader->GetOutput()->GetOrigin()[1],
    niftiReader->GetOutput()->GetOrigin()[2]);
reslice->SetOutputExtent(niftiReader->GetOutput()->GetExtent());
reslice->Update();

```

This is the original nifiti image before applying the transformation.

 ![ref_volumetric_ellipsoid_old](https://discourse.vtk.org/uploads/default/original/2X/9/90ef64cd365230bc3bbbb7c2e542238c1fc6f728.png)

This DRR image produced after applying the transformation

 ![deformed_volumetric_ellipsoid](https://discourse.vtk.org/uploads/default/original/2X/9/958a7a6556bc7d6c59768718b29363a49faa97d3.png) !

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [August 19, 2020, 9:12pm UTC](https://discourse.vtk.org/t/apply-transformation-to-nifiti-image/4017/4 "2020-08-19T21:12:04Z")

</div>

You don’t need a transform filter, but set the transform as input to the image reslice filter.

---

<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: [August 22, 2020, 5:21pm UTC](https://discourse.vtk.org/t/apply-transformation-to-nifiti-image/4017/5 "2020-08-22T17:21:08Z")

</div>

I have applied vtkImageReslice filter to transform the given nifti image.  
I have produced deformed versions, but for some images I was unable to get the whole ellipsoid and only appear some part of the ellipsoid (see attached sample images)?

I’m writing this to ask about the SetResliceAxes functionality. Should I use this here in order to overcome this issue? if so how ?

// Reslice does all of the work  
vtkSmartPointer reslice =  
vtkSmartPointer::New();  
reslice-\>SetInputConnection(niftiReader-\>GetOutputPort());  
reslice-\>SetResliceTransform(transform);  
reslice-\>SetInterpolationModeToCubic();  
reslice-\>SetOutputSpacing(  
niftiReader-\>GetOutput()-\>GetSpacing()[0],  
niftiReader-\>GetOutput()-\>GetSpacing()[1],  
niftiReader-\>GetOutput()-\>GetSpacing()[2]);  
reslice-\>SetOutputOrigin(  
niftiReader-\>GetOutput()-\>GetOrigin()[0],  
niftiReader-\>GetOutput()-\>GetOrigin()[1],  
niftiReader-\>GetOutput()-\>GetOrigin()[2]);  
reslice-\>SetOutputExtent(niftiReader-\>GetOutput()-\>GetExtent());  
reslice-\>Update();

 ![ellipsoid_3](https://discourse.vtk.org/uploads/default/original/2X/8/80fd6bd8366680711ff05c93859a46382f54cd12.png)

 ![ellipsoid_4](https://discourse.vtk.org/uploads/default/original/2X/7/79b9ddcc4bbf5d3013bed18c32e729c1e7a57667.png)

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [August 22, 2020, 7:02pm UTC](https://discourse.vtk.org/t/apply-transformation-to-nifiti-image/4017/6 "2020-08-22T19:02:47Z")

</div>

If you don’t want the ellipsoid to be cropped then make the output extent larger.
