{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## **What are DCM files?**\n\nDCM file is an image following Digital Imaging and Communications in Medicine (DICOM) format. It wasw created by The National Electrical Manufactureres Association (NEMA) in 1983, so it's not a new standard but it was revised many times since then. Often the same data are stored as .DICOM files.  \nBoth standards are used to store various medical images like CT scans, MRIs, PET, ultrasound, etc.\n\n## **Why do we need this format?**\nThe idea behind this format is to store pixels and datasets in the form of attributes (e.g. patients relevant data) in one place.\n\n## **How to open it?**\nTo open files in this standard we can use [pydicom](https://pydicom.github.io/) library. If you want to manually browse the files you can install for example an Chrome extension [DICOM Medical Image Reader](https://chrome.google.com/webstore/detail/dicom-medical-image-reade/phakdkeobphiapdoggpcdilgmjbepnfg?hl=pl).","metadata":{}},{"cell_type":"code","source":"from glob import glob\nimport matplotlib.pylab as plt\n\nimport pydicom as dicom","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:12:59.726132Z","iopub.execute_input":"2022-08-12T22:12:59.72724Z","iopub.status.idle":"2022-08-12T22:12:59.897087Z","shell.execute_reply.started":"2022-08-12T22:12:59.727129Z","shell.execute_reply":"2022-08-12T22:12:59.895905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = glob(\"../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/*\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-12T22:12:59.899137Z","iopub.execute_input":"2022-08-12T22:12:59.89949Z","iopub.status.idle":"2022-08-12T22:12:59.930269Z","shell.execute_reply.started":"2022-08-12T22:12:59.899458Z","shell.execute_reply":"2022-08-12T22:12:59.929306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use('default')\nfig, axes = plt.subplots(4,4, figsize=(12,12))\ntrain_images\nfor i, ax in enumerate(axes.reshape(-1)):\n    img_path = train_images[i]\n    img = dicom.dcmread(img_path)  \n    ax.imshow(img.pixel_array)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:12:59.931946Z","iopub.execute_input":"2022-08-12T22:12:59.932332Z","iopub.status.idle":"2022-08-12T22:13:02.886795Z","shell.execute_reply.started":"2022-08-12T22:12:59.932302Z","shell.execute_reply":"2022-08-12T22:13:02.884571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **What are .nii files?**\n\nNII format is created by **Neuroimaging Informatics Technology Initiative**. It is commonly used to store magnetic resonance imaging (MRI) data.\n\n## **How to open it?**\nTo open files in this standard we can use [nibabel](https://nipy.org/nibabel/gettingstarted.html) library.","metadata":{}},{"cell_type":"code","source":"import nibabel as nib","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:13:02.889294Z","iopub.execute_input":"2022-08-12T22:13:02.889929Z","iopub.status.idle":"2022-08-12T22:13:03.02699Z","shell.execute_reply.started":"2022-08-12T22:13:02.889887Z","shell.execute_reply":"2022-08-12T22:13:03.025259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/rsna-2022-cervical-spine-fracture-detection/segmentations/1.2.826.0.1.3680043.10633.nii'","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:13:03.028733Z","iopub.execute_input":"2022-08-12T22:13:03.029221Z","iopub.status.idle":"2022-08-12T22:13:03.035698Z","shell.execute_reply.started":"2022-08-12T22:13:03.029173Z","shell.execute_reply":"2022-08-12T22:13:03.033958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = nib.load(path).get_fdata()\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:21:13.436132Z","iopub.execute_input":"2022-08-12T22:21:13.436581Z","iopub.status.idle":"2022-08-12T22:21:13.728568Z","shell.execute_reply.started":"2022-08-12T22:21:13.436541Z","shell.execute_reply":"2022-08-12T22:21:13.725729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"The .nii files are stored in memory as numpy's: {type(img)}.\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:24:12.673521Z","iopub.execute_input":"2022-08-12T22:24:12.674044Z","iopub.status.idle":"2022-08-12T22:24:12.680169Z","shell.execute_reply.started":"2022-08-12T22:24:12.674005Z","shell.execute_reply":"2022-08-12T22:24:12.679018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we see this picture is **3D**. The 3rd dimension is a number of slices. In order to visualise it we have to take only one slice.","metadata":{}},{"cell_type":"code","source":"plt.style.use('default')\nfig, axes = plt.subplots(4,4, figsize=(12,12))\nfor i, ax in enumerate(axes.reshape(-1)):\n    ax.imshow(img[:,:,1 + i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:15:07.413576Z","iopub.execute_input":"2022-08-12T22:15:07.414011Z","iopub.status.idle":"2022-08-12T22:15:09.510685Z","shell.execute_reply.started":"2022-08-12T22:15:07.413979Z","shell.execute_reply":"2022-08-12T22:15:09.509447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We see that the orientation of segments os different than images. In order to align them we can simply use `rotate()` function from `skimage` library.","metadata":{}},{"cell_type":"code","source":"img.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:21:16.812928Z","iopub.execute_input":"2022-08-12T22:21:16.813362Z","iopub.status.idle":"2022-08-12T22:21:16.821935Z","shell.execute_reply.started":"2022-08-12T22:21:16.813328Z","shell.execute_reply":"2022-08-12T22:21:16.820568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.transform import rotate\n\nplt.style.use('default')\nfig, axes = plt.subplots(4,4, figsize=(12,12))\nfor i, ax in enumerate(axes.reshape(-1)):\n    img_rot = rotate(img[:,:,1 + i], -90)\n    ax.imshow(img_rot)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T22:21:17.038891Z","iopub.execute_input":"2022-08-12T22:21:17.03929Z","iopub.status.idle":"2022-08-12T22:21:19.580093Z","shell.execute_reply.started":"2022-08-12T22:21:17.039259Z","shell.execute_reply":"2022-08-12T22:21:19.57869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}