Skip to content

Commit 2bb5311

Browse files
authored
21 add DICOM format in the LoadImage tutorial (#23)
* [DLMED] add DICOM format in the tutorial Signed-off-by: Nic Ma <[email protected]> * [DLMED] update according to comments Signed-off-by: Nic Ma <[email protected]> * [DLMED] update ITK shape Signed-off-by: Nic Ma <[email protected]>
1 parent 4d93de3 commit 2bb5311

File tree

1 file changed

+111
-49
lines changed

1 file changed

+111
-49
lines changed

load_medical_images.ipynb

Lines changed: 111 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,11 @@
6666
},
6767
{
6868
"cell_type": "code",
69-
"execution_count": 1,
69+
"execution_count": null,
7070
"metadata": {
7171
"tags": []
7272
},
73-
"outputs": [
74-
{
75-
"name": "stdout",
76-
"output_type": "stream",
77-
"text": [
78-
"MONAI version: 0.2.0\n",
79-
"Python version: 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 19:07:31) [GCC 7.3.0]\n",
80-
"Numpy version: 1.18.1\n",
81-
"Pytorch version: 1.6.0\n",
82-
"\n",
83-
"Optional dependencies:\n",
84-
"Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.\n",
85-
"Nibabel version: 3.1.1\n",
86-
"scikit-image version: 0.15.0\n",
87-
"Pillow version: 7.2.0\n",
88-
"Tensorboard version: 2.1.0\n",
89-
"\n",
90-
"For details about installing the optional dependencies, please visit:\n",
91-
" https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies\n",
92-
"\n"
93-
]
94-
}
95-
],
73+
"outputs": [],
9674
"source": [
9775
"# Copyright 2020 MONAI Consortium\n",
9876
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
@@ -133,14 +111,14 @@
133111
"cell_type": "markdown",
134112
"metadata": {},
135113
"source": [
136-
"## Load image with default image reader\n",
114+
"## Load Nifti image with default image reader\n",
137115
"MONAI leverages `ITK` as the default image reader, it can support most of the common medical image formats.\n",
138116
"More details, please check: https://github.com/InsightSoftwareConsortium/ITK/tree/master/Modules/IO"
139117
]
140118
},
141119
{
142120
"cell_type": "code",
143-
"execution_count": 8,
121+
"execution_count": 2,
144122
"metadata": {
145123
"tags": []
146124
},
@@ -149,23 +127,23 @@
149127
"name": "stdout",
150128
"output_type": "stream",
151129
"text": [
152-
"image data shape:(128, 128, 128)\n",
153-
"meta data:{'ITK_FileNotes': '', 'aux_file': '', 'bitpix': '64', 'cal_max': '0', 'cal_min': '0', 'datatype': '64', 'descrip': '', 'dim[0]': '3', 'dim[1]': '128', 'dim[2]': '128', 'dim[3]': '128', 'dim[4]': '1', 'dim[5]': '1', 'dim[6]': '1', 'dim[7]': '1', 'dim_info': '0', 'intent_code': '0', 'intent_name': '', 'intent_p1': '0', 'intent_p2': '0', 'intent_p3': '0', 'nifti_type': '1', 'pixdim[0]': '0', 'pixdim[1]': '1', 'pixdim[2]': '1', 'pixdim[3]': '1', 'pixdim[4]': '0', 'pixdim[5]': '0', 'pixdim[6]': '0', 'pixdim[7]': '0', 'qform_code': '1', 'qform_code_name': 'NIFTI_XFORM_SCANNER_ANAT', 'qoffset_x': '-0', 'qoffset_y': '-0', 'qoffset_z': '0', 'quatern_b': '0', 'quatern_c': '0', 'quatern_d': '1', 'scl_inter': '0', 'scl_slope': '1', 'sform_code': '0', 'sform_code_name': 'NIFTI_XFORM_UNKNOWN', 'slice_code': '0', 'slice_duration': '0', 'slice_end': '0', 'slice_start': '0', 'srow_x': '0 0 0 0', 'srow_y': '0 0 0 0', 'srow_z': '0 0 0 0', 'toffset': '0', 'vox_offset': '352', 'xyzt_units': '2', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n",
130+
"image data shape:(64, 128, 96)\n",
131+
"meta data:{'ITK_FileNotes': '', 'aux_file': '', 'bitpix': '64', 'cal_max': '0', 'cal_min': '0', 'datatype': '64', 'descrip': '', 'dim[0]': '3', 'dim[1]': '96', 'dim[2]': '128', 'dim[3]': '64', 'dim[4]': '1', 'dim[5]': '1', 'dim[6]': '1', 'dim[7]': '1', 'dim_info': '0', 'intent_code': '0', 'intent_name': '', 'intent_p1': '0', 'intent_p2': '0', 'intent_p3': '0', 'nifti_type': '1', 'pixdim[0]': '0', 'pixdim[1]': '1', 'pixdim[2]': '1', 'pixdim[3]': '1', 'pixdim[4]': '0', 'pixdim[5]': '0', 'pixdim[6]': '0', 'pixdim[7]': '0', 'qform_code': '1', 'qform_code_name': 'NIFTI_XFORM_SCANNER_ANAT', 'qoffset_x': '-0', 'qoffset_y': '-0', 'qoffset_z': '0', 'quatern_b': '0', 'quatern_c': '0', 'quatern_d': '1', 'scl_inter': '0', 'scl_slope': '1', 'sform_code': '0', 'sform_code_name': 'NIFTI_XFORM_UNKNOWN', 'slice_code': '0', 'slice_duration': '0', 'slice_end': '0', 'slice_start': '0', 'srow_x': '0 0 0 0', 'srow_y': '0 0 0 0', 'srow_z': '0 0 0 0', 'toffset': '0', 'vox_offset': '352', 'xyzt_units': '2', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n",
154132
" [0., 1., 0.],\n",
155133
" [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n",
156134
" [0., 1., 0., 0.],\n",
157135
" [0., 0., 1., 0.],\n",
158136
" [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n",
159137
" [0., 1., 0., 0.],\n",
160138
" [0., 0., 1., 0.],\n",
161-
" [0., 0., 0., 1.]]), 'spatial_shape': [128, 128, 128], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.nii.gz'}\n"
139+
" [0., 0., 0., 1.]]), 'spatial_shape': [64, 128, 96], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.nii.gz'}\n"
162140
]
163141
}
164142
],
165143
"source": [
166144
"# generate 3D test images\n",
167145
"tempdir = tempfile.mkdtemp()\n",
168-
"test_image = np.random.rand(128, 128, 128)\n",
146+
"test_image = np.random.rand(64, 128, 96)\n",
169147
"filename = os.path.join(tempdir, \"test_image.nii.gz\")\n",
170148
"itk_np_view = itk.image_view_from_array(test_image)\n",
171149
"itk.imwrite(itk_np_view, filename)\n",
@@ -179,31 +157,31 @@
179157
"cell_type": "markdown",
180158
"metadata": {},
181159
"source": [
182-
"## Load a list of images and stack as 1 training item\n",
160+
"## Load a list of Nifti images and stack as 1 training item\n",
183161
"Loading a list of files, stack them together and add a new dimension as first dimension.\n",
184162
"\n",
185163
"And use the meta data of the first image to represent the stacked result."
186164
]
187165
},
188166
{
189167
"cell_type": "code",
190-
"execution_count": 9,
168+
"execution_count": 3,
191169
"metadata": {},
192170
"outputs": [
193171
{
194172
"name": "stdout",
195173
"output_type": "stream",
196174
"text": [
197-
"image data shape:(3, 128, 128, 128)\n",
198-
"meta data:{'ITK_FileNotes': '', 'aux_file': '', 'bitpix': '64', 'cal_max': '0', 'cal_min': '0', 'datatype': '64', 'descrip': '', 'dim[0]': '3', 'dim[1]': '128', 'dim[2]': '128', 'dim[3]': '128', 'dim[4]': '1', 'dim[5]': '1', 'dim[6]': '1', 'dim[7]': '1', 'dim_info': '0', 'intent_code': '0', 'intent_name': '', 'intent_p1': '0', 'intent_p2': '0', 'intent_p3': '0', 'nifti_type': '1', 'pixdim[0]': '0', 'pixdim[1]': '1', 'pixdim[2]': '1', 'pixdim[3]': '1', 'pixdim[4]': '0', 'pixdim[5]': '0', 'pixdim[6]': '0', 'pixdim[7]': '0', 'qform_code': '1', 'qform_code_name': 'NIFTI_XFORM_SCANNER_ANAT', 'qoffset_x': '-0', 'qoffset_y': '-0', 'qoffset_z': '0', 'quatern_b': '0', 'quatern_c': '0', 'quatern_d': '1', 'scl_inter': '0', 'scl_slope': '1', 'sform_code': '0', 'sform_code_name': 'NIFTI_XFORM_UNKNOWN', 'slice_code': '0', 'slice_duration': '0', 'slice_end': '0', 'slice_start': '0', 'srow_x': '0 0 0 0', 'srow_y': '0 0 0 0', 'srow_z': '0 0 0 0', 'toffset': '0', 'vox_offset': '352', 'xyzt_units': '2', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n",
175+
"image data shape:(3, 64, 128, 96)\n",
176+
"meta data:{'ITK_FileNotes': '', 'aux_file': '', 'bitpix': '64', 'cal_max': '0', 'cal_min': '0', 'datatype': '64', 'descrip': '', 'dim[0]': '3', 'dim[1]': '96', 'dim[2]': '128', 'dim[3]': '64', 'dim[4]': '1', 'dim[5]': '1', 'dim[6]': '1', 'dim[7]': '1', 'dim_info': '0', 'intent_code': '0', 'intent_name': '', 'intent_p1': '0', 'intent_p2': '0', 'intent_p3': '0', 'nifti_type': '1', 'pixdim[0]': '0', 'pixdim[1]': '1', 'pixdim[2]': '1', 'pixdim[3]': '1', 'pixdim[4]': '0', 'pixdim[5]': '0', 'pixdim[6]': '0', 'pixdim[7]': '0', 'qform_code': '1', 'qform_code_name': 'NIFTI_XFORM_SCANNER_ANAT', 'qoffset_x': '-0', 'qoffset_y': '-0', 'qoffset_z': '0', 'quatern_b': '0', 'quatern_c': '0', 'quatern_d': '1', 'scl_inter': '0', 'scl_slope': '1', 'sform_code': '0', 'sform_code_name': 'NIFTI_XFORM_UNKNOWN', 'slice_code': '0', 'slice_duration': '0', 'slice_end': '0', 'slice_start': '0', 'srow_x': '0 0 0 0', 'srow_y': '0 0 0 0', 'srow_z': '0 0 0 0', 'toffset': '0', 'vox_offset': '352', 'xyzt_units': '2', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n",
199177
" [0., 1., 0.],\n",
200178
" [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n",
201179
" [0., 1., 0., 0.],\n",
202180
" [0., 0., 1., 0.],\n",
203181
" [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n",
204182
" [0., 1., 0., 0.],\n",
205183
" [0., 0., 1., 0.],\n",
206-
" [0., 0., 0., 1.]]), 'spatial_shape': [128, 128, 128], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.nii.gz'}\n"
184+
" [0., 0., 0., 1.]]), 'spatial_shape': [64, 128, 96], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.nii.gz'}\n"
207185
]
208186
}
209187
],
@@ -219,6 +197,90 @@
219197
"print(f\"meta data:{meta}\")"
220198
]
221199
},
200+
{
201+
"cell_type": "markdown",
202+
"metadata": {},
203+
"source": [
204+
"## Load 3D image in DICOM format"
205+
]
206+
},
207+
{
208+
"cell_type": "code",
209+
"execution_count": 4,
210+
"metadata": {},
211+
"outputs": [
212+
{
213+
"name": "stdout",
214+
"output_type": "stream",
215+
"text": [
216+
"image data shape:(64, 128, 96)\n",
217+
"meta data:{'0008|0016': '1.2.840.10008.5.1.4.1.1.7.2', '0008|0018': '1.2.826.0.1.3680043.2.1125.1.64732409224407717340688313759116819', '0008|0020': '20200917', '0008|0030': '012951.461675 ', '0008|0050': '', '0008|0060': 'OT', '0008|0090': '', '0010|0010': '', '0010|0020': '', '0010|0030': '', '0010|0040': '', '0020|000d': '1.2.826.0.1.3680043.2.1125.1.22199909824554379284164927198357157', '0020|000e': '1.2.826.0.1.3680043.2.1125.1.64330868579047092348166172965815757', '0020|0010': '', '0020|0011': '', '0020|0013': '', '0020|0052': '1.2.826.0.1.3680043.2.1125.1.36887462277578362563842268362959985', '0028|0002': '1', '0028|0004': 'MONOCHROME2 ', '0028|0008': '64', '0028|0009': '(5200,9230)', '0028|0010': '128', '0028|0011': '96', '0028|0100': '8', '0028|0101': '8', '0028|0102': '7', '0028|0103': '0', '0028|1052': '0 ', '0028|1053': '1 ', '0028|1054': 'US', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n",
218+
" [0., 1., 0.],\n",
219+
" [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n",
220+
" [0., 1., 0., 0.],\n",
221+
" [0., 0., 1., 0.],\n",
222+
" [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n",
223+
" [0., 1., 0., 0.],\n",
224+
" [0., 0., 1., 0.],\n",
225+
" [0., 0., 0., 1.]]), 'spatial_shape': [64, 128, 96], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.dcm'}\n"
226+
]
227+
}
228+
],
229+
"source": [
230+
"filename = os.path.join(tempdir, \"test_image.dcm\")\n",
231+
"dcm_image = np.random.randint(256, size=(64, 128, 96)).astype(np.uint8())\n",
232+
"itk_np_view = itk.image_view_from_array(dcm_image)\n",
233+
"itk.imwrite(itk_np_view, filename)\n",
234+
"data, meta = LoadImage()(filename)\n",
235+
"\n",
236+
"print(f\"image data shape:{data.shape}\")\n",
237+
"print(f\"meta data:{meta}\")"
238+
]
239+
},
240+
{
241+
"cell_type": "markdown",
242+
"metadata": {},
243+
"source": [
244+
"## Load a list of DICOM images and stack as 1 training item\n",
245+
"Loading a list of files, stack them together and add a new dimension as first dimension.\n",
246+
"\n",
247+
"And use the meta data of the first image to represent the stacked result."
248+
]
249+
},
250+
{
251+
"cell_type": "code",
252+
"execution_count": 5,
253+
"metadata": {},
254+
"outputs": [
255+
{
256+
"name": "stdout",
257+
"output_type": "stream",
258+
"text": [
259+
"image data shape:(3, 64, 128, 96)\n",
260+
"meta data:{'0008|0016': '1.2.840.10008.5.1.4.1.1.7.2', '0008|0018': '1.2.826.0.1.3680043.2.1125.1.55124182629917658983046437541692429', '0008|0020': '20200917', '0008|0030': '012953.377970 ', '0008|0050': '', '0008|0060': 'OT', '0008|0090': '', '0010|0010': '', '0010|0020': '', '0010|0030': '', '0010|0040': '', '0020|000d': '1.2.826.0.1.3680043.2.1125.1.54570928137383298712421968711546946', '0020|000e': '1.2.826.0.1.3680043.2.1125.1.79105954593572939973262028207196688', '0020|0010': '', '0020|0011': '', '0020|0013': '', '0020|0052': '1.2.826.0.1.3680043.2.1125.1.37569814707528773722727172695078933', '0028|0002': '1', '0028|0004': 'MONOCHROME2 ', '0028|0008': '64', '0028|0009': '(5200,9230)', '0028|0010': '128', '0028|0011': '96', '0028|0100': '8', '0028|0101': '8', '0028|0102': '7', '0028|0103': '0', '0028|1052': '0 ', '0028|1053': '1 ', '0028|1054': 'US', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n",
261+
" [0., 1., 0.],\n",
262+
" [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n",
263+
" [0., 1., 0., 0.],\n",
264+
" [0., 0., 1., 0.],\n",
265+
" [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n",
266+
" [0., 1., 0., 0.],\n",
267+
" [0., 0., 1., 0.],\n",
268+
" [0., 0., 0., 1.]]), 'spatial_shape': [64, 128, 96], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.dcm'}\n"
269+
]
270+
}
271+
],
272+
"source": [
273+
"filenames = [\"test_image.dcm\", \"test_image2.dcm\", \"test_image3.dcm\"]\n",
274+
"for i, name in enumerate(filenames):\n",
275+
" filenames[i] = os.path.join(tempdir, name)\n",
276+
" itk_np_view = itk.image_view_from_array(dcm_image)\n",
277+
" itk.imwrite(itk_np_view, filenames[i])\n",
278+
"data, meta = LoadImage()(filenames)\n",
279+
"\n",
280+
"print(f\"image data shape:{data.shape}\")\n",
281+
"print(f\"meta data:{meta}\")"
282+
]
283+
},
222284
{
223285
"cell_type": "markdown",
224286
"metadata": {},
@@ -228,7 +290,7 @@
228290
},
229291
{
230292
"cell_type": "code",
231-
"execution_count": 12,
293+
"execution_count": 6,
232294
"metadata": {
233295
"tags": []
234296
},
@@ -237,18 +299,18 @@
237299
"name": "stdout",
238300
"output_type": "stream",
239301
"text": [
240-
"image data shape:(256, 256)\n",
302+
"image data shape:(128, 256)\n",
241303
"meta data:{'origin': array([0., 0.]), 'spacing': array([1., 1.]), 'direction': array([[1., 0.],\n",
242304
" [0., 1.]]), 'original_affine': array([[1., 0., 0.],\n",
243305
" [0., 1., 0.],\n",
244306
" [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n",
245307
" [0., 1., 0.],\n",
246-
" [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.png'}\n"
308+
" [0., 0., 1.]]), 'spatial_shape': [128, 256], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.png'}\n"
247309
]
248310
}
249311
],
250312
"source": [
251-
"test_image = np.random.randint(0, 256, size=[256, 256])\n",
313+
"test_image = np.random.randint(0, 256, size=[128, 256])\n",
252314
"filename = os.path.join(tempdir, \"test_image.png\")\n",
253315
"Image.fromarray(test_image.astype(\"uint8\")).save(filename)\n",
254316
"data, meta = LoadImage()(filename)\n",
@@ -272,26 +334,26 @@
272334
},
273335
{
274336
"cell_type": "code",
275-
"execution_count": 17,
337+
"execution_count": 7,
276338
"metadata": {},
277339
"outputs": [
278340
{
279341
"name": "stdout",
280342
"output_type": "stream",
281343
"text": [
282-
"image data shape:(256, 256)\n",
344+
"image data shape:(128, 256)\n",
283345
"meta data:{'origin': array([0., 0.]), 'spacing': array([1., 1.]), 'direction': array([[1., 0.],\n",
284346
" [0., 1.]]), 'original_affine': array([[1., 0., 0.],\n",
285347
" [0., 1., 0.],\n",
286348
" [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n",
287349
" [0., 1., 0.],\n",
288-
" [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.png'}\n"
350+
" [0., 0., 1.]]), 'spatial_shape': [128, 256], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.png'}\n"
289351
]
290352
}
291353
],
292354
"source": [
293355
"loader = LoadImage()\n",
294-
"loader.register(ITKReader(c_order_axis_indexing=True))\n",
356+
"loader.register(ITKReader())\n",
295357
"data, meta = loader(filename)\n",
296358
"\n",
297359
"print(f\"image data shape:{data.shape}\")\n",
@@ -310,15 +372,15 @@
310372
},
311373
{
312374
"cell_type": "code",
313-
"execution_count": 18,
375+
"execution_count": 8,
314376
"metadata": {},
315377
"outputs": [
316378
{
317379
"name": "stdout",
318380
"output_type": "stream",
319381
"text": [
320-
"image data shape:(256, 256, 2)\n",
321-
"meta data:{'format': None, 'mode': 'LA', 'width': 256, 'height': 256, 'info': {}, 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.png'}\n"
382+
"image data shape:(128, 256, 2)\n",
383+
"meta data:{'format': None, 'mode': 'LA', 'width': 256, 'height': 128, 'info': {}, 'spatial_shape': [256, 128], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.png'}\n"
322384
]
323385
}
324386
],
@@ -340,7 +402,7 @@
340402
},
341403
{
342404
"cell_type": "code",
343-
"execution_count": 23,
405+
"execution_count": 9,
344406
"metadata": {
345407
"tags": []
346408
},
@@ -355,7 +417,7 @@
355417
" [0., 1., 0.],\n",
356418
" [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n",
357419
" [0., 1., 0.],\n",
358-
" [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.png'}\n"
420+
" [0., 0., 1.]]), 'spatial_shape': [128, 256], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.png'}\n"
359421
]
360422
}
361423
],
@@ -383,7 +445,7 @@
383445
},
384446
{
385447
"cell_type": "code",
386-
"execution_count": 27,
448+
"execution_count": 10,
387449
"metadata": {},
388450
"outputs": [],
389451
"source": [

0 commit comments

Comments
 (0)