Repository navigation
Expand file tree
/
Copy pathdicomProcessing.py
More file actions
289 lines (261 loc) · 12.7 KB
/
Copy pathdicomProcessing.py
File metadata and controls
289 lines (261 loc) · 12.7 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import vtk
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import os
import re
import json
import pydicom
import numpy as np
from pydicom.pixel_data_handlers.util import apply_modality_lut, apply_voi_lut
import pandas as pd
from config import EXCEL_PATHS
'''
The DICOM standard specifies the patient coordinate system in a very specific way: the positive X-axis points to the patient's left,
the positive Y-axis points to the anterior (front) of the patient, and the positive Z-axis points to the head.
In other words, if the patient is lying down in the scanner with their head pointed at the screen and their feet pointing away,
their left would be to the right of the screen, their anterior would be to the top of the screen, and their head would be coming out of the screen.
'''
excel_paths = EXCEL_PATHS
max_landmarks = {'DATASET_AXIAL':11,'DATASET_SAGITTAL':7,'DATASET_DYNAMIC':18}
class Landmark:
def __init__(self, position):
self.position = position # includes the index
self.circle = vtk.vtkRegularPolygonSource()
self.circle.SetNumberOfSides(50)
self.circle.SetCenter(position[0], position[1], 0)
self.circle.SetRadius(1.5) # Set the radius of the marker
#print(f'Landmark index: {self.position[2]}, Landmark center: {self.circle.GetCenter()} , Landmark radius: {self.circle.GetRadius()}')
self.circleMapper = vtk.vtkPolyDataMapper()
self.circleMapper.SetInputConnection(self.circle.GetOutputPort())
self.circleActor = vtk.vtkActor()
self.circleActor.SetMapper(self.circleMapper)
r, g, b = 179/255.0, 29/255.0, 29/255.0 #normalized to 0-1 scale
self.circleActor.GetProperty().SetColor(r,g,b) #b31d1d color
class DICOMImage:
def __init__(self, dicom_dir,ren):
# DICOM Paths for pydicom parsing
self.dicom_dir = dicom_dir
self.dicom_paths = [os.path.join(dicom_dir, file_name) for file_name in sorted(os.listdir(dicom_dir)) if file_name.endswith('.dcm')]
self.image_data = vtk.vtkImageData()
self.actor = vtk.vtkImageActor()
self.ren = ren
self.ren.AddActor(self.actor)
self.landmarks={}
# Image properties
self.index = 0
self.max_slice = len(self.dicom_paths)
self.width = 0
self.height = 0
self.dataset_type = ''
self.landmark_count = 1
self.max_count = 0
self.knee = ''
self.individual = ''
self.sequence = ''
self.extract_components(dicom_dir) # populate them props
#first load
self.load_dicom()
self.update_image()
self.actor.Modified()
self.center = self.actor.GetCenter()
# Check if there's a corresponding JSON file with landmarks
json_file = os.path.join(self.dicom_dir, f"{self.sequence}.json")
if os.path.exists(json_file):
self.load_landmarks_from_json(json_file)
self.status = 1
else:
self.status = 0
''' DICOM image iteration Treatment '''
def load_dicom(self):
if not self.dicom_paths:
print("No valid DICOM images found.")
return
try:
first_ds = pydicom.dcmread(self.dicom_paths[0])
self.image_data.SetDimensions(first_ds.Columns,first_ds.Rows, len(self.dicom_paths))
self.image_data.AllocateScalars(vtk.VTK_INT, 1)
self.image_data.width = self.width = first_ds.Columns
self.image_data.height = self.height = first_ds.Rows
for i, dicom_path in enumerate(self.dicom_paths):
try:
ds = pydicom.dcmread(dicom_path)
arr = ds.pixel_array
hu = apply_modality_lut(arr, ds)
pixel_array = apply_voi_lut(hu, ds)
# normalizing the image intensities between 0 and 255 in order to display as a grayscale image.
pixel_array = ((pixel_array - pixel_array.min()) * (255.0 / (pixel_array.max() - pixel_array.min()))).astype('uint8')
for y in range(arr.shape[0]):
for x in range(arr.shape[1]):
self.image_data.SetScalarComponentFromFloat(x, y, i, 0, pixel_array[y, x])
except Exception as e:
print(f"Error reading {dicom_path} during pixel array formation: {str(e)}")
continue
except Exception as e:
print(f"Error reading {self.dicom_paths[0]}: {str(e)}")
self.dicom_paths.pop(0)
self.load_dicom()
# next image
def next_image(self):
if self.index < len(self.dicom_paths) - 1:
self.index += 1
self.update_image()
# prev image
def prev_image(self):
if self.index > 0:
self.index -= 1
self.update_image()
# update slice image
def update_image(self):
reslice = vtk.vtkImageReslice()
reslice.SetInputData(self.image_data)
reslice.SetOutputDimensionality(2)
reslice.SetResliceAxesOrigin(0, 0, self.index)
reslice.Update()
self.actor.GetMapper().SetInputData(reslice.GetOutput())
self.actor.Modified()
self.update_landmarks_visibility()
# actor current properties for window/level and constrast on window
def get_image_property(self):
return self.actor.GetProperty()
# get actor center
def get_center(self):
return self.center
''' Landmark State Treatment '''
def add_landmark(self, position):
landmark = Landmark(position)
landmark.circleActor.SetUserTransform(self.actor.GetUserTransform()) # Apply the same transformation
self.ren.AddActor(landmark.circleActor)
slice_index = position[2] # z is the slice index, and also the key
if slice_index not in self.landmarks:
self.landmarks[slice_index] = []
self.landmarks[slice_index].append({
"Index": self.landmark_count,
"Position": landmark
})
self.landmark_count+=1
self.update_landmarks_visibility()
self.ren.GetRenderWindow().Render()
def remove_landmark(self):
if self.index in self.landmarks and len(self.landmarks[self.index]) > 0:
removed_landmark = self.landmarks[self.index].pop()
self.landmark_count-=1
# remove the landmark actor from the renderer
self.ren.RemoveActor(removed_landmark["Position"].circleActor)
self.ren.GetRenderWindow().Render()
return True, (removed_landmark["Position"].position)
return False,()
# Update of the points/landmark visibility, landmark marked on a slice are specific only to that slice
def update_landmarks_visibility(self):
for slice_index, landmarks in self.landmarks.items():
for landmark in landmarks:
# if the slice index is the current index, the landmark should be visible
# else it should be invisible
landmark['Position'].circleActor.SetVisibility(slice_index == self.index)
# Saves the landmarks to the supposed files
def save_landmarks(self):
json_file = os.path.join(self.dicom_dir, f"{self.sequence}.json")
print('-> Landmarks saved for json file: ', json_file)
# dictionary where each key-value pair represents a slice/frame
slice_data_dict = {
slice_id: {
"Dataset": self.dataset_type,
"Individual": self.individual,
"Knee": self.knee,
"Sequence": self.sequence,
"Slice": slice_id,
"#Landmarks": len(lm_list),
"Landmarks": [
{
"Index" : lm["Index"],
"Position": (lm["Position"].position[0], lm["Position"].position[1]) # change the position for the image processing coordinates
} for lm in lm_list
],
}
for slice_id, lm_list in self.landmarks.items() if lm_list
}
try:
with open(json_file, "w") as f:
json.dump(slice_data_dict, f, ensure_ascii=False, indent=4)
except Exception as e:
print(f"Failed to save data to JSON due to: {e}")
return 0
combined_landmarks = []
for slice_id, lm_list in self.landmarks.items():
for lm in lm_list:
combined_landmarks.append((lm["Position"].position[0], lm["Position"].position[1], lm["Index"], slice_id))
#sort the landmarks to be on the correct order
combined_landmarks = sorted(combined_landmarks, key=lambda x:x[2])
excel_data_dict = {
"Dataset": self.dataset_type,
"Individual": self.individual,
"Knee": self.knee,
"Sequence": self.sequence,
"Landmarks": combined_landmarks
}
try:
excel_file_path = excel_paths[self.dataset_type]
subset_df = pd.read_excel(excel_file_path)
# Check if sequence exists in the DataFrame and remove rows
matching_rows = (subset_df["Individual"] == self.individual) & (subset_df["Sequence"] == self.sequence) & (subset_df["Knee"] == self.knee)
if matching_rows.any():
subset_df = subset_df[~matching_rows]
excel_data_dict["Landmarks"] = str(excel_data_dict["Landmarks"])
subset_df = subset_df.append(excel_data_dict, ignore_index=True)
subset_df.to_excel(excel_file_path, index=False)
except Exception as e:
print(f"Failed to save data to Excel due to: {e}")
# reinstate the empty json file
with open(json_file,'w') as f:
json.dump({},f,indent=4)
return 0
self.status = 1
# remove slices where there are empty lists
self.landmarks = {k: v for k,v in self.landmarks.items() if v}
return 1
# Clear all landmarks marked in every slice and if status 1 clear json
def clear_landmarks(self):
self.landmark_count = 1
for slice_index, landmarks in self.landmarks.items():
for landmark in landmarks:
self.ren.RemoveActor(landmark["Position"].circleActor)
self.landmarks.clear()
self.ren.GetRenderWindow().Render()
if self.status == 1:
json_file = os.path.join(self.dicom_dir, f"{self.sequence}.json")
with open(json_file,'w') as f:
json.dump({},f,indent=4)
self.status = 0
excel_file_path = excel_paths[self.dataset_type]
subset_df = pd.read_excel(excel_file_path)
# its not removing the landmarks
mask = (subset_df["Individual"] == int(self.individual)) & (subset_df["Sequence"] == self.sequence) & (subset_df["Knee"] == self.knee)
subset_df = subset_df[~(mask)]
subset_df.to_excel(excel_file_path, index=False)
return 1
return 0
# in case of json file exists - only load the landmarks into the images
def load_landmarks_from_json(self, json_file):
with open(json_file, 'r') as file:
slice_data_dict = json.load(file)
for slice_id, data in slice_data_dict.items():
slice_index = int(slice_id)
landmarks = data["Landmarks"]
for landmark in landmarks:
position = [landmark["Position"][0], landmark["Position"][1], slice_index]
self.add_landmark(position)
self.landmark_count = sum(len(landmarks) for landmarks in self.landmarks.values())
self.update_landmarks_visibility()
# Get properties from any images sequence path
def extract_components(self,dicom_dir):
# Define the regex pattern
pattern = r"(?P<Dataset>DATASET_\w+)[\\/](?P<Individual>\d+)[\\/](?P<Knee>LEFT|RIGHT)[\\/](?P<Sequence>[\w-]+)[\\/]*"
match = re.search(pattern, dicom_dir)
# matched groups as a dictionary if true
if match:
# example: {'Dataset': 'DATASET_AXIAL', 'Individual': '1', 'Knee': 'LEFT', 'Sequence': 'pd_tse_fs_tra_320_3'}
self.dataset_type = match.groupdict().get('Dataset')
self.individual = match.groupdict().get('Individual')
self.knee = match.groupdict().get('Knee')
self.sequence = match.groupdict().get('Sequence')
self.max_count = max_landmarks[self.dataset_type]
else:
print('Impossible to parse the string and get the properties of dicom images')