医工互联

 找回密码
 注册[Register]

手机动态码快速登录

手机号快速登录

微信登录

微信扫一扫,快速登录

QQ登录

只需一步,快速开始

查看: 440|回复: 0
收起左侧

Li‘s 影像组学radiomics视频学习笔记(35)-基于2D超声影像的影像组学特征提

[复制链接]

  离线 

发表于 2022-9-23 20:48:11 来自手机 | 显示全部楼层 |阅读模式 <
作者:北欧森林
链接: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

215001uk2c1egirohegmd7.jpeg


2、尝试提取影像学特征:

  1. import radiomics
  2. from radiomics import featureextractor
  3. imageFile = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/test.nii.gz"
  4. maskFile = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/mask.nii.gz"
  5. extractor = featureextractor.RadiomicsFeatureExtractor()
  6. featureVector = extractor.execute(imageFile, maskFile)
  7. #print(featureVector.items())
  8. for featureName in featureVector.keys():
  9.     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

  1. import SimpleITK as sitk
  2. import numpy as np
  3. folderPath = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/"
  4. reader = sitk.ImageSeriesReader()
  5. dicom_names = reader.GetGDCMSeriesFileNames(folderPath)
  6. reader.SetFileNames(dicom_names)
  7. image = reader.Execute()
  8. image_arr = sitk.GetArrayFromImage(image) # Note: order:z, y, x !!
  9. size = image.GetSize()
  10. origin = image.GetOrigin() #order: x, y, z
  11. spacing = image.GetSpacing() #order:x, y, z
  12. direction = image.GetDirection()
  13. #print(spacing)
  14. pixelType = sitk.sitkInt8 #注意这里是Int8
  15. image_new = sitk.Image(size,pixelType)
  16. #image_arr_new = image_arr[:,:,::-1] #镜像翻转操作
  17. image_arr_new = image_arr
  18. #print(image_arr_new.shape)
  19. image_new = sitk.GetImageFromArray(image_arr_new)
  20. image_new.SetDirection(direction)
  21. image_new.SetSpacing(spacing)
  22. image_new.SetOrigin(origin)
  23. 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) 后显示为:

  1. (1, 900, 1600, 3)
复制代码
Notes: 这里涉及python读入nifti文件的格式问题,其顺序为 z,y,x (对应这里的1,900,1600); 这里的3为不同的slice

6、探索这个“3”代表的意义:将每一个slice导出来,通过对比发现其中的端倪。执行代码:

  1. image_arr_new = image_arr[:,:,:,0]
  2. #image_arr_new = image_arr[:,:,:,1]
  3. #image_arr_new = image_arr[:,:,:,2]
  4. #print(image_arr_new.shape)
  5. image_new = sitk.GetImageFromArray(image_arr_new)
  6. image_new.SetDirection(direction)
  7. image_new.SetSpacing(spacing)
  8. image_new.SetOrigin(origin)
  9. sitk.WriteImage(image_new,folderPath + "test-slice0.nii.gz")
  10. #sitk.WriteImage(image_new,folderPath + "test-slice1.nii.gz")
  11. #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之间的差异(视频里李博士演示了对比前二者)

  1. import SimpleITK as sitk
  2. import numpy as np
  3. slicer0 = sitk.ReadImage("test-slice0.nii.gz")
  4. slicer1 = sitk.ReadImage("test-slice1.nii.gz")
  5. slicer0_arr = sitk.GetArrayFromImage(slicer0)
  6. slicer1_arr = sitk.GetArrayFromImage(slicer1)
  7. comp = slicer0_arr == slicer1_arr
  8. print(comp)
复制代码
215002pcciif55z17wecw5.jpeg


9、将差异的部分保存为影像导出

  1. image_arr = slicer0_arr - slicer1_arr
  2. image_arr[image_arr != 0] = 1
  3.  
  4. size = slicer0.GetSize()
  5. origin = slicer0.GetOrigin() #order: x, y, z
  6. spacing = slicer0.GetSpacing() #order:x, y, z
  7. direction = slicer0.GetDirection()
  8.  
  9. image_new = sitk.GetImageFromArray(image_arr)
  10. image_new.SetDirection(direction)
  11. image_new.SetSpacing(spacing)
  12. image_new.SetOrigin(origin)
  13. 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文件
