|
a |
|
b/DataPreparation.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "code", |
|
|
5 |
"execution_count": null, |
|
|
6 |
"metadata": {}, |
|
|
7 |
"outputs": [], |
|
|
8 |
"source": [ |
|
|
9 |
"import os\n", |
|
|
10 |
"import re\n", |
|
|
11 |
"import numpy as np\n", |
|
|
12 |
"from rt_utils import RTStructBuilder\n", |
|
|
13 |
"import matplotlib.pyplot as plt\n", |
|
|
14 |
"import pydicom \n", |
|
|
15 |
"from PIL import Image\n", |
|
|
16 |
"CT_PATH = './CT_Slices'" |
|
|
17 |
] |
|
|
18 |
}, |
|
|
19 |
{ |
|
|
20 |
"cell_type": "code", |
|
|
21 |
"execution_count": null, |
|
|
22 |
"metadata": {}, |
|
|
23 |
"outputs": [], |
|
|
24 |
"source": [ |
|
|
25 |
"def transform_to_hu(medical_image):\n", |
|
|
26 |
" intercept = medical_image.RescaleIntercept\n", |
|
|
27 |
" slope = medical_image.RescaleSlope\n", |
|
|
28 |
" image = medical_image.pixel_array\n", |
|
|
29 |
" hu_image = image * slope + intercept\n", |
|
|
30 |
"\n", |
|
|
31 |
" return hu_image\n", |
|
|
32 |
"\n", |
|
|
33 |
"def apply_windowing(ds, window_center, window_width):\n", |
|
|
34 |
" # Load the DICOM file\n", |
|
|
35 |
" \n", |
|
|
36 |
" # Get the pixel data and rescale to Hounsfield units (HU)\n", |
|
|
37 |
" pixel_array = ds.pixel_array.astype(np.float32)\n", |
|
|
38 |
" intercept = ds.RescaleIntercept\n", |
|
|
39 |
" slope = ds.RescaleSlope\n", |
|
|
40 |
" hu_array = pixel_array * slope + intercept\n", |
|
|
41 |
" \n", |
|
|
42 |
" # Apply windowing\n", |
|
|
43 |
" window_min = window_center - (window_width / 2)\n", |
|
|
44 |
" window_max = window_center + (window_width / 2)\n", |
|
|
45 |
" windowed_array = np.clip(hu_array, window_min, window_max)\n", |
|
|
46 |
" \n", |
|
|
47 |
" # Normalize the windowed array to [0, 1]\n", |
|
|
48 |
" normalized_array = (windowed_array - window_min) / (window_max - window_min)\n", |
|
|
49 |
" \n", |
|
|
50 |
" return normalized_array\n", |
|
|
51 |
"\n", |
|
|
52 |
"\n", |
|
|
53 |
"def set_window(img, hu=[-800.,1200.]):\n", |
|
|
54 |
" window = np.array(hu)\n", |
|
|
55 |
" newimg = (img-window[0]) / (window[1]-window[0])\n", |
|
|
56 |
" newimg[newimg < 0] = 0\n", |
|
|
57 |
" newimg[newimg > 1] = 1\n", |
|
|
58 |
" newimg = (newimg * 255).astype('uint8')\n", |
|
|
59 |
" return newimg\n", |
|
|
60 |
"\n", |
|
|
61 |
"\n", |
|
|
62 |
"def zero_center(hu_image):\n", |
|
|
63 |
" hu_image = hu_image - np.mean(hu_image)\n", |
|
|
64 |
" return hu_image" |
|
|
65 |
] |
|
|
66 |
}, |
|
|
67 |
{ |
|
|
68 |
"cell_type": "code", |
|
|
69 |
"execution_count": null, |
|
|
70 |
"metadata": {}, |
|
|
71 |
"outputs": [], |
|
|
72 |
"source": [ |
|
|
73 |
"def rename_folders(path):\n", |
|
|
74 |
" for patient in os.listdir(path):\n", |
|
|
75 |
" old_path = os.path.join(path, patient)\n", |
|
|
76 |
" new_name = re.sub(r'[^0-9]','',patient)\n", |
|
|
77 |
" new_path = os.path.join(path, new_name)\n", |
|
|
78 |
" os.rename(old_path, new_name)\n", |
|
|
79 |
" \n", |
|
|
80 |
"rename_folders(CT_PATH)" |
|
|
81 |
] |
|
|
82 |
}, |
|
|
83 |
{ |
|
|
84 |
"cell_type": "code", |
|
|
85 |
"execution_count": null, |
|
|
86 |
"metadata": {}, |
|
|
87 |
"outputs": [], |
|
|
88 |
"source": [ |
|
|
89 |
"DATA_PATH = './data/'" |
|
|
90 |
] |
|
|
91 |
}, |
|
|
92 |
{ |
|
|
93 |
"cell_type": "code", |
|
|
94 |
"execution_count": null, |
|
|
95 |
"metadata": {}, |
|
|
96 |
"outputs": [], |
|
|
97 |
"source": [ |
|
|
98 |
"import shutil\n", |
|
|
99 |
"def create_ds_structure(path):\n", |
|
|
100 |
" for patient in os.listdir(path):\n", |
|
|
101 |
" p_path = os.path.join(path, patient)\n", |
|
|
102 |
" dicom_path = os.path.join(p_path, 'dicom')\n", |
|
|
103 |
" r_path = os.path.join(p_path, 'r')\n", |
|
|
104 |
" if not os.path.exists(dicom_path):\n", |
|
|
105 |
" os.mkdir(dicom_path)\n", |
|
|
106 |
" if not os.path.exists(r_path):\n", |
|
|
107 |
" os.mkdir(r_path)\n", |
|
|
108 |
" for file in os.listdir(p_path):\n", |
|
|
109 |
" file_path = os.path.join(p_path, file)\n", |
|
|
110 |
" if os.path.isfile(file_path):\n", |
|
|
111 |
" if(file[0:2]=='CT'):\n", |
|
|
112 |
" new_path = os.path.join(dicom_path, file)\n", |
|
|
113 |
" shutil.move(file_path, new_path)\n", |
|
|
114 |
" if(file[0:2]=='RS'):\n", |
|
|
115 |
" new_path = os.path.join(r_path, file)\n", |
|
|
116 |
" shutil.move(file_path, new_path)\n", |
|
|
117 |
"\n", |
|
|
118 |
" \n", |
|
|
119 |
"\n", |
|
|
120 |
"create_ds_structure(DATA_PATH) \n", |
|
|
121 |
"\n", |
|
|
122 |
"\n" |
|
|
123 |
] |
|
|
124 |
}, |
|
|
125 |
{ |
|
|
126 |
"cell_type": "code", |
|
|
127 |
"execution_count": null, |
|
|
128 |
"metadata": {}, |
|
|
129 |
"outputs": [], |
|
|
130 |
"source": [ |
|
|
131 |
"\n", |
|
|
132 |
"\n", |
|
|
133 |
"# Load existing RT Struct. Requires the series path and existing RT Struct path\n", |
|
|
134 |
"rtstruct = RTStructBuilder.create_from(\n", |
|
|
135 |
" dicom_series_path=\"./data/01/dicom\", \n", |
|
|
136 |
" rt_struct_path=\"./data/01/r/RS.1.2.410.200018.1001.1.3.89143765.2.20151123111228992.dcm\"\n", |
|
|
137 |
")\n", |
|
|
138 |
"\n", |
|
|
139 |
"# View all of the ROI names from within the image\n", |
|
|
140 |
"print(rtstruct.get_roi_names())\n", |
|
|
141 |
"ds = []\n", |
|
|
142 |
"for image in os.listdir('./data/01/dicom/'):\n", |
|
|
143 |
" img_path = os.path.join('./data/01/dicom/', image)\n", |
|
|
144 |
" ds.append(pydicom.dcmread(img_path))\n", |
|
|
145 |
"\n", |
|
|
146 |
"\n", |
|
|
147 |
"\n", |
|
|
148 |
"rtstruct" |
|
|
149 |
] |
|
|
150 |
}, |
|
|
151 |
{ |
|
|
152 |
"cell_type": "code", |
|
|
153 |
"execution_count": null, |
|
|
154 |
"metadata": {}, |
|
|
155 |
"outputs": [], |
|
|
156 |
"source": [ |
|
|
157 |
"img_idx = 33\n", |
|
|
158 |
"mask_idx = 44 -img_idx\n", |
|
|
159 |
"ds_img = apply_windowing(ds[img_idx], 35, 350)\n", |
|
|
160 |
"# Loading the 3D Mask from within the RT Struct\n", |
|
|
161 |
"mask_3d = rtstruct.get_roi_mask_by_name(\"heart\")\n", |
|
|
162 |
"\n", |
|
|
163 |
"\n", |
|
|
164 |
"fliped = np.flip(mask_3d, axis=2)\n", |
|
|
165 |
"# Display one slice of the region\n", |
|
|
166 |
"first_mask_slice = fliped[:, :, img_idx]\n", |
|
|
167 |
"plt.figure(figsize = (5,5))\n", |
|
|
168 |
"plt.imshow(first_mask_slice)\n", |
|
|
169 |
"#plt.imshow(ds_img, cmap='gray',alpha=0.9)" |
|
|
170 |
] |
|
|
171 |
}, |
|
|
172 |
{ |
|
|
173 |
"cell_type": "code", |
|
|
174 |
"execution_count": null, |
|
|
175 |
"metadata": {}, |
|
|
176 |
"outputs": [], |
|
|
177 |
"source": [ |
|
|
178 |
"IMG_PATH = './2d_data/images/'\n", |
|
|
179 |
"MASKS_PATH = './2d_data/masks/'\n", |
|
|
180 |
"def create_2d_dataset(path, img_path_save, mask_path_save):\n", |
|
|
181 |
" for patient in os.listdir(path):\n", |
|
|
182 |
" p_path = os.path.join(path, patient)\n", |
|
|
183 |
"\n", |
|
|
184 |
" dicom_path = os.path.join(p_path, 'dicom')\n", |
|
|
185 |
" \n", |
|
|
186 |
" r_path = os.path.join(p_path, 'r')\n", |
|
|
187 |
"\n", |
|
|
188 |
" r_file = os.listdir(r_path)[0]\n", |
|
|
189 |
" r_file = os.path.join(r_path, r_file)\n", |
|
|
190 |
"\n", |
|
|
191 |
"\n", |
|
|
192 |
" try: \n", |
|
|
193 |
" rtstruct = RTStructBuilder.create_from(\n", |
|
|
194 |
" dicom_series_path=dicom_path, \n", |
|
|
195 |
" rt_struct_path=r_file\n", |
|
|
196 |
" ) \n", |
|
|
197 |
" mask_3d = rtstruct.get_roi_mask_by_name(\"heart\")\n", |
|
|
198 |
" fliped = np.flip(mask_3d, axis=2)\n", |
|
|
199 |
" idx = 0\n", |
|
|
200 |
" for dcm_file in os.listdir(dicom_path):\n", |
|
|
201 |
" dcm_file_path = os.path.join(dicom_path, dcm_file)\n", |
|
|
202 |
" new_dcm_file = os.path.join(img_path_save,(patient+'_'+dcm_file))\n", |
|
|
203 |
"\n", |
|
|
204 |
" mask_filename = new_dcm_file.replace('.dcm', '_mask.png')\n", |
|
|
205 |
" mask_filename = mask_filename.replace('images', 'masks')\n", |
|
|
206 |
" #print(mask_filename)\n", |
|
|
207 |
" mask_tosave = Image.fromarray(fliped[:,:,idx])\n", |
|
|
208 |
" if not os.path.exists(new_dcm_file):\n", |
|
|
209 |
" shutil.copy(dcm_file_path, new_dcm_file)\n", |
|
|
210 |
" if not os.path.exists(mask_filename):\n", |
|
|
211 |
" mask_tosave.save(mask_filename)\n", |
|
|
212 |
"\n", |
|
|
213 |
" idx+=1\n", |
|
|
214 |
" except: \n", |
|
|
215 |
" print(f'Skipping patient {patient}')\n", |
|
|
216 |
" \n", |
|
|
217 |
" \n", |
|
|
218 |
"\n", |
|
|
219 |
"create_2d_dataset(DATA_PATH, IMG_PATH, MASKS_PATH)" |
|
|
220 |
] |
|
|
221 |
}, |
|
|
222 |
{ |
|
|
223 |
"cell_type": "code", |
|
|
224 |
"execution_count": null, |
|
|
225 |
"metadata": {}, |
|
|
226 |
"outputs": [], |
|
|
227 |
"source": [] |
|
|
228 |
} |
|
|
229 |
], |
|
|
230 |
"metadata": { |
|
|
231 |
"kernelspec": { |
|
|
232 |
"display_name": "DL", |
|
|
233 |
"language": "python", |
|
|
234 |
"name": "python3" |
|
|
235 |
}, |
|
|
236 |
"language_info": { |
|
|
237 |
"codemirror_mode": { |
|
|
238 |
"name": "ipython", |
|
|
239 |
"version": 3 |
|
|
240 |
}, |
|
|
241 |
"file_extension": ".py", |
|
|
242 |
"mimetype": "text/x-python", |
|
|
243 |
"name": "python", |
|
|
244 |
"nbconvert_exporter": "python", |
|
|
245 |
"pygments_lexer": "ipython3", |
|
|
246 |
"version": "3.10.11" |
|
|
247 |
}, |
|
|
248 |
"orig_nbformat": 4 |
|
|
249 |
}, |
|
|
250 |
"nbformat": 4, |
|
|
251 |
"nbformat_minor": 2 |
|
|
252 |
} |