##SimpleITK CT图像窗宽窗位变换
# -- coding: utf-8 --
import SimpleITK as sitk
import os
import numpy as np
def window_transform(ct_array, windowWidth, windowCenter, normal=False):
"""
return: trucated image according to window center and window width
and normalized to [0,1]
"""
minWindow = float(windowCenter) - 0.5*float(windowWidth)
newimg = (ct_array - minWindow) / float(windowWidth)
newimg[newimg < 0] = 0
newimg[newimg > 1] = 1
if not normal:
newimg = (newimg * 255).astype('float32')
return newimg
print("-"*100)
image=sitk.ReadImage(path,sitk.sitkFloat32)
array=sitk.GetArrayFromImage(image)
array=window_transform(array,400,100,True)
new_image=sitk.GetImageFromArray(new_image)
new_image.SetDirection(image.GetDirection())
new_image.SetOrigin([0,0,0])
new_image.SetSpacing([1,1,1])