# Gantry tilt correction for list of DICOM images

**URL:** https://discourse.itk.org/t/gantry-tilt-correction-for-list-of-dicom-images/5907
**Category:** Beginner Questions
**Tags:** python, dicom, simpleitk
**Created:** [April 29, 2023, 5:26pm UTC](https://discourse.itk.org/t/gantry-tilt-correction-for-list-of-dicom-images/5907 "2023-04-29T17:26:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![EthanHarvey98](https://discourse.itk.org/user_avatar/discourse.itk.org/ethanharvey98/32/3439_2.png) [@EthanHarvey98](https://discourse.itk.org/u/EthanHarvey98)
#### Post date: [April 29, 2023, 5:26pm UTC](https://discourse.itk.org/t/gantry-tilt-correction-for-list-of-dicom-images/5907/1 "2023-04-29T17:26:41Z")

</div>

Hello,

I am working on writing my own gantry tilt correction function in Python. The function takes in a list of correctly ordered DICOM images loaded by `pydicom`, corrects each slice, and returns a corrected 3D image.

```python
def correct_gantry_tilt(images):
    
    corrected_images = np.zeros((len(images), images[0].Rows, images[0].Columns))
    
    for image_index, image in enumerate(images):
        
        gantry_tilt = image.GantryDetectorTilt
        slice_location = image.SliceLocation
        inter_slice_distance = image.SliceThickness * np.cos(np.radians(gantry_tilt))
        
        image = sitk.GetImageFromArray(image.pixel_array[:,:,np.newaxis])
        transform = sitk.AffineTransform(3)
        transform.SetCenter(image.TransformContinuousIndexToPhysicalPoint(np.array(image.GetSize())/2))
        transform.SetTranslation((0, 0, (slice_location-images[0].SliceLocation)/inter_slice_distance))
        transform.Shear(0, 2, np.radians(gantry_tilt))
        output_shape = (512, 512, 512)
        resampled_image = sitk.Resample(image, output_shape, transform, sitk.sitkLinear, [-output_shape[0]//2,0,0], image.GetSpacing(), image.GetDirection())
        resampled_image = sitk.GetArrayFromImage(resampled_image)
        
        corrected_images[image_index,:,:] = np.amax(resampled_image, axis=2)
    
    return corrected_images

```

The current implementation does not seem to get the tilt angle correct and is slow (this is because of the large `output_shape` that is being reduced with `np.amax()`). Does anyone have any insight into how to efficiently correct gantry tilt of a single DICOM image?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [May 1, 2023, 7:10pm UTC](https://discourse.itk.org/t/gantry-tilt-correction-for-list-of-dicom-images/5907/2 "2023-05-01T19:10:19Z")

</div>

One option is to read the image with non-orthogonal direction matrix by using `ImageSeriesReader` with [`SetForceOrthogonalDirection(True)`](https://itk.org/Doxygen/html/classitk_1_1ImageSeriesReader.html#a0010020aa79587ec8d92a9332d36832b), and the resampling it onto orthogonal grid. I don’t know whether it would be noticeably faster than resampling slices one by one.

---

<div class="post-metadata">

### Author: ![EthanHarvey98](https://discourse.itk.org/user_avatar/discourse.itk.org/ethanharvey98/32/3439_2.png) [@EthanHarvey98](https://discourse.itk.org/u/EthanHarvey98)
#### Post date: [May 1, 2023, 7:38pm UTC](https://discourse.itk.org/t/gantry-tilt-correction-for-list-of-dicom-images/5907/3 "2023-05-01T19:38:56Z")

</div>

Thank you for the response, @dzenanz.

Is that a method in the python library? The following doesn’t seem to work for me.

```auto
      1 reader = sitk.ImageSeriesReader()
----> 2 reader.SetForceOrthogonalDirection(True)

AttributeError: 'ImageSeriesReader' object has no attribute 'SetForceOrthogonalDirection'

```

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [May 1, 2023, 7:47pm UTC](https://discourse.itk.org/t/gantry-tilt-correction-for-list-of-dicom-images/5907/4 "2023-05-01T19:47:25Z")

</div>

It might not be exposed in SimpleITK. Try `itk.ImageSeriesReader`, example [here](https://examples.itk.org/src/io/gdcm/readdicomseriesandwrite3dimage/documentation). And use ITK for the rest of the code, or convert the image from itk to sitk.
