我需要将 DICOM 图像转换为 JPG/PNG 并使用 VTK 保存图像,但我生成的图像与原始图像不匹配。
我知道我需要重新缩放图像的像素来转换它,但我不知道如何。有谁知道我如何才能正确进行转换?
下面是我的 python 代码:
from vtk import *
reader = vtkDICOMImageReader()
reader.SetFileName('image.dcm')
reader.Update()
castFilter = vtkImageCast()
castFilter.SetOutputScalarTypeToUnsignedChar()
castFilter.SetInputConnection(reader.GetOutputPort())
castFilter.Update()
writer = vtkJPEGWriter()
writer.SetFileName('output.jpg')
writer.SetInputConnection(castFilter.GetOutputPort())
writer.Write()
最佳答案
MRI 和 CT 模式中的 DICOM 通常是短类型,并且您将图像无情地转换为 unsigned char。
如果您尝试获取相应的 uchar 图像,则应该使用 vtkImageShiftScale
,就像 vtkImageCast
文档所述:
Warning As vtkImageCast only casts values without rescaling them, its use is not recommented. vtkImageShiftScale is the recommented way to change the type of an image data.
关于python-2.7 - 使用VTK转换DICOM图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33536215/