-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread_all_dicom_series.py
60 lines (46 loc) · 1.79 KB
/
read_all_dicom_series.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
# -*- coding: utf-8 -*-
"""
Created on January 2021
@author: Katarina Milicevic, School of Electrical Engineering
Belgrade, Serbia
Find and extract individual series from folder with scan files
"""
import SimpleITK as sitk
import os
def main(fileDir, saveDir):
imageReader = sitk.ImageSeriesReader()
try:
seriesIDs = imageReader.GetGDCMSeriesIDs(fileDir)
except:
return 1
if not os.path.exists(saveDir):
os.makedirs(saveDir)
for i in range(0,len(seriesIDs)):
# dicom_names = reader.GetGDCMSeriesFileNames(sys.argv[1])
dicom_names = imageReader.GetGDCMSeriesFileNames(fileDir, seriesIDs[i])
imageReader.SetFileNames(dicom_names)
# Reading phase from one dicom file
reader = sitk.ImageFileReader()
reader.SetFileName(dicom_names[0])
reader.ReadImageInformation()
phase_name_key = "0008|103e"
phase_name = reader.GetMetaData(phase_name_key)
print(f"({phase_name_key}) = = \"{phase_name}\"")
time_key = "0008|0032"
try:
time = reader.GetMetaData(time_key)
print(f"({time_key}) = = \"{time}\"")
except:
print("There's no time key!")
image = imageReader.Execute()
size = image.GetSize()
print("Image size:", size[0], size[1], size[2])
phase_name = phase_name.replace(" ", "")
phase_name = phase_name.replace(".", "")
phase_name = phase_name.replace("/", "-")
outputFileName = saveDir+ "/series" + str(i) + "_" + phase_name + ".mha"
print("Writing image:", outputFileName)
sitk.WriteImage(image, outputFileName)
spacing = image.GetSpacing()
print(f"(spacing) = = \"{spacing}\"")
return 0