# Correcting the Gantry Tilt in CT scans

**URL:** <https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526>\
**Category:** Development\
**Created:** [April 9, 2021, 3:34pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526 "2021-04-09T15:34:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanidhya26](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sanidhya26/32/3215_2.png) [@sanidhya26](https://discourse.vtk.org/u/sanidhya26)\
**Post date:** [April 9, 2021, 3:35pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/1 "2021-04-09T15:35:00Z")

</div>

Hi I am using vtkDicomReader to read the scans and then using vtkDICOMCTRectifier for gantry tilt correction. Here is my code:

rectify = vtkDICOMCTRectifier()  
rectify.SetVolumeMatrix(self.reader.GetPatientMatrix())  
rectify.SetInputConnection(self.reader.GetOutputPort())  
rectify.Update()

I am passing the output of the rectifier to vtkImageMapToColors.  
The gantry tilt is not corrected from the above code. Is there anything else I have to do?

---

<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:** [April 10, 2021, 5:17am UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/2 "2021-04-10T05:17:48Z")

</div>

It’s possibly just a misunderstanding of what vtkDICOMCTRectifier does. It actually keeps the same slice planes as the input, it just shifts the bounds of each slice plane so that boundary of the volume is rectangular:

 ![rectify1](https://discourse.vtk.org/uploads/default/original/2X/5/5b6f5187c57b02ab75a9a1145eff385cbc616dcd.png)  
So the result is a rectangular volume, and the volume is rotated rather than sheared.

An alternative way of rectifying a volume (which is _not_ what vtkDICOMCTRectifier does) is to reslice the image so that all of the planes are vertical. But this kind of oblique reslicing always causes a small amount of image degradation:

 ![rectify2](https://discourse.vtk.org/uploads/default/original/2X/a/acb571427853c8bd6b182a8afcb9fccc581a42c4.png)

Probably the easiest way to check if the rectification works is to look at a sagittal view to see if there is distortion.

---

<div class="post-metadata">

**Author:** ![sanidhya26](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sanidhya26/32/3215_2.png) [@sanidhya26](https://discourse.vtk.org/u/sanidhya26)\
**Post date:** [April 10, 2021, 7:50am UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/3 "2021-04-10T07:50:48Z")

</div>

Hi David,

Thanks for the prompt reply and a detailed explanation.  
The above code is not correcting the shear.

This is how the sagittal view looks after the rectification.

 ![sag_gantry](https://discourse.vtk.org/uploads/default/original/2X/a/a80545e76a32605ddf09824088be87e58ba80342.png)

Is it right to pass the rectifier output to the vtk rendering pipeline or should i transform/reslice the volume with the transformation matrix from rectifier?

---

<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:** [April 10, 2021, 2:18pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/4 "2021-04-10T14:18:18Z")

</div>

Yes, that’s definitely not rectified! Can you send the PatientMatrix from the reader? And even better, can you send the ImagePositionPatient and ImageOrientationPatient from at least two of the images?

The rectifier output can be sent directly to the rendering pipeline, no further transformations should be necessary. I’ve tested it on several images.

---

<div class="post-metadata">

**Author:** ![pieper](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pieper/32/17_2.png) [@pieper](https://discourse.vtk.org/u/pieper)\
**Post date:** [April 10, 2021, 2:49pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/5 "2021-04-10T14:49:41Z")

</div>

You might also try the approach used in [Slicer’s dicom importer](https://slicer.readthedocs.io/en/latest/user_guide/modules/dicom.html?highlight=tilt#panels-and-their-use) where we create a nonlinear transform to correct for gantry tilt. This preserves the original image data so you can later resample it at arbitrary resolution if you choose. This approach also handles the not uncommon issue of irregular slice spacing in CT (created by changing the table speed) or even gaps (which may be done on purpose to minimize radiation exposure or could be due to missing slices).

> <https://github.com/Slicer/Slicer/commit/3328b81211cb2e9ae16a0b49097744171c8c71c0>

---

<div class="post-metadata">

**Author:** ![sanidhya26](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sanidhya26/32/3215_2.png) [@sanidhya26](https://discourse.vtk.org/u/sanidhya26)\
**Post date:** [April 11, 2021, 7:52am UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/6 "2021-04-11T07:52:00Z")

</div>

Patient Matrix:

1 -0 0 -125  
0 -0.963631 -0.267238 137.383  
0 0.267238 -0.963631 55.521  
0 0 0 1

Slice: 100:  
Image Position Patient: -125.000-103.054\21.020  
Image Orientation Patient: 1.000000\0.000000\0.000000\0.000000\0.963630-0.267238

Slice: 189:  
Image Position Patient: -125.000-103.054\78.744  
Image Orientation Patient: 1.000000\0.000000\0.000000\0.000000\0.963630-0.267238

---

<div class="post-metadata">

**Author:** ![sanidhya26](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sanidhya26/32/3215_2.png) [@sanidhya26](https://discourse.vtk.org/u/sanidhya26)\
**Post date:** [April 11, 2021, 7:54am UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/7 "2021-04-11T07:54:33Z")

</div>

Thanks Steve. Will try this out as well.

---

<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:** [April 11, 2021, 2:53pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/8 "2021-04-11T14:53:05Z")

</div>

Thanks, those all look correct.

Are you sure that you are calling Update() on the reader before the following?

```auto
rectify.SetVolumeMatrix(self.reader.GetPatientMatrix())

```

The PatientMatrix will be identity until either Update() or UpdateInformation() is called. I’m just checking because that would be the simplest reason for it to fail.

---

<div class="post-metadata">

**Author:** ![sanidhya26](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sanidhya26/32/3215_2.png) [@sanidhya26](https://discourse.vtk.org/u/sanidhya26)\
**Post date:** [April 12, 2021, 7:40am UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/9 "2021-04-12T07:40:31Z")

</div>

yup, i am calling reader.Update()

---

<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:** [April 12, 2021, 1:26pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/10 "2021-04-12T13:26:23Z")

</div>

Thanks for the reply. If you built vtk-dicom from source, one thing you can run as a test is the “TestDICOMDisplay” example. Its source is in the Examples directory, and it is built if BUILD\_DICOM\_EXAMPLES and BUILD\_EXAMPLES are set during configuration. It requires all the dicom files as input:

```
TestDICOMDisplay /path/to/files/*.dcm

```

This example automatically applies tilt correction to any input CT files.

Edit: Also, you can try changing the interpolation used by the rectifier. When I try them locally on one of my own tilted data sets, all four methods work:

```
SetInterpolationModeToNearest()
SetInterpolationModeToLinear()
SetInterpolationModeToCubic()
SetInterpolationModeToWindowedSinc()

```

The default is WindowedSinc.

---

<div class="post-metadata">

**Author:** ![sanidhya26](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sanidhya26/32/3215_2.png) [@sanidhya26](https://discourse.vtk.org/u/sanidhya26)\
**Post date:** [April 13, 2021, 7:53am UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/11 "2021-04-13T07:53:45Z")

</div>

Hi David,

Changing the interpolation modes didnt help.

I have to build vtk from source with examples to test with “TestDICOMDisplay”. Apart from this is there anything else i can try?

---

<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:** [April 13, 2021, 12:06pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/12 "2021-04-13T12:06:43Z")

</div>

Which vtk binary package are you using? I can try using the same one to see if I can reproduce the problem.

---

<div class="post-metadata">

**Author:** ![sanidhya26](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sanidhya26/32/3215_2.png) [@sanidhya26](https://discourse.vtk.org/u/sanidhya26)\
**Post date:** [April 14, 2021, 12:53pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/13 "2021-04-14T12:53:17Z")

</div>

I built it from source with gdcm and vtk-dicom enabled. Cloned it from kitware github repo.

---

<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:** [April 14, 2021, 1:09pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/14 "2021-04-14T13:09:39Z")

</div>

In order to move forward from here, it seems the only options are for you to send me your code, so that I can test it locally, or for you to build TestDICOMDisplay, to see if it works on your data set.

---

<div class="post-metadata">

**Author:** ![sanidhya26](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sanidhya26/32/3215_2.png) [@sanidhya26](https://discourse.vtk.org/u/sanidhya26)\
**Post date:** [April 14, 2021, 1:16pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/15 "2021-04-14T13:16:51Z")

</div>

i can send you the scans, will that be enough?

---

<div class="post-metadata">

**Author:** ![sanidhya26](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sanidhya26/32/3215_2.png) [@sanidhya26](https://discourse.vtk.org/u/sanidhya26)\
**Post date:** [April 14, 2021, 1:21pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/16 "2021-04-14T13:21:02Z")

</div>

if it works for you then there’s something I am doing wrong, and then I can send you the code. is that ok?

---

<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:** [April 14, 2021, 1:53pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/17 "2021-04-14T13:53:53Z")

</div>

Yes, if you can send a link to your data and/or code vi email (david.gobbi at gmail dot com) or via this messageboard, then I can take a look.

---

<div class="post-metadata">

**Author:** ![sanidhya26](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sanidhya26/32/3215_2.png) [@sanidhya26](https://discourse.vtk.org/u/sanidhya26)\
**Post date:** [April 15, 2021, 1:35pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/18 "2021-04-15T13:35:22Z")

</div>

Hi David,

I have mailed you the scans and the code snippet.

---

<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:** [April 15, 2021, 2:23pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/19 "2021-04-15T14:23:45Z")

</div>

Thanks. I tested the image with the [TestDICOMDisplay](https://github.com/dgobbi/vtk-dicom/blob/master/Examples/TestDICOMDisplay.cxx) example, and the rectification worked properly. Here’s a screenshot, the sagittal view is in the lower right corner:  
 ![dicom41_rectified](https://discourse.vtk.org/uploads/default/original/2X/0/03abb2296061abad8328384694a91d221c2fe6ce.png)

I’m not able to run the code that you sent, because it isn’t a complete program. But the part of the code where it does the rectification looks fine. Here is the similar code from TestDICOMDisplay.cxx, for comparison:

```auto
  vtkSmartPointer<vtkDICOMCTRectifier> rect;
  if (meta->Get(DC::Modality).Matches("CT"))
  {
    rect = vtkSmartPointer<vtkDICOMCTRectifier>::New();
    rect->SetVolumeMatrix(reader->GetPatientMatrix());
    rect->SetInputConnection(portToDisplay);
    rect->Update();
    portToDisplay = rect->GetOutputPort();
  }

```

So I’m not sure what the problem is. Is it possible that your display pipeline is actually displaying the wrong image? Or maybe the display pipeline is using the PatientMatrix in a way that causes the rectification to be reversed? I notice that you never call `rectify.GetRectifiedMatrix()`. If you want to use the matrix to set the orientation of the image after rectification, it’s necessary to use the RectifiedMatrix (since the original PatientMatrix no longer applies, once the rectification has been done).

---

<div class="post-metadata">

**Author:** ![sanidhya26](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sanidhya26/32/3215_2.png) [@sanidhya26](https://discourse.vtk.org/u/sanidhya26)\
**Post date:** [April 15, 2021, 3:19pm UTC](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526/20 "2021-04-15T15:19:33Z")

</div>

I am just passing the rectifier output to vtkImageMapToColors. I am changing the camera viewup vector so that the views are displayed in correct orientation, but that should not affect the rectification right?.

[Next page](https://discourse.vtk.org/t/correcting-the-gantry-tilt-in-ct-scans/5526.md?page=2)
