{"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":"\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install \"python-gdcm\" pydicom pylibjpeg","metadata":{"execution":{"iopub.status.busy":"2022-09-18T20:03:34.036089Z","iopub.execute_input":"2022-09-18T20:03:34.03652Z","iopub.status.idle":"2022-09-18T20:03:44.698512Z","shell.execute_reply.started":"2022-09-18T20:03:34.036483Z","shell.execute_reply":"2022-09-18T20:03:44.697389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport cv2\nimport matplotlib.animation as animation\nfrom IPython.display import HTML\n\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport nibabel as nib","metadata":{"execution":{"iopub.status.busy":"2022-09-18T20:04:48.742375Z","iopub.execute_input":"2022-09-18T20:04:48.743504Z","iopub.status.idle":"2022-09-18T20:04:48.749319Z","shell.execute_reply.started":"2022-09-18T20:04:48.743458Z","shell.execute_reply":"2022-09-18T20:04:48.748223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# patient with normal orientation\nfig = plt.figure()\n\n# read & sort dcm files\ndcm_dir = '../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001'\ndcm_list = os.listdir(dcm_dir)\ndcm_list.sort(key=lambda x: int(x.rstrip('.dcm')))\ndatasets = [pydicom.dcmread(os.path.join(dcm_dir,fname)) for fname in dcm_list]\nprint(datasets[10].ImagePositionPatient)\nprint(datasets[10].ImageOrientationPatient)\nprint(datasets[10])\n\n# display\n# images = [apply_voi_lut(dataset.pixel_array, dataset) for dataset in datasets]\n# ims = [[plt.imshow(img,cmap='gray',animated=True)] for img in images]\n# ani = animation.ArtistAnimation(fig, ims, interval=500, repeat_delay=1000, blit=True)\n# HTML(ani.to_jshtml())\n","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-18T20:03:44.709985Z","iopub.execute_input":"2022-09-18T20:03:44.710479Z","iopub.status.idle":"2022-09-18T20:03:45.240127Z","shell.execute_reply.started":"2022-09-18T20:03:44.710438Z","shell.execute_reply":"2022-09-18T20:03:45.239327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# patient with different orientation\nfig = plt.figure()\n\n# read & sort dcm files\ndcm_dir = '../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.16729'\ndcm_list = os.listdir(dcm_dir)\ndcm_list.sort(key=lambda x: int(x.rstrip('.dcm')))\ndatasets = [pydicom.dcmread(os.path.join(dcm_dir,fname)) for fname in dcm_list]\nprint(datasets[500])\n\n# display\n# images = [apply_voi_lut(dataset.pixel_array, dataset) for dataset in datasets]\n# ims = [[plt.imshow(img,cmap='gray',animated=True)] for img in images]\n# ani = animation.ArtistAnimation(fig, ims, interval=500, repeat_delay=1000, blit=True)\n# HTML(ani.to_jshtml())\n","metadata":{"execution":{"iopub.status.busy":"2022-09-18T20:03:45.241195Z","iopub.execute_input":"2022-09-18T20:03:45.241917Z","iopub.status.idle":"2022-09-18T20:03:46.368416Z","shell.execute_reply.started":"2022-09-18T20:03:45.241883Z","shell.execute_reply":"2022-09-18T20:03:46.367365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(cv2.ROTATE_90_CLOCKWISE)\nprint(cv2.ROTATE_90_COUNTERCLOCKWISE)\nprint(cv2.ROTATE_180)","metadata":{"execution":{"iopub.status.busy":"2022-09-18T20:03:46.370925Z","iopub.execute_input":"2022-09-18T20:03:46.371441Z","iopub.status.idle":"2022-09-18T20:03:46.376853Z","shell.execute_reply.started":"2022-09-18T20:03:46.371401Z","shell.execute_reply":"2022-09-18T20:03:46.375742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# assuming at least the z axis is correct\n# rotate 90 degree to match the orientation\n# if the ImageOrientationPatient[:3] is:\n# (1,0,0) it is in normal orientation\n# (0,-1,0) it is rotated 90 degree clockwise\n# (0,1,0) it is rotated 90 degree counter-\n\ndef get_rotateCode(dataset):\n    \"\"\"\n    0 : cv2.ROTATE_90_CLOCKWISE\n    1 : cv2.ROTATE_180\n    2 : cv2.ROTATE_90_COUNTERCLOCKWISE\n    3 : no rotation\n    \"\"\"\n    ori = dataset.ImageOrientationPatient[:3]\n    if ori[0] > 0.7:\n        return 3 \n    if ori[1] < -0.7:\n        return 2\n    if ori[1] > 0.7:\n        return 0\n    if ori[0] < -0.7:\n        return 1\n    # just in case\n    else:\n        raise ValueError(ori)\n    \n    \n\nfig = plt.figure()\n\n# patient with normal orientation\ndcm_dir = '../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001'\ndcm_list = os.listdir(dcm_dir)\ndcm_list.sort(key=lambda x: int(x.rstrip('.dcm')))\ndatasets = [pydicom.dcmread(os.path.join(dcm_dir,fname)) for fname in dcm_list]\nimages_up = [cv2.resize(apply_voi_lut(dataset.pixel_array, dataset), dsize=(128,128))\\\n             for dataset in datasets]\nrotateCode = get_rotateCode(datasets[0])\nprint(rotateCode)\nif rotateCode != 3:\n    images_up = [cv2.rotate(img, rotateCode) for img in images_up]\n    \n# patient with rotated orientation\ndcm_dir = '../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.16729'\ndcm_list = os.listdir(dcm_dir)\ndcm_list.sort(key=lambda x: int(x.rstrip('.dcm')))\ndatasets = [pydicom.dcmread(os.path.join(dcm_dir,fname)) for fname in dcm_list]\nimages_right = [cv2.resize(apply_voi_lut(dataset.pixel_array, dataset),dsize=(128,128))\\\n               for dataset in datasets]\nrotateCode = get_rotateCode(datasets[0])\nprint(rotateCode)\nif rotateCode != 3:\n    images_right = [cv2.rotate(img, rotateCode) for img in images_right]\n\n# display\nlen1 = len(images_up)\nlen2 = len(images_right)\nims = [[plt.imshow(\n            np.concatenate((images_up[i],images_right[int(i/len1*len2)]), axis=1),\n            cmap='gray',animated=True)] \\\n       for i in range(len1)]\nani = animation.ArtistAnimation(fig, ims, interval=500, repeat_delay=1000, blit=True)\nHTML(ani.to_jshtml())\n","metadata":{"execution":{"iopub.status.busy":"2022-09-18T20:09:12.084324Z","iopub.execute_input":"2022-09-18T20:09:12.085235Z","iopub.status.idle":"2022-09-18T20:09:39.401218Z","shell.execute_reply.started":"2022-09-18T20:09:12.085195Z","shell.execute_reply":"2022-09-18T20:09:39.400113Z"},"trusted":true},"execution_count":null,"outputs":[]}]}