215002ynehnld6r755reer.jpeg

  1. import SimpleITK as sitk
  2. import numpy as np
  3. folderPath = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/"
  4. mask = sitk.ReadImage('mask.nii.gz')
  5. mask_arr = sitk.GetArrayFromImage(mask)
  6. reader = sitk.ImageSeriesReader()
  7. dicom_names = reader.GetGDCMSeriesFileNames(folderPath)
  8. reader.SetFileNames(dicom_names)
  9. image = reader.Execute()
  10. image_arr = sitk.GetArrayFromImage(image) # Note: order:z, y, x !!
  11. size = image.GetSize()
  12. origin = image.GetOrigin() #order: x, y, z
  13. spacing = image.GetSpacing() #order:x, y, z
  14. direction = image.GetDirection()
  15. pixelType = sitk.sitkInt8 #注意这里是Int8
  16. image_new = sitk.Image(size,pixelType)
  17. mask_new = sitk.Image(size,pixelType)
  18. #image_arr_new = image_arr[:,:,::-1] #镜像翻转操作
  19. image_arr_new = image_arr[:,:,:,0]
  20. print(image_arr.shape)
  21. image_new = sitk.GetImageFromArray(image_arr_new)
  22. image_new.SetDirection(direction)
  23. image_new.SetSpacing(spacing)
  24. image_new.SetOrigin(origin)
  25. mask_new = sitk.GetImageFromArray(mask_arr) # 视频里勘误为mask_new,其实就应该是mask_arr, print二者可以看出区别来
  26. mask_new.SetDirection(direction)
  27. mask_new.SetSpacing(spacing)
  28. mask_new.SetOrigin(origin)
  29. sitk.WriteImage(image_new,"test0.nii.gz")
  30. sitk.WriteImage(mask_new,"mask0.nii.gz")
复制代码
215003mxx9hnqkkxzm441m.jpeg


12、再次尝试提取影像组学特征(代码[2]),发现代码可以运行。

但是这里有一个隐藏的bug,就是mask文件虽然重新保存了,但是并不能和原来的位置匹配。
通过软件查看图像可以发现其中的区别。
215004gonbrrrtnto9nybb.jpeg


13、再次回到原始代码里查找问题来源

  1. import SimpleITK as sitk
  2. import numpy as np
  3. folderPath = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/"
  4. mask = sitk.ReadImage('mask.nii.gz')
  5. mask_arr = sitk.GetArrayFromImage(mask)
  6. #reader = sitk.ImageSeriesReader()
  7. #dicom_names = reader.GetGDCMSeriesFileNames(folderPath)
  8. #reader.SetFileNames(dicom_names)
  9. #image = reader.Execute()
  10. image = sitk.ReadImage('test.nii.gz')
  11. image_arr = sitk.GetArrayFromImage(image) # Note: order:z, y, x !!
  12. size = image.GetSize()
  13. origin = image.GetOrigin() #order: x, y, z
  14. spacing = image.GetSpacing() #order:x, y, z
  15. direction = image.GetDirection()
  16. pixelType = sitk.sitkInt8 #注意这里是Int8
  17. image_new = sitk.Image(size,pixelType)
  18. mask_new = sitk.Image(size,pixelType)
  19. #image_arr_new = image_arr[:,:,::-1] #镜像翻转操作
  20. image_arr_new = image_arr[:,:,:,0]
  21. print(image_arr.shape)
  22. image_new = sitk.GetImageFromArray(image_arr_new)
  23. image_new.SetDirection(direction)
  24. image_new.SetSpacing(spacing)
  25. image_new.SetOrigin(origin)
  26. mask_new = sitk.GetImageFromArray(mask_arr)
  27. mask_new.SetDirection(direction)
  28. mask_new.SetSpacing(spacing)
  29. mask_new.SetOrigin(origin)
  30. sitk.WriteImage(image_new,"test0.nii.gz")
  31. sitk.WriteImage(mask_new,"mask0.nii.gz")
复制代码

14、历尽周折,现在test0.nii.gz文件和mask0.nii.gz文件相匹配了!可以愉快地提取影像组学特征了。

215004fvsji9kinh92vvrj.jpeg


  1. import radiomics
  2. from radiomics import featureextractor
  3. imageFile = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/test0.nii.gz"
  4. maskFile = "/Users/Mac/Documents/JianShuNotes/2D_Ultrasound/mask0.nii.gz"
  5. extractor = featureextractor.RadiomicsFeatureExtractor()
  6. featureVector = extractor.execute(imageFile, maskFile)
  7. #print(featureVector.items())
  8. for featureName in featureVector.keys():
  9.     print("%s: %s" % (featureName, featureVector[featureName]))
复制代码
215005szb6jqajkjkb2yfk.jpeg



来源:https://blog.csdn.net/weixin_38594676/article/details/113144043
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

提醒:禁止复制他人回复等『恶意灌水』行为,违者重罚!
您需要登录后才可以回帖 登录 | 注册[Register] 手机动态码快速登录 微信登录

本版积分规则

发布主题 快速回复 收藏帖子 返回列表 客服中心 搜索
简体中文 繁體中文 English 한국 사람 日本語 Deutsch русский بالعربية TÜRKÇE português คนไทย french

QQ|RSS订阅|小黑屋|处罚记录|手机版|联系我们|Archiver|医工互联 |粤ICP备2021178090号 |网站地图

GMT+8, 2024-11-24 18:12 , Processed in 0.297766 second(s), 66 queries .

Powered by Discuz!

Copyright © 2001-2023, Discuz! Team.

快速回复 返回顶部 返回列表