离线
作者:北欧森林
链接:https://www.jianshu.com/p/f82d30289d68
来源:简书,已获转载授权
RadiomicsWorld.com “影像组学世界”论坛:
影像组学世界/RadiomicsWorld
本笔记来源于B站Up主: 有Li 的影像组学系列教学视频
本节(35)主要介绍: 2D超声影像组学的特征提取
视频中李博士情境再现了小白研究者可能碰到的各种技术难题,并演示了解决这些问题的思路。
1、将dicom格式的2D 超声图像转为压缩的nifti格式,将其命名为test.nii.gz; 勾画ROI后同样保存为压缩的nifti格式,命名为mask.nii.gz
2、尝试提取影像学特征:
import radiomics from radiomics import featureextractor imageFile = "/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
3、按照提示,有可能是数据格式的问题。回到源代码,修改文件相应的格式,将其命名为 test1.nii.gz
import SimpleITK as sitk import numpy as np folderPath = "/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, z spacing = image.GetSpacing() #order:x, y, z direction = image.GetDirection() #print(spacing) pixelType = sitk.sitkInt8 #注意这里是Int8 image_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") 复制代码
4、将步骤[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!
5、执行 print(image_arr_new.shape) 后显示为:
Notes: 这里涉及python读入nifti文件的格式问题,其顺序为 z,y,x (对应这里的1,900,1600); 这里的3为不同的slice
6、探索这个“3”代表的意义:将每一个slice导出来,通过对比发现其中的端倪。执行代码:
image_arr_new = image_arr[:,:,:,0] #image_arr_new = image_arr[:,:,:,1] #image_arr_new = image_arr[:,:,:,2] #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 + "test-slice0.nii.gz") #sitk.WriteImage(image_new,folderPath + "test-slice1.nii.gz") #sitk.WriteImage(image_new,folderPath + "test-slice2.nii.gz") 复制代码
7、将test-slice0.nii.gz,test-slice1.nii.gz,test-slice2.nii.gz在软件中打开,查看它们之间的差异(肉眼似乎看不出啥区别)
8、使用写代码的方法来探索test-slice0.nii.gz,test-slice1.nii.gz,test-slice2.nii.gz之间的差异(视频里李博士演示了对比前二者)
import SimpleITK as sitk import numpy as np slicer0 = 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_arr print(comp) 复制代码
9、将差异的部分保存为影像导出
image_arr = slicer0_arr - slicer1_arr image_arr[image_arr != 0] = 1 size = slicer0.GetSize() origin = slicer0.GetOrigin() #order: x, y, z spacing = slicer0.GetSpacing() #order:x, y, z direction = 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") 复制代码
10、此时在软件中打开comp.nii.gz文件,查看差异(在于右下角的水印部分)(视频中是左下角的水印)。
换句话说,这3张test-slice0.nii.gz,test-slice1.nii.gz,test-slice2.nii.gz是一样的,差别仅在于右下角的水印。所以,后续的工作只需选择其中的一个即可。
11、再次查看test-slice0.nii.gz和mask文件,发现mask文件和源文件不对应。
mask.nii.gz是基于test.nii.gz文件画出并保存的,但是在test.nii.gz保存为test1.nii.gz时存在一个翻转(我没有搞明白这一点),所以应重新保存一下mask文件
import SimpleITK as sitk import numpy as np folderPath = "/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, z spacing = image.GetSpacing() #order:x, y, z direction = image.GetDirection() pixelType = sitk.sitkInt8 #注意这里是Int8 image_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") 复制代码
12、再次尝试提取影像组学特征(代码[2]),发现代码可以运行。
但是这里有一个隐藏的bug,就是mask文件虽然重新保存了,但是并不能和原来的位置匹配。
通过软件查看图像可以发现其中的区别。
13、再次回到原始代码里查找问题来源
import SimpleITK as sitk import numpy as np folderPath = "/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, z spacing = image.GetSpacing() #order:x, y, z direction = image.GetDirection() pixelType = sitk.sitkInt8 #注意这里是Int8 image_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") 复制代码
14、历尽周折,现在test0.nii.gz文件和mask0.nii.gz文件相匹配了!可以愉快地提取影像组学特征了。
import radiomics from radiomics import featureextractor imageFile = "/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])) 复制代码
来源:https://blog.csdn.net/weixin_38594676/article/details/113144043
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!