Python版:TotalSegmentator安装与操作手册——初学者友好指南
1. TotalSegmentator 介绍
TotalSegmentator 是一个基于Pytorch nnUNet 框架的医学图像分割工具。主要用于在 CT 和 MR 图像中进行解剖结构的分割。
Github地址:https://github.com/wasserth/TotalSegmentator
2. 安装(Linux conda环境下)
# 环境要求:
Python >= 3.9
Pytorch >= 1.12.1
# 环境安装
conda create -n TotalSegmentator python=3.10
conda activate TotalSegmentator
pip install TotalSegmentator
# 如果需要使用 --preview 选项,需要安装 xvfb 和 fury:
sudo apt-get install xvfb
pip install fury -i https://pypi.tuna.tsinghua.edu.cn/simple
3. 使用示例
3.1 单样本
# 对 CT 图像进行分割
TotalSegmentator -i ct.nii.gz -o segmentation
TotalSegmentator -i ./nii/pat1.nii.gz -o ./segmentation -rsr vertebrae_L1 vertebrae_L2 vertebrae_L3 vertebrae_L4 vertebrae_L5 -rmb --statistics --ml
# 对 MR 图像进行分割
TotalSegmentator -i mri.nii.gz -o segmentation --task total_mr
PS:执行命令时,第一步会自动下载预训练模型,因此,可先尝试先运行上述命令。
-i 输入图像,为包含一名患者的所有 DICOM 切片的 Nifti 文件或文件夹作为输入
-o 输出目录
--task 指基于CT图像或者MRI图像的主任务(CT图像是total,MRI是total_mr),或者子任务(子任务一般与血管,肌肉,积液,皮肤等任务相关),可参考本文末尾表格
-rs 指定感兴趣的分割目标
-rsr 类似于roi_subset,但使用更慢但更稳健的模型来查找ROI(推荐,准确度高)
--statistics # 计算分割结果的volume(即分割出的感兴趣区域(ROI)的体积,单位为立方毫米(mm³))与intensity (分割区域内的平均强度(灰度值),Hounsfield Units(HU,用于CT图像)或无单位(用于MRI图像))
-rmb 从最终分割中移除小的连通组件(小于0.2ml),用于减少噪音
--fast:能够用更快的运行时间和更少的内存需求进行分割,此选项下模型分辨率较低(3 毫米),正常模型分辨率为1.5 毫米。
3.2 批量使用
输入目录nii
如下:
批量处理命令(shell):
ls nii/ | while read i; do base_name=$(echo $i | sed 's/\.nii\.gz$//'); TotalSegmentator -i ./nii/$i -o ./segmentation/${base_name}/${base_name} -rsr vertebrae_L1 vertebrae_L2 vertebrae_L3 vertebrae_L4 vertebrae_L5 -rmb --statistics --ml; done
输出目录框架(与输入文件保持相同的文件名)
3.3 使用前预训练模型下载
在运行前,第一步会默认下载任务相关的预训练模型,而下载速度过慢容易导致中断,按下述方法可提升下载速度:
# 尝试修改权重下载地址为 GitHub 镜像
将 lib.py 文件中的权重下载地址从:
url = "https://github.com/wasserth/TotalSegmentator/releases/download"
改为
url = "https://github.moeyy.xyz/https://github.com/wasserth/TotalSegmentator/releases/download"
PS: lib.py 文件可通过 python -c "import totalsegmentator; print(totalsegmentator.__file__)" 命令定位
或直接在本地下载
Releases · wasserth/TotalSegmentator
TotalSegmentator 的预训练模型默认存储在 ~/.totalsegmentator/nnunet/results 目录,如下:
3.4 使用小技巧
分割单一器官或几个器官
TotalSegmentator会自动分割所有器官,但实际应用中,我们只对其中的一个或几个器官感兴趣。-rs
或-rsr
参数指定器官,上述命令表示只分割腰椎骨L1区域,如需指定多个器官就用就用空格分开各个器官,示例如下:
TotalSegmentator -i ./nii/pat1.nii.gz -o ./ --roi_subset vertebrae_L1 vertebrae_L2 vertebrae_L3 vertebrae_L4 vertebrae_L5 -rmb --statistics
但当你指定多个器官时,TotalSegmentator默认为每一个器官输出一个分割文件(比如上述命令输出vertebrae_L1.nii.gz、vertebrae_L2.nii.gz到vertebrae_L5.nii.gz共5个文件,指定--ml
可保存所有的分割结果到一个nii.gz文件中
TotalSegmentator -i ./nii/pat1.nii.gz -o ./segmentation -rsr vertebrae_L1 vertebrae_L2 vertebrae_L3 vertebrae_L4 vertebrae_L5 -rmb --statistics --ml
分割任务选择
TotalSegmentator主要用于 CT 和 MR 图像中进行解剖结构的分割,包括Main tasks 和Subtasks 。其中Main tasks 包括117个基于CT图像和50个基于MR图像的分割任务(主要为器官,骨椎和动静脉)。Subtasks 为血管,肌肉,积液,皮肤的识别。
(1)分割任务查找工具
TotalSegmentator 提供了一个查找任务的窗口TotalSegmentator find task
示例如下:
这表示,vertebrae_C3的分割任务在total下,即为基于CT图像的主要解剖任务。因此我们可以在上述命令的基础上添加-rs为vertebrae_C3。
整体命令为
TotalSegmentator -i ct.nii.gz -o seg -ta total -rs vertebrae_C3
(2)Main tasks
可做的任务清单可见网址,或下述表格
GitHub – wasserth/TotalSegmentator: Tool for robust segmentation of >100 important anatomical structures in CT and MR images
基于CT图像的解剖任务
Index | TotalSegmentator name | TA2 name | 中文名称 |
1 | spleen | 脾脏 | |
2 | kidney_right | 右肾 | |
3 | kidney_left | 左肾 | |
4 | gallbladder | 胆囊 | |
5 | liver | 肝脏 | |
6 | stomach | 胃 | |
7 | pancreas | 胰腺 | |
8 | adrenal_gland_right | suprarenal gland | 右肾上腺 |
9 | adrenal_gland_left | suprarenal gland | 左肾上腺 |
10 | lung_upper_lobe_left | superior lobe of left lung | 左肺上叶 |
11 | lung_lower_lobe_left | inferior lobe of left lung | 左肺下叶 |
12 | lung_upper_lobe_right | superior lobe of right lung | 右肺上叶 |
13 | lung_middle_lobe_right | middle lobe of right lung | 右肺中叶 |
14 | lung_lower_lobe_right | inferior lobe of right lung | 右肺下叶 |
15 | esophagus | 食管 | |
16 | trachea | 气管 | |
17 | thyroid_gland | 甲状腺 | |
18 | small_bowel | small intestine | 小肠 |
19 | duodenum | 十二指肠 | |
20 | colon | 结肠 | |
21 | urinary_bladder | 膀胱 | |
22 | prostate | 前列腺 | |
23 | kidney_cyst_left | 左肾囊肿 | |
24 | kidney_cyst_right | 右肾囊肿 | |
25 | sacrum | 骶骨 | |
26 | vertebrae_S1 | 骶椎S1 | |
27 | vertebrae_L5 | 腰椎L5 | |
28 | vertebrae_L4 | 腰椎L4 | |
29 | vertebrae_L3 | 腰椎L3 | |
30 | vertebrae_L2 | 腰椎L2 | |
31 | vertebrae_L1 | 腰椎L1 | |
32 | vertebrae_T12 | 胸椎T12 | |
33 | vertebrae_T11 | 胸椎T11 | |
34 | vertebrae_T10 | 胸椎T10 | |
35 | vertebrae_T9 | 胸椎T9 | |
36 | vertebrae_T8 | 胸椎T8 | |
37 | vertebrae_T7 | 胸椎T7 | |
38 | vertebrae_T6 | 胸椎T6 | |
39 | vertebrae_T5 | 胸椎T5 | |
40 | vertebrae_T4 | 胸椎T4 | |
41 | vertebrae_T3 | 胸椎T3 | |
42 | vertebrae_T2 | 胸椎T2 | |
43 | vertebrae_T1 | 胸椎T1 | |
44 | vertebrae_C7 | 颈椎C7 | |
45 | vertebrae_C6 | 颈椎C6 | |
46 | vertebrae_C5 | 颈椎C5 | |
47 | vertebrae_C4 | 颈椎C4 | |
48 | vertebrae_C3 | 颈椎C3 | |
49 | vertebrae_C2 | 颈椎C2 | |
50 | vertebrae_C1 | 颈椎C1 | |
51 | heart | 心脏 | |
52 | aorta | 主动脉 | |
53 | pulmonary_vein | 肺静脉 | |
54 | brachiocephalic_trunk | 无名动脉(头臂干) | |
55 | subclavian_artery_right | 右锁骨下动脉 | |
56 | subclavian_artery_left | 左锁骨下动脉 | |
57 | common_carotid_artery_right | 右颈总动脉 | |
58 | common_carotid_artery_left | 左颈总动脉 | |
59 | brachiocephalic_vein_left | 左无名静脉 | |
60 | brachiocephalic_vein_right | 右无名静脉 | |
61 | atrial_appendage_left | 左心耳 | |
62 | superior_vena_cava | 上腔静脉 | |
63 | inferior_vena_cava | 下腔静脉 | |
64 | portal_vein_and_splenic_vein | hepatic portal vein | 门静脉和脾静脉 |
65 | iliac_artery_left | common iliac artery | 左髂总动脉 |
66 | iliac_artery_right | common iliac artery | 右髂总动脉 |
67 | iliac_vena_left | common iliac vein | 左髂总静脉 |
68 | iliac_vena_right | common iliac vein | 右髂总静脉 |
69 | humerus_left | 左肱骨 | |
70 | humerus_right | 右肱骨 | |
71 | scapula_left | 左肩胛骨 | |
72 | scapula_right | 右肩胛骨 | |
73 | clavicula_left | clavicle | 左锁骨 |
74 | clavicula_right | clavicle | 右锁骨 |
75 | femur_left | 左股骨 | |
76 | femur_right | 右股骨 | |
77 | hip_left | 左髋骨 | |
78 | hip_right | 右髋骨 | |
79 | spinal_cord | 脊髓 | |
80 | gluteus_maximus_left | gluteus maximus muscle | 左臀大肌 |
81 | gluteus_maximus_right | gluteus maximus muscle | 右臀大肌 |
82 | gluteus_medius_left | gluteus medius muscle | 左臀中肌 |
83 | gluteus_medius_right | gluteus medius muscle | 右臀中肌 |
84 | gluteus_minimus_left | gluteus minimus muscle | 左臀小肌 |
85 | gluteus_minimus_right | gluteus minimus muscle | 右臀小肌 |
86 | autochthon_left | 左髂腰肌 | |
87 | autochthon_right | 右髂腰肌 | |
88 | iliopsoas_left | iliopsoas muscle | 左髂腰肌 |
89 | iliopsoas_right | iliopsoas muscle | 右髂腰肌 |
90 | brain | 大脑 | |
91 | skull | 颅骨 | |
92 | rib_left_1 | 左侧第1肋骨 | |
93 | rib_left_2 | 左侧第2肋骨 | |
94 | rib_left_3 | 左侧第3肋骨 | |
95 | rib_left_4 | 左侧第4肋骨 | |
96 | rib_left_5 | 左侧第5肋骨 | |
97 | rib_left_6 | 左侧第6肋骨 | |
98 | rib_left_7 | 左侧第7肋骨 | |
99 | rib_left_8 | 左侧第8肋骨 | |
100 | rib_left_9 | 左侧第9肋骨 | |
101 | rib_left_10 | 左侧第10肋骨 | |
102 | rib_left_11 | 左侧第11肋骨 | |
103 | rib_left_12 | 左侧第12肋骨 | |
104 | rib_right_1 | 右侧第1肋骨 | |
105 | rib_right_2 | 右侧第2肋骨 | |
106 | rib_right_3 | 右侧第3肋骨 | |
107 | rib_right_4 | 右侧第4肋骨 | |
108 | rib_right_5 | 右侧第5肋骨 | |
109 | rib_right_6 | 右侧第6肋骨 | |
110 | rib_right_7 | 右侧第7肋骨 | |
111 | rib_right_8 | 右侧第8肋骨 | |
112 | rib_right_9 | 右侧第9肋骨 | |
113 | rib_right_10 | 右侧第10肋骨 | |
114 | rib_right_11 | 右侧第11肋骨 | |
115 | rib_right_12 | 右侧第12肋骨 | |
116 | sternum | 胸骨 | |
117 | costal_cartilages | 肋软骨 |
基于MRI图像的解剖任务
Index | TotalSegmentator name | TA2 name | 中文名称 |
---|---|---|---|
1 | spleen | 脾脏 | |
2 | kidney_right | 右肾 | |
3 | kidney_left | 左肾 | |
4 | gallbladder | 胆囊 | |
5 | liver | 肝脏 | |
6 | stomach | 胃 | |
7 | pancreas | 胰腺 | |
8 | adrenal_gland_right | suprarenal gland | 右肾上腺 |
9 | adrenal_gland_left | suprarenal gland | 左肾上腺 |
10 | lung_left | 左肺 | |
11 | lung_right | 右肺 | |
12 | esophagus | 食管 | |
13 | small_bowel | small intestine | 小肠 |
14 | duodenum | 十二指肠 | |
15 | colon | 结肠 | |
16 | urinary_bladder | 膀胱 | |
17 | prostate | 前列腺 | |
18 | sacrum | 骶骨 | |
19 | vertebrae | 椎骨 | |
20 | intervertebral_discs | 椎间盘 | |
21 | spinal_cord | 脊髓 | |
22 | heart | 心脏 | |
23 | aorta | 主动脉 | |
24 | inferior_vena_cava | 下腔静脉 | |
25 | portal_vein_and_splenic_vein | hepatic portal vein | 门静脉和脾静脉 |
26 | iliac_artery_left | common iliac artery | 左髂总动脉 |
27 | iliac_artery_right | common iliac artery | 右髂总动脉 |
28 | iliac_vena_left | common iliac vein | 左髂总静脉 |
29 | iliac_vena_right | common iliac vein | 右髂总静脉 |
30 | humerus_left | 左肱骨 | |
31 | humerus_right | 右肱骨 | |
32 | scapula_left | 左肩胛骨 | |
33 | scapula_right | 右肩胛骨 | |
34 | clavicula_left | 左锁骨 | |
35 | clavicula_right | 右锁骨 | |
36 | femur_left | 左股骨 | |
37 | femur_right | 右股骨 | |
38 | hip_left | 左髋骨 | |
39 | hip_right | 右髋骨 | |
40 | gluteus_maximus_left | gluteus maximus muscle | 左臀大肌 |
41 | gluteus_maximus_right | gluteus maximus muscle | 右臀大肌 |
42 | gluteus_medius_left | gluteus medius muscle | 左臀中肌 |
43 | gluteus_medius_right | gluteus medius muscle | 右臀中肌 |
44 | gluteus_minimus_left | gluteus minimus muscle | 左臀小肌 |
45 | gluteus_minimus_right | gluteus minimus muscle | 右臀小肌 |
46 | autochthon_left | 左髂腰肌 | |
47 | autochthon_right | 右髂腰肌 | |
48 | iliopsoas_left | iliopsoas muscle | 左髂腰肌 |
49 | iliopsoas_right | iliopsoas muscle | 右髂腰肌 |
50 | brain | 大脑 |
(3)Subtasks
Subtasks主要为血管,肌肉,积液,皮肤的识别,具体可参考官方文档:GitHub – wasserth/TotalSegmentator: Tool for robust segmentation of >100 important anatomical structures in CT and MR images
其他
如何你想知道图像是哪种模态(CT 或 MR):
# 需要先安装 pip install xgboost
totalseg_get_modality -i image.nii.gz -o modality.json
如何你想知道 CT 图像是哪个 contrast phase(对比剂增强阶段):
totalseg_get_phase -i ct.nii.gz -o contrast_phase.json
在TotalSegmentator分割的基础上,将原图和分割结果加载进ITK-SNAP或者3D slicer等标注软件进行手动修改。大大减少标注时间,提升标注效率。
作者:今天也不想动