将图像转换为RGB,以便我可以可视化图像

时间:2019-04-04 05:25:57

标签: python-3.x image-resizing simpleitk

我正在尝试对图像进行分割,但无法应用于对图像重新调整强度

我试图重塑图像并应用rescaleintensity,但无法获取

 def load_sitk_image(path,vol_size=np.array([128,128,64]),dst_res=[1,1,1],method = sitk.sitkLinear ) :

img = sitk.ReadImage(path)

ret = np.zeros([vol_size[0], vol_size[1], vol_size[2]], dtype=np.float32)

#we rotate the image according to its transformation using the direction and according to the final spacing we want

factor = np.asarray(img.GetSpacing()) / [dst_res[0], dst_res[1],dst_res[2]]

factorSize = np.asarray(img.GetSize() * factor, dtype=float)

newSize = np.max([factorSize, vol_size], axis=0)

newSize = newSize.astype(dtype='int')

T=sitk.AffineTransform(3)

T.SetMatrix(img.GetDirection())

resampler = sitk.ResampleImageFilter()

resampler.SetReferenceImage(img)

resampler.SetOutputSpacing([dst_res[0], dst_res[1], dst_res[2]])

resampler.SetSize(newSize.tolist())

resampler.SetInterpolator(method)

imgResampled = resampler.Execute(img)

imgCentroid = np.asarray(newSize, dtype=float) / 2.0

imgStartPx = (imgCentroid - vol_size / 2.0).astype(dtype='int')

regionExtractor = sitk.RegionOfInterestImageFilter()

size_2_set = vol_size.astype(dtype='int')

regionExtractor.SetSize(size_2_set.tolist())

regionExtractor.SetIndex(imgStartPx.tolist())

imgResampledCropped = regionExtractor.Execute(imgResampled)

ret = np.transpose(sitk.GetArrayFromImage(imgResampledCropped).astype(dtype=float),[0,1,2])

ret = (ret - ret.min())/(ret.max()-ret.min())

#ret = (ret - ret.mean())/ret.std()

return ret

img_T1 = load_sitk_image(path = r"C:\Users\chinnu\Desktop\post processing project\training part 1\case01.mhd")

img_T2 = load_sitk_image(path =r"C:\Users\chinnu\Desktop\post processing project\training\Case01_segmentation.mhd")


img_T1_255 = sitk.Cast(sitk.RescaleIntensity(img_T1), sitk.sitkUInt8)

> 错误:

TypeError跟踪(最近一次通话)  在()中       3       4 ----> 5 img_T1_255 = sitk.Cast(sitk.RescaleIntensity(img_T1),sitk.sitkUInt8)

〜\ Anaconda3 \ lib \ site-packages \ SimpleITK \ SimpleITK.py in RescaleIntensity(image1,outputMinimum,outputMaximum)   59087   59088“”“

  

59089返回_SimpleITK.RescaleIntensity(image1,outputMinimum,outputMaximum)     59090类RichardsonLucyDeconvolutionImageFilter(ImageFilter_2):     59091“”“

TypeError:在方法'RescaleIntensity'中,类型为'itk :: simple :: Image const&'的参数1

我希望像我这样的结果可以应用重新缩放强度

1 个答案:

答案 0 :(得分:1)

检查'img_T1'的类型-我认为您正在load_sitk_image函数中返回一个numpy数组。您需要使用GetImageFromArray返回SimpleITK.Image