{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install natsort","metadata":{"execution":{"iopub.status.busy":"2023-09-09T07:08:20.698118Z","iopub.execute_input":"2023-09-09T07:08:20.698518Z","iopub.status.idle":"2023-09-09T07:08:31.838345Z","shell.execute_reply.started":"2023-09-09T07:08:20.698483Z","shell.execute_reply":"2023-09-09T07:08:31.836791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom natsort import natsorted\nimport pandas as pd\nimport numpy as np\nimport pydicom\nimport matplotlib.pyplot as plt\nimport matplotlib.animation as animation\nfrom IPython.display import HTML\nimport matplotlib as rc\n%matplotlib inline\n# gif 保存のためにインストール\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2023-09-09T07:08:31.840859Z","iopub.execute_input":"2023-09-09T07:08:31.841716Z","iopub.status.idle":"2023-09-09T07:08:31.851122Z","shell.execute_reply.started":"2023-09-09T07:08:31.841673Z","shell.execute_reply":"2023-09-09T07:08:31.849862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 画像の確認\ndataset = pydicom.read_file('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/21057/1028.dcm')\nimg = dataset.pixel_array\nplt.axis(False)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T07:08:31.852595Z","iopub.execute_input":"2023-09-09T07:08:31.853118Z","iopub.status.idle":"2023-09-09T07:08:32.089155Z","shell.execute_reply.started":"2023-09-09T07:08:31.853074Z","shell.execute_reply":"2023-09-09T07:08:32.088149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 10004の画像を3次元化するため対象のフォルダのデータを取得し縦方向に結合していく\n# 結合するフォルダ\ndir_path = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004'\n# 結合のための配列を用意\nfilepaths = []\nfilenamelists = []\ndcm_img = [] # 分析用\n\n# 対象のディレクトリからデータを取得\nfor dirname, _, filenames in os.walk(dir_path):\n    for k,filename in enumerate(filenames):\n        if filename not in filenamelists:\n            filepaths.append(os.path.join(dirname, filename))\n            filenamelists.append(filename)\n            \nfilepaths = natsorted(set(filepaths))\nfor filepath in filepaths:\n    dataset = pydicom.read_file(filepath)\n    img = dataset.pixel_array\n    dcm_img.append(img)\n        \nnp_dcm_img = np.array(dcm_img)\n# 形状の確認\nnp_dcm_img.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-09T07:09:06.363271Z","iopub.execute_input":"2023-09-09T07:09:06.363663Z","iopub.status.idle":"2023-09-09T07:09:25.915738Z","shell.execute_reply.started":"2023-09-09T07:09:06.363615Z","shell.execute_reply":"2023-09-09T07:09:25.914712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_gif_image(imgs, savename = 'sample',duration=1):\n    gif_list = []\n    for i, img in enumerate(imgs):\n        pil_img = Image.fromarray(img)\n        gif_list.append(pil_img)\n    gif_list[0].save(f'{savename}.gif',save_all=True, append_images=gif_list[1:], optimize=True, duration=duration, loop=0)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T07:09:29.305952Z","iopub.execute_input":"2023-09-09T07:09:29.306339Z","iopub.status.idle":"2023-09-09T07:09:29.312557Z","shell.execute_reply.started":"2023-09-09T07:09:29.306303Z","shell.execute_reply":"2023-09-09T07:09:29.311444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 指定方向からのviewに変更\ndef create_gif_return_nparray(np_img, num = 0):\n    return_img = []\n    for i in range(np_img.shape[num]):\n        if num==0:\n            re_img = np_img[i,:,:]\n        elif num==1:\n            re_img= np_img[:,i,:]\n        elif num ==2:\n            re_img = np_img[:,:,i]\n        return_img.append(re_img)\n    return_img = np.array(return_img)\n    return return_img","metadata":{"execution":{"iopub.status.busy":"2023-09-09T07:09:35.054958Z","iopub.execute_input":"2023-09-09T07:09:35.055333Z","iopub.status.idle":"2023-09-09T07:09:35.062377Z","shell.execute_reply.started":"2023-09-09T07:09:35.055301Z","shell.execute_reply":"2023-09-09T07:09:35.061329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filename\ntopviewname = 'topview_train_10004'\nsideviewname = 'sideview_train_10004'","metadata":{"execution":{"iopub.status.busy":"2023-09-09T07:08:32.145648Z","iopub.execute_input":"2023-09-09T07:08:32.146408Z","iopub.status.idle":"2023-09-09T07:08:32.158078Z","shell.execute_reply.started":"2023-09-09T07:08:32.146373Z","shell.execute_reply":"2023-09-09T07:08:32.156803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np_dcm_img_0 = create_gif_return_nparray(np_dcm_img, num=0)\nnp_dcm_img_2 = create_gif_return_nparray(np_dcm_img, num=2)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T07:09:43.84214Z","iopub.execute_input":"2023-09-09T07:09:43.842522Z","iopub.status.idle":"2023-09-09T07:09:46.677378Z","shell.execute_reply.started":"2023-09-09T07:09:43.84249Z","shell.execute_reply":"2023-09-09T07:09:46.676048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_gif_image(np_dcm_img_0, savename=topviewname)\nsave_gif_image(np_dcm_img_2, savename=sideviewname)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T07:10:35.31273Z","iopub.execute_input":"2023-09-09T07:10:35.313252Z","iopub.status.idle":"2023-09-09T07:10:48.99469Z","shell.execute_reply.started":"2023-09-09T07:10:35.313209Z","shell.execute_reply":"2023-09-09T07:10:48.993705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = np_dcm_img[:,:,100]\nplt.axis(False)\nplt.imshow(img)\nprint(img.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T07:10:05.533204Z","iopub.execute_input":"2023-09-09T07:10:05.53383Z","iopub.status.idle":"2023-09-09T07:10:05.726715Z","shell.execute_reply.started":"2023-09-09T07:10:05.533784Z","shell.execute_reply":"2023-09-09T07:10:05.725541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HTML(f'<img src=\"./{topviewname}.gif\" />')","metadata":{"execution":{"iopub.status.busy":"2023-09-09T07:11:35.001743Z","iopub.execute_input":"2023-09-09T07:11:35.002105Z","iopub.status.idle":"2023-09-09T07:11:35.008867Z","shell.execute_reply.started":"2023-09-09T07:11:35.002074Z","shell.execute_reply":"2023-09-09T07:11:35.007665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HTML(f'<img src=\"./{sideviewname}.gif\" />')","metadata":{"execution":{"iopub.status.busy":"2023-09-09T07:11:19.283686Z","iopub.execute_input":"2023-09-09T07:11:19.284448Z","iopub.status.idle":"2023-09-09T07:11:19.291685Z","shell.execute_reply.started":"2023-09-09T07:11:19.284406Z","shell.execute_reply":"2023-09-09T07:11:19.290498Z"},"trusted":true},"execution_count":null,"outputs":[]}],"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"}}