{"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":"# Create Single Sagittal Three Layer JPG from each Study\n\n## Each layer is from a different sagittal location within the study\n\n#### Caveat:  I'm not hopeful that this is a good approach since: \n- compression fractures of the vertebral body may show on the sagittal views \n- but small non displaced fractures of the vertebral spine would be hard to pick up\n- resolution is often lost on the reconstructed images and is dependent on the original slice thickness\n- 3 views likely won't capture the relevant areas","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom pathlib import Path\nimport numpy as np\n\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n%matplotlib inline\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2022-08-23T22:16:54.181802Z","iopub.execute_input":"2022-08-23T22:16:54.183093Z","iopub.status.idle":"2022-08-23T22:16:54.43422Z","shell.execute_reply.started":"2022-08-23T22:16:54.182984Z","shell.execute_reply":"2022-08-23T22:16:54.433029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\")\n#test_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/test.csv\")\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T22:16:54.439106Z","iopub.execute_input":"2022-08-23T22:16:54.439524Z","iopub.status.idle":"2022-08-23T22:16:54.479764Z","shell.execute_reply.started":"2022-08-23T22:16:54.43949Z","shell.execute_reply":"2022-08-23T22:16:54.478463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### The source images are from a prior notebook which created multiple sagittal slices for each study.\n\n- Three sample images are taken at the midline of the images and 15 pixels on either side to hopefully capture a close to midline image for one of the three\n\n- The three images are added as RGB layers and saved as a jpg.","metadata":{}},{"cell_type":"code","source":"dest = Path(\"/kaggle/working/\")\nsrc = Path(\"../input/simplify-data-by-creating-sagittal-slices/sag\")\n\ndef create_three_layer_sag(study_id, relative_locations_to_midline = [\"-15\", \"0\", \"15\"]):\n   \n    layers = []\n    for location in relative_locations_to_midline:\n        filename = f'{study_id}_{location}.jpg'\n        pathname = src/study_id/filename\n\n        im = mpimg.imread(pathname)[:,:,0]\n        layers.append(im)\n\n    layers = np.dstack(layers)\n    cv2.imwrite(str(dest/f\"{study_id}.jpg\"), layers)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T22:16:54.483799Z","iopub.execute_input":"2022-08-23T22:16:54.484583Z","iopub.status.idle":"2022-08-23T22:16:54.493105Z","shell.execute_reply.started":"2022-08-23T22:16:54.484533Z","shell.execute_reply":"2022-08-23T22:16:54.491823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create and save three layer jpgs for all training studies","metadata":{}},{"cell_type":"code","source":"#take 3 slices and create new jpg \nstudy_ids = train_df.StudyInstanceUID.values\n\nfor study_id in tqdm(study_ids, total=len(study_ids)):\n    #take 3 spaced sagittal images and create one RGB jpg\n    create_three_layer_sag(study_id)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T22:16:54.496436Z","iopub.execute_input":"2022-08-23T22:16:54.496993Z","iopub.status.idle":"2022-08-23T22:17:55.449678Z","shell.execute_reply.started":"2022-08-23T22:16:54.496933Z","shell.execute_reply":"2022-08-23T22:17:55.447962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show a sample","metadata":{}},{"cell_type":"code","source":"study_id = study_ids[100]\nimg = cv2.imread(str(dest/ f'{study_id}.jpg'), cv2.IMREAD_COLOR)\n \nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T22:17:55.451421Z","iopub.execute_input":"2022-08-23T22:17:55.451839Z","iopub.status.idle":"2022-08-23T22:17:55.765389Z","shell.execute_reply.started":"2022-08-23T22:17:55.451802Z","shell.execute_reply":"2022-08-23T22:17:55.763917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Several more samples. \n\n#### Compression fractures of the vertebral body may show on the sagittal views but small non displaced fractures of the vertebral spine would be hard to pick up","metadata":{}},{"cell_type":"code","source":"images = study_ids[:9]\n\n# set the canvas size in inches\nplt.figure(figsize=(15,15))\n\nfor i, study_id in enumerate(images):\n  img_path = str(str(dest/ f'{study_id}.jpg'))\n  # the number of images in the grid is 3*3 (9)\n  plt.subplot(3,3,i+1)    \n  img = plt.imread(img_path)\n  plt.imshow(img, cmap='bone')\n  plt.title(Path(img_path).name)\n  plt.axis(\"off\")\n\nplt.show()\nplt.close()","metadata":{"execution":{"iopub.status.busy":"2022-08-23T22:17:55.766842Z","iopub.execute_input":"2022-08-23T22:17:55.767197Z","iopub.status.idle":"2022-08-23T22:17:56.955604Z","shell.execute_reply.started":"2022-08-23T22:17:55.767165Z","shell.execute_reply":"2022-08-23T22:17:56.954103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}