-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDicomVisualizer.py
More file actions
30 lines (25 loc) · 889 Bytes
/
Copy pathDicomVisualizer.py
File metadata and controls
30 lines (25 loc) · 889 Bytes
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
import os
import pydicom
import numpy as np
import matplotlib.pyplot as plt
def load_volume_from_files(dicom_dir):
files = [os.path.join(dicom_dir, f) for f in os.listdir(dicom_dir) if f.endswith(".dcm")]
slices = []
for f in files:
dcm = pydicom.dcmread(f)
slices.append(dcm)
slices.sort(key=lambda x: float(x.InstanceNumber)) # sort slices properly
volume = np.stack([s.pixel_array for s in slices], axis=0)
volume = volume.astype(np.float32)
volume = (volume - volume.min()) / (volume.max() - volume.min())
return volume
def show_middle_slice(volume):
mid = volume.shape[0] // 2
plt.imshow(volume[mid], cmap='gray')
plt.title(f"Middle Slice (index {mid})")
plt.axis('off')
plt.show()
# === USAGE ===
dicom_dir = "series" # This matches your folder
vol = load_volume_from_files(dicom_dir)
show_middle_slice(vol)