/
CBCT_preprocess.py
81 lines (63 loc) · 2.34 KB
/
CBCT_preprocess.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import SimpleITK as sitk
from tqdm import tqdm
def ExtractLargestConnectedComponents(label):
ccFilter = sitk.ConnectedComponentImageFilter()
label = ccFilter.Execute(label)
labelStat = sitk.LabelShapeStatisticsImageFilter()
labelStat.Execute(label)
largestVol = 0
largestVol2 = 0
largestLabel = 0
largestLabel2 = 0
for labelNum in labelStat.GetLabels():
if labelStat.GetPhysicalSize(labelNum) > largestVol:
largestVol = labelStat.GetPhysicalSize(labelNum)
largestLabel = labelNum
elif labelStat.GetPhysicalSize(labelNum) > largestVol2:
largestVol2 = labelStat.GetPhysicalSize(labelNum)
largestLabel2 = labelNum
print(largestLabel,largestLabel2)
thresholdFilter = sitk.BinaryThresholdImageFilter()
thresholdFilter.SetInsideValue(1)
thresholdFilter.SetOutsideValue(0)
thresholdFilter.SetLowerThreshold(largestLabel)
thresholdFilter.SetUpperThreshold(largestLabel)
label_0 = thresholdFilter.Execute(label)
thresholdFilter.SetLowerThreshold(largestLabel2)
thresholdFilter.SetUpperThreshold(largestLabel2)
label_1 = thresholdFilter.Execute(label)
addFilter = sitk.AddImageFilter()
# label = addFilter.Execute(label_0,label_1)
label = label_0
return label
def preprocess(src,tgt):
if not os.path.exists(src):
return
reader = sitk.ImageFileReader()
reader.SetFileName(src)
label = reader.Execute()
# lcc
label = ExtractLargestConnectedComponents(label)
# smoothing
# smoothFilter = sitk.BinaryMorphologicalClosingImageFilter()
# smoothFilter = sitk.BinaryMorphologicalOpeningImageFilter()
# smoothFilter.SetKernelType(sitk.sitkBall)
# smoothFilter.SetKernelRadius(2)
# label = smoothFilter.Execute(label)
writer = sitk.ImageFileWriter()
writer.SetFileName(tgt)
writer.Execute(label)
def main():
data_dir = "Z:/data/intracranial/followup_flatten/3DRA"
pbar = tqdm(os.listdir(data_dir))
# cases = ["stent_ChanYS_followup","stent_WongYK_followup"]
cases = ["stent_ChanT_baseline-post","stent_ChanT_12months","stent_LingKW_baseline-post","stent_MakSL_baseline","stent_MakSL_baseline-post","stent_NgNY_baseline-post","stent_YeungSL_baseline-post"]
for case in pbar:
if not case in cases:
continue
pbar.set_description(case)
preprocess(os.path.join(data_dir,case, "3DRA_seg_ICA_terminus.nii.gz"),os.path.join(data_dir,case, "3DRA_seg_ICA_terminus_lcc.nii.gz"))
# exit()
if __name__ == "__main__":
main()