-
Notifications
You must be signed in to change notification settings - Fork 0
/
classify_dicom_images.py
51 lines (43 loc) · 1.85 KB
/
classify_dicom_images.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
import os
import shutil
import pydicom
def find_dicom_files(directory):
dicom_files = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".dcm"):
dicom_files.append(os.path.join(root, file))
return dicom_files
def categorize_dicom_files(dicom_files):
categorized_files = {"cine": [], "mag": [], "x": [], "y": [], "z": [], "unknown": []}
for file in dicom_files:
ds = pydicom.dcmread(file)
if (0x0043, 0x1030) in ds:
image_type = ds[(0x0043, 0x1030)].value
if image_type == 0 or image_type == 7:
categorized_files["cine"].append(file)
elif image_type == 2 or image_type == 8:
categorized_files["mag"].append(file)
elif image_type == 3 or image_type == 9:
categorized_files["x"].append(file)
elif image_type == 4 or image_type == 10:
categorized_files["y"].append(file)
elif image_type == 5 or image_type == 11:
categorized_files["z"].append(file)
else:
categorized_files["unknown"].append(file)
else:
categorized_files["unknown"].append(file)
return categorized_files
def copy_categorized_files(categorized_files, output_directory):
for category, files in categorized_files.items():
category_directory = os.path.join(output_directory, category)
os.makedirs(category_directory, exist_ok=True)
for file in files:
shutil.copy(file, category_directory)
if __name__ == "__main__":
input_directory = ""
output_directory = ""
dicom_files = find_dicom_files(input_directory)
categorized_files = categorize_dicom_files(dicom_files)
copy_categorized_files(categorized_files, output_directory)