Correcting the Gantry Tilt in CT scans

Changing the ViewUp will not affect the rectification. It really looks to me as if you are displaying the wrong image. Is there somewhere in your code where you set the input of vtkImageMapToColors to something else? Or maybe you are doing self.color = vtkImageMapToColors() more than once, thus overwriting your old color mapper with a new one? All that I can do is make wild guesses, since I haven’t seen the code you use to display the image.

vtkImageMapToColors() is called only once and then I am passing the output of self.color to vtkImageViewer2(). Should I use a different image viewer?

Here is my viewer code:

        viewer.SetInputConnection(self.color.GetOutputPort())
        viewer.SetOffScreenRendering(1)
        if view_type == Constants.ViewType.AXIAL:
            viewer.SetSliceOrientationToXY()
        elif view_type == Constants.ViewType.CORONAL:
            viewer.SetSliceOrientationToXZ()
        elif view_type == Constants.ViewType.SAGITAL:
            viewer.SetSliceOrientationToYZ()
        viewer.GetRenderWindow().SetMultiSamples(128)
        viewer.GetRenderer().UseFXAAOn()
        # viewer.GetRenderer().GetActiveCamera().ParallelProjectionOff()

        worldPicker = vtkPointPicker()
        worldPicker.SetTolerance(0.001)
        widget.SetPicker(worldPicker)
        viewer.GetImageActor().InterpolateOn()
        viewer.SetRenderWindow(widget.GetRenderWindow())
        viewer.SetupInteractor(widget)
        widget.SetInteractorStyle(interactor_style)
        viewer.Render()
        if view_type == Constants.ViewType.CORONAL:
            if image_orientation == Constants.ViewType.AXIAL:
                viewer.GetRenderer().GetActiveCamera().SetViewUp(0, -1, 0)
            elif image_orientation == Constants.ViewType.SAGITAL:
                viewer.GetRenderer().GetActiveCamera().SetViewUp(-1, 0, 0)
        elif view_type == Constants.ViewType.SAGITAL:
            if image_orientation == Constants.ViewType.AXIAL:
                viewer.GetRenderer().GetActiveCamera().SetViewUp(0, 0, -1)
            elif image_orientation == Constants.ViewType.SAGITAL:
                viewer.GetRenderer().GetActiveCamera().SetViewUp(0, 1, 0)
            elif image_orientation == Constants.ViewType.CORONAL:
                viewer.GetRenderer().GetActiveCamera().SetViewUp(0, 1, 0)
        viewer.GetRenderer().ResetCamera()
        viewer.SetSlice(int(viewer.GetSliceMax() / 2))
        viewer.Render()
        viewer.UpdateDisplayExtent()
        viewer.Render()

Using vtkImageViewer2 should be fine.

As a test, can you add a vtkJPEGWriter to your code and use it to write the output of vtkImageMapToColors as a set of jpeg images? Add it immediately after self.color.SetInputConnection(rectify.GetOutputPort()).

ok, will try this out

Its only writing axial slices. Is there any way to change the axis, so that i can write the sagittal images?

You can use vtkImagePermute

permute = vtkImagePermute()
permute.SetInputConnection(...)
permute.SetFilteredAxes(1,2,0)
permute.Update()

Same result. can it be a python issue?

output_ima256

It’s extremely unlikely to be a Python issue.

I wrote a Python example that you can try: DisplayCT.py (2.1 KB) , here is the image that it displays:

DisplayCT

I found the problem. In order for the rectification to work correctly, you need this:

   reader.SetMemoryRowOrderToFileNative()

This line keeps the reader from vertically flipping the images into the “VTK preferred orientation”. I don’t want to go too deep into the meaning of “VTK preferred orientation”, but the basic idea is that the default settings of vtkImageViewer assume that the first pixel of an image is at the bottom left, meaning that images are rasterized from bottom to top. Of course most images, including JPEG and DICOM, are rasterized from top to bottom, but most VTK image readers tend to automatically apply a flip. But enough on this topic, and back to CT rectification:

The vtkDICOMReader seems to have a bug where it orthogonalizes the 3x3 portion of the PatientMatrix when it adjusts it to account for the “VTK preferred orientation” flip. Usually this 3x3 portion of the matrix is already orthogonal. It’s only non-orthogonal for tilted-gantry CT images.

I’m going to dig through the code to find the bug, but until I fix it, you will have to use SetMemoryRowOrderToFileNative(). I always use this setting, which is why I didn’t see the bug previously.

Hey it worked, Thank you so much. The orientation got screwed though, will have to change the viewup vectors.