在线
本节(35)主要介绍:2D超声影像组学的特征提取
视频中情境再现了小白研究者可能碰到的各种技术难题,并演示了解决这些问题的思路。
将dicom格式的2D 超声图像转为压缩的nifti格式,将其命名为test.nii.gz; 勾画ROI后同样保存为压缩的nifti格式,命名为mask.nii.gz
import radiomics from radiomics import featureextractorimageFile = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/test.nii.gz"maskFile = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/mask.nii.gz"extractor = featureextractor.RadiomicsFeatureExtractor()featureVector = extractor.execute(imageFile, maskFile)#print(featureVector.items())for featureName in featureVector.keys(): print("%s: %s" % (featureName, featureVector[featureName]))出现了如下报错信息:
sitk::ERROR: Pixel type: vector of 8-bit unsigned integer is not supported in 3D byN3itk6simple26LabelStatisticsImageFilterE
按照提示,有可能是数据格式的问题。回到源代码,修改文件相应的格式,将其命名为 test1.nii.gz
import SimpleITK as sitkimport numpy as npfolderPath = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/"reader = sitk.ImageSeriesReader()dicom_names = reader.GetGDCMSeriesFileNames(folderPath)reader.SetFileNames(dicom_names)image = reader.Execute()image_arr = sitk.GetArrayFromImage(image) # Note: order:z, y, x !!size = image.GetSize()origin = image.GetOrigin() #order: x, y, zspacing = image.GetSpacing() #order:x, y, zdirection = image.GetDirection()#print(spacing) pixelType = sitk.sitkInt8 #注意这里是Int8image_new = sitk.Image(size,pixelType)#image_arr_new = image_arr[:,:,::-1] #镜像翻转操作image_arr_new = image_arr#print(image_arr_new.shape)image_new = sitk.GetImageFromArray(image_arr_new)image_new.SetDirection(direction)image_new.SetSpacing(spacing)image_new.SetOrigin(origin)sitk.WriteImage(image_new,folderPath + "test1.nii.gz")
将步骤[2]中的test.nii.gz替换为test1.nii.gz,再次执行该步骤
此时又出现了报错信息:
sitk::ERROR: Input "labelImage" for "LabelStatisticsImageFilter" has dimension of 3 which does not match the primary input's dimension of 2!
执行 print(image_arr_new.shape) 后显示为:
(1, 900, 1600, 3) Notes : 这里涉及python读入nifti文件的格式问题,其顺序为 z,y,x(对应这里的1,900,1600); 这里的3为不同的slice
将test-slice0.nii.gz,test-slice1.nii.gz,test-slice2.nii.gz在软件中打开,查看它们之间的差异(肉眼似乎看不出啥区别)
使用写代码的方法来探索test-slice0.nii.gz,test-slice1.nii.gz,test-slice2.nii.gz之间的差异(视频里演示了对比前二者)
import SimpleITK as sitkimport numpy as npslicer0 = sitk.ReadImage("test-slice0.nii.gz")slicer1 = sitk.ReadImage("test-slice1.nii.gz")slicer0_arr = sitk.GetArrayFromImage(slicer0)slicer1_arr = sitk.GetArrayFromImage(slicer1)comp = slicer0_arr == slicer1_arrprint(comp)
image_arr = slicer0_arr - slicer1_arrimage_arr[image_arr != 0] = 1size = slicer0.GetSize()origin = slicer0.GetOrigin() #order: x, y, zspacing = slicer0.GetSpacing() #order:x, y, zdirection = slicer0.GetDirection()image_new = sitk.GetImageFromArray(image_arr)image_new.SetDirection(direction)image_new.SetSpacing(spacing)image_new.SetOrigin(origin)sitk.WriteImage(image_new,"comp.nii.gz")
此时在软件中打开comp.nii.gz文件,查看差异(在于右下角的水印部分)(视频中是左下角的水印)。换句话说,这3张test-slice0.nii.gz,test-slice1.nii.gz,test-slice2.nii.gz是一样的,差别仅在于右下角的水印。所以,后续的工作只需选择其中的一个即可。
再次查看test-slice0.nii.gz和mask文件,发现mask文件和源文件不对应。mask.nii.gz是基于test.nii.gz文件画出并保存的,但是在test.nii.gz保存为test1.nii.gz时存在一个翻转(我没有搞明白这一点),所以应重新保存一下mask文件
import SimpleITK as sitkimport numpy as npfolderPath = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/"mask = sitk.ReadImage('mask.nii.gz')mask_arr = sitk.GetArrayFromImage(mask)reader = sitk.ImageSeriesReader()dicom_names = reader.GetGDCMSeriesFileNames(folderPath)reader.SetFileNames(dicom_names)image = reader.Execute()image_arr = sitk.GetArrayFromImage(image) # Note: order:z, y, x !!size = image.GetSize()origin = image.GetOrigin() #order: x, y, zspacing = image.GetSpacing() #order:x, y, zdirection = image.GetDirection()pixelType = sitk.sitkInt8 #注意这里是Int8image_new = sitk.Image(size,pixelType)mask_new = sitk.Image(size,pixelType)#image_arr_new = image_arr[:,:,::-1] #镜像翻转操作image_arr_new = image_arr[:,:,:,0]print(image_arr.shape)image_new = sitk.GetImageFromArray(image_arr_new)image_new.SetDirection(direction)image_new.SetSpacing(spacing)image_new.SetOrigin(origin)mask_new = sitk.GetImageFromArray(mask_arr) # 视频里勘误为mask_new,其实就应该是mask_arr, print二者可以看出区别来mask_new.SetDirection(direction)mask_new.SetSpacing(spacing)mask_new.SetOrigin(origin)sitk.WriteImage(image_new,"test0.nii.gz")sitk.WriteImage(mask_new,"mask0.nii.gz")
再次尝试提取影像组学特征(代码[2]),发现代码可以运行。但是这里有一个隐藏的bug,就是mask文件虽然重新保存了,但是并不能和原来的位置匹配。通过软件查看图像可以发现其中的区别
import SimpleITK as sitkimport numpy as npfolderPath = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/"mask = sitk.ReadImage('mask.nii.gz')mask_arr = sitk.GetArrayFromImage(mask)#reader = sitk.ImageSeriesReader()#dicom_names = reader.GetGDCMSeriesFileNames(folderPath)#reader.SetFileNames(dicom_names)#image = reader.Execute()image = sitk.ReadImage('test.nii.gz')image_arr = sitk.GetArrayFromImage(image) # Note: order:z, y, x !!size = image.GetSize()origin = image.GetOrigin() #order: x, y, zspacing = image.GetSpacing() #order:x, y, zdirection = image.GetDirection()pixelType = sitk.sitkInt8 #注意这里是Int8image_new = sitk.Image(size,pixelType)mask_new = sitk.Image(size,pixelType)#image_arr_new = image_arr[:,:,::-1] #镜像翻转操作image_arr_new = image_arr[:,:,:,0]print(image_arr.shape)image_new = sitk.GetImageFromArray(image_arr_new)image_new.SetDirection(direction)image_new.SetSpacing(spacing)image_new.SetOrigin(origin)mask_new = sitk.GetImageFromArray(mask_arr)mask_new.SetDirection(direction)mask_new.SetSpacing(spacing)mask_new.SetOrigin(origin)sitk.WriteImage(image_new,"test0.nii.gz")sitk.WriteImage(mask_new,"mask0.nii.gz")
历尽周折,现在test0.nii.gz文件和mask0.nii.gz文件相匹配了!可以愉快地提取影像组学特征了
import radiomics from radiomics import featureextractorimageFile = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/test0.nii.gz"maskFile = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/mask0.nii.gz"extractor = featureextractor.RadiomicsFeatureExtractor()featureVector = extractor.execute(imageFile, maskFile)#print(featureVector.items())for featureName in featureVector.keys(): print("%s: %s" % (featureName, featureVector[featureName]))邮箱:l-ry@hotmail.com(请将问题描述完整)
个人精力有限,无法私聊答疑,有问题请发送邮件,请互相尊重。
如需添加影像组学微信讨论组 ,请添加微信:hochzeitstorte
来源:https://zhuanlan.zhihu.com/p/389341780
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!