diff --git a/load_medical_images.ipynb b/load_medical_images.ipynb index 2db2667b7a..f0af97b1b0 100644 --- a/load_medical_images.ipynb +++ b/load_medical_images.ipynb @@ -66,33 +66,11 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "MONAI version: 0.2.0\n", - "Python version: 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 19:07:31) [GCC 7.3.0]\n", - "Numpy version: 1.18.1\n", - "Pytorch version: 1.6.0\n", - "\n", - "Optional dependencies:\n", - "Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.\n", - "Nibabel version: 3.1.1\n", - "scikit-image version: 0.15.0\n", - "Pillow version: 7.2.0\n", - "Tensorboard version: 2.1.0\n", - "\n", - "For details about installing the optional dependencies, please visit:\n", - " https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "# Copyright 2020 MONAI Consortium\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", @@ -133,14 +111,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Load image with default image reader\n", + "## Load Nifti image with default image reader\n", "MONAI leverages `ITK` as the default image reader, it can support most of the common medical image formats.\n", "More details, please check: https://github.com/InsightSoftwareConsortium/ITK/tree/master/Modules/IO" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 2, "metadata": { "tags": [] }, @@ -149,8 +127,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "image data shape:(128, 128, 128)\n", - "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", + "image data shape:(64, 128, 96)\n", + "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", " [0., 1., 0.],\n", " [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", @@ -158,14 +136,14 @@ " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 0., 1., 0.],\n", - " [0., 0., 0., 1.]]), 'spatial_shape': [128, 128, 128], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.nii.gz'}\n" + " [0., 0., 0., 1.]]), 'spatial_shape': [64, 128, 96], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.nii.gz'}\n" ] } ], "source": [ "# generate 3D test images\n", "tempdir = tempfile.mkdtemp()\n", - "test_image = np.random.rand(128, 128, 128)\n", + "test_image = np.random.rand(64, 128, 96)\n", "filename = os.path.join(tempdir, \"test_image.nii.gz\")\n", "itk_np_view = itk.image_view_from_array(test_image)\n", "itk.imwrite(itk_np_view, filename)\n", @@ -179,7 +157,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Load a list of images and stack as 1 training item\n", + "## Load a list of Nifti images and stack as 1 training item\n", "Loading a list of files, stack them together and add a new dimension as first dimension.\n", "\n", "And use the meta data of the first image to represent the stacked result." @@ -187,15 +165,15 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "image data shape:(3, 128, 128, 128)\n", - "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", + "image data shape:(3, 64, 128, 96)\n", + "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", " [0., 1., 0.],\n", " [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", @@ -203,7 +181,7 @@ " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 0., 1., 0.],\n", - " [0., 0., 0., 1.]]), 'spatial_shape': [128, 128, 128], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.nii.gz'}\n" + " [0., 0., 0., 1.]]), 'spatial_shape': [64, 128, 96], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.nii.gz'}\n" ] } ], @@ -219,6 +197,90 @@ "print(f\"meta data:{meta}\")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Load 3D image in DICOM format" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "image data shape:(64, 128, 96)\n", + "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", + " [0., 1., 0.],\n", + " [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n", + " [0., 1., 0., 0.],\n", + " [0., 0., 1., 0.],\n", + " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", + " [0., 1., 0., 0.],\n", + " [0., 0., 1., 0.],\n", + " [0., 0., 0., 1.]]), 'spatial_shape': [64, 128, 96], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.dcm'}\n" + ] + } + ], + "source": [ + "filename = os.path.join(tempdir, \"test_image.dcm\")\n", + "dcm_image = np.random.randint(256, size=(64, 128, 96)).astype(np.uint8())\n", + "itk_np_view = itk.image_view_from_array(dcm_image)\n", + "itk.imwrite(itk_np_view, filename)\n", + "data, meta = LoadImage()(filename)\n", + "\n", + "print(f\"image data shape:{data.shape}\")\n", + "print(f\"meta data:{meta}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Load a list of DICOM images and stack as 1 training item\n", + "Loading a list of files, stack them together and add a new dimension as first dimension.\n", + "\n", + "And use the meta data of the first image to represent the stacked result." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "image data shape:(3, 64, 128, 96)\n", + "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", + " [0., 1., 0.],\n", + " [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n", + " [0., 1., 0., 0.],\n", + " [0., 0., 1., 0.],\n", + " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", + " [0., 1., 0., 0.],\n", + " [0., 0., 1., 0.],\n", + " [0., 0., 0., 1.]]), 'spatial_shape': [64, 128, 96], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.dcm'}\n" + ] + } + ], + "source": [ + "filenames = [\"test_image.dcm\", \"test_image2.dcm\", \"test_image3.dcm\"]\n", + "for i, name in enumerate(filenames):\n", + " filenames[i] = os.path.join(tempdir, name)\n", + " itk_np_view = itk.image_view_from_array(dcm_image)\n", + " itk.imwrite(itk_np_view, filenames[i])\n", + "data, meta = LoadImage()(filenames)\n", + "\n", + "print(f\"image data shape:{data.shape}\")\n", + "print(f\"meta data:{meta}\")" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -228,7 +290,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 6, "metadata": { "tags": [] }, @@ -237,18 +299,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "image data shape:(256, 256)\n", + "image data shape:(128, 256)\n", "meta data:{'origin': array([0., 0.]), 'spacing': array([1., 1.]), 'direction': array([[1., 0.],\n", " [0., 1.]]), 'original_affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", - " [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.png'}\n" + " [0., 0., 1.]]), 'spatial_shape': [128, 256], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.png'}\n" ] } ], "source": [ - "test_image = np.random.randint(0, 256, size=[256, 256])\n", + "test_image = np.random.randint(0, 256, size=[128, 256])\n", "filename = os.path.join(tempdir, \"test_image.png\")\n", "Image.fromarray(test_image.astype(\"uint8\")).save(filename)\n", "data, meta = LoadImage()(filename)\n", @@ -272,26 +334,26 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "image data shape:(256, 256)\n", + "image data shape:(128, 256)\n", "meta data:{'origin': array([0., 0.]), 'spacing': array([1., 1.]), 'direction': array([[1., 0.],\n", " [0., 1.]]), 'original_affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", - " [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.png'}\n" + " [0., 0., 1.]]), 'spatial_shape': [128, 256], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.png'}\n" ] } ], "source": [ "loader = LoadImage()\n", - "loader.register(ITKReader(c_order_axis_indexing=True))\n", + "loader.register(ITKReader())\n", "data, meta = loader(filename)\n", "\n", "print(f\"image data shape:{data.shape}\")\n", @@ -310,15 +372,15 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "image data shape:(256, 256, 2)\n", - "meta data:{'format': None, 'mode': 'LA', 'width': 256, 'height': 256, 'info': {}, 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.png'}\n" + "image data shape:(128, 256, 2)\n", + "meta data:{'format': None, 'mode': 'LA', 'width': 256, 'height': 128, 'info': {}, 'spatial_shape': [256, 128], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.png'}\n" ] } ], @@ -340,7 +402,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 9, "metadata": { "tags": [] }, @@ -355,7 +417,7 @@ " [0., 1., 0.],\n", " [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", - " [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.png'}\n" + " [0., 0., 1.]]), 'spatial_shape': [128, 256], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.png'}\n" ] } ], @@ -383,7 +445,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [