-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeatureExtration4.py
59 lines (46 loc) · 1.82 KB
/
FeatureExtration4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
from radiomics import featureextractor
import SimpleITK as sitk
import csv
# 配置参数文件路径
params = None # 或者 None 使用默认参数
# 初始化特征提取器
extractor = featureextractor.RadiomicsFeatureExtractor(params)
# 指定图像和掩码文件夹
image_folder = "ZJ_ADC_Source"
mask_folder = "ZJ_ADC_Seg"
# 输出 CSV 文件路径
output_csv = "output.csv"
# 初始化 CSV 文件
with open(output_csv, 'w', newline='') as outputFile:
fieldnames = ["Image", "Mask"]
for key in extractor.featureClassNames:
fieldnames.extend(extractor.getFeatureNames(key))
writer = csv.DictWriter(outputFile, fieldnames=fieldnames)
writer.writeheader()
# 遍历文件夹并进行特征提取
for image_name in os.listdir(image_folder):
if not image_name.endswith(".nii.gz"):
continue
base_name = os.path.splitext(os.path.splitext(image_name)[0])[0]
mask_name = f"{base_name}_seg.nii.gz"
image_path = os.path.join(image_folder, image_name)
mask_path = os.path.join(mask_folder, mask_name)
if not os.path.exists(mask_path):
print(f"Mask for {image_name} not found. Skipping...")
continue
# 读取图像和掩码
image = sitk.ReadImage(image_path)
mask = sitk.ReadImage(mask_path)
# 提取特征
result = extractor.execute(image, mask)
# 保存到 CSV 文件
with open(output_csv, 'a', newline='') as outputFile:
writer = csv.DictWriter(outputFile, fieldnames=fieldnames)
row_data = {"Image": image_name, "Mask": mask_name}
for key, value in result.items():
if key.startswith("diagnostics"):
continue
row_data[key] = value
writer.writerow(row_data)
print(f"Features extracted from {image_name}")