Save the vtkImageReslice 2D output to file

Hello friends,

I have the following problem with saving the vtkImageReslice output to file using vtkBMPWriter.

The output of the vtkImageReslice in the vtkImageViewer2 looks good, as demonstrated in the following image:
axial

It is exactly what I need.

Now, I need to save this output to the bmp file so I use vtkBMPWriter. The problem is that I need to set the vtkImageReslice output to the unsigned char as the vtkBMPWriter doesn’t support another input.

Then, when I save the output, it looks like on the following image:

axial2

There are some unwanted vertical lines.

Here is the code snippet:

       self.imageReslice = vtk.vtkImageReslice()
       self.imageReslice.SetInputConnection(self.extractor.GetOutputPort())
       self.imageReslice.SetOutputDimensionality(2)
       self.imageReslice.SetResliceAxesDirectionCosines(1, 0, 0, 0, 1, 0, 0, 0, 1) 
       self.imageReslice.InterpolateOn()
       self.imageReslice.SetOutputSpacing(xSpacing, ySpacing, zSpacing)
       self.imageReslice.SetResliceAxesOrigin(center1[0], center1[1], center1[2])
       self.imageReslice.AutoCropOutputOn()
       self.imageReslice.UpdateWholeExtent()
       
       viewer = vtk.vtkImageViewer2()
       viewer.SetInputConnection(self.imageReslice.GetOutputPort())
       viewer.SetColorWindow(255)
       viewer.SetColorLevel(127.5)
       viewer.Render()

       self.imageReslice.SetOutputScalarType(vtk.VTK_UNSIGNED_CHAR)

       self.writerAxial.SetInputConnection(self.imageReslice.GetOutputPort())
       self.writerAxial.SetFileName("axial.bmp")
       self.writerAxial.Update()
       self.writerAxial.Write()

What can I do to have result on the saved image the same as the result in the image viewer window?
Do I need to add some filter or do anything else?

Thank you and have a nice day,

Jirka

You can convert the data type of vtkImageReslice.GetOutput from ‘float’ to unsigned_char.

The code would looks like:

imageReslice.Update()
imgCast = vtk.vtkImageCast()
imgCast.SetInputData(imageReslice.GetOutput())
imgCast.SetOutputScalrTypeToChar()
imgCast.Update()
...
writerAxial.SetInput(imgCast.GetOutput())

The BMP writer might require a 24-bit RGB image instead of just an 8-bit image.

Try using vtkImageResliceToColors instead of using vtkImageReslice with SetOutputScalarType().