{"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":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\nimport pydicom as dicom\nfrom pydicom import dcmread\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-07T04:32:28.077889Z","iopub.execute_input":"2022-09-07T04:32:28.078982Z","iopub.status.idle":"2022-09-07T04:32:35.517283Z","shell.execute_reply.started":"2022-09-07T04:32:28.078931Z","shell.execute_reply":"2022-09-07T04:32:35.516342Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"With pydicom, we can not convert dicom files to numpy arrays with the meta informatin \"JPEG Lossless, Non-Hierarchical, First-Order Prediction (Process 14 [Selection Value 1])\" .So we just ignore these pictures...\nHowever,SimpleITK could handle these pictures.\n\nFor example,for the patient 1.2.826.0.1.3680043.25399 in the \"test_images/\", all his/her dicom files will be ignored if we use the pyidcom method.\n\nthese lines ignore pictures,and return empty arrays:","metadata":{}},{"cell_type":"code","source":"path=\"../input/rsna-2022-cervical-spine-fracture-detection/test_images/1.2.826.0.1.3680043.25399/\"\nim=\"1.dcm\"\nIMG_SIZE=512\n#load files as we did before\ndc = dicom.read_file(os.path.join(path, im))\nif dc.file_meta.TransferSyntaxUID.name == 'JPEG Lossless, Non-Hierarchical, First-Order Prediction (Process 14 [Selection Value 1])':\n    print(\"ignore pictures\")\n    \n#convert to nparray  \n#https://www.kaggle.com/code/thedevastator/tf-rsna-efficient-net-baseline\ndef load_dicom_with_pydicom(path, size = 64):\n    try:\n        img=dicom.dcmread(path)\n        img.PhotometricInterpretation = 'YBR_FULL'\n        data=img.pixel_array\n        data=data-np.min(data)\n        if np.max(data) != 0:\n            data=data/np.max(data)\n        data=(data*255).astype(np.uint8)        \n        return cv2.cvtColor(data.reshape(512, 512), cv2.COLOR_GRAY2RGB)\n    except: \n        print(\"erro\")\n        return np.zeros((512, 512, 3))\n    \ndata=load_dicom_with_pydicom(\"../input/rsna-2022-cervical-spine-fracture-detection/test_images/1.2.826.0.1.3680043.25399/1.dcm\")\nplt.imshow(data)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T04:40:46.805385Z","iopub.execute_input":"2022-09-07T04:40:46.805811Z","iopub.status.idle":"2022-09-07T04:40:47.183157Z","shell.execute_reply.started":"2022-09-07T04:40:46.805762Z","shell.execute_reply":"2022-09-07T04:40:47.181815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here we use SimpleITK","metadata":{}},{"cell_type":"code","source":"import SimpleITK as sitk\ndef load_dicom_with_SimpleITK(path, size = 64):\n    try:\n        img=sitk.ReadImage(path)\n        data=sitk.GetArrayFromImage(img)\n        data=data-np.min(data)\n        if np.max(data) != 0:\n            data=data/np.max(data)\n        data=(data*255).astype(np.uint8)        \n        return cv2.cvtColor(data.reshape(512, 512), cv2.COLOR_GRAY2RGB)\n    except: \n        print(\"erro\")\n        return np.zeros((512, 512, 3))\n    \ndata=load_dicom_with_SimpleITK(\"../input/rsna-2022-cervical-spine-fracture-detection/test_images/1.2.826.0.1.3680043.25399/1.dcm\")\nplt.imshow(data)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T04:44:50.014091Z","iopub.execute_input":"2022-09-07T04:44:50.015137Z","iopub.status.idle":"2022-09-07T04:44:50.419995Z","shell.execute_reply.started":"2022-09-07T04:44:50.015084Z","shell.execute_reply":"2022-09-07T04:44:50.418863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}