{"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":"# BTRC: A simple tool to crop 3D images","metadata":{}},{"cell_type":"markdown","source":"The scans has too many empty spaces, so ```cropped_images``` is simple function to crop all superfluous area (pixels with 0 value near the edge) in 3D images","metadata":{}},{"cell_type":"code","source":"import numpy as np \nfrom glob import glob\nimport plotly.express as px\nimport pydicom\n\ndef read_dicom_files(cohort, case, mpMRI):\n    PATH = '../input/rsna-miccai-brain-tumor-radiogenomic-classification'\n    files_glob = f'{PATH}/{cohort}/{case}/{mpMRI}/*.dcm'\n    sorted_files = sorted(glob(files_glob),key=lambda f: int(f.split('Image-')[1].split('.')[0]))\n    return [pydicom.read_file(f) for f in sorted_files]","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-22T10:58:38.696494Z","iopub.execute_input":"2021-08-22T10:58:38.696937Z","iopub.status.idle":"2021-08-22T10:58:38.704311Z","shell.execute_reply.started":"2021-08-22T10:58:38.696901Z","shell.execute_reply":"2021-08-22T10:58:38.702908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cropped_images(images):\n    min=np.array(np.nonzero(images)).min(axis=1)\n    max=np.array(np.nonzero(images)).max(axis=1)+1 # --> Thank you @lai321!\n    return images[min[0]:max[0],min[1]:max[1],min[2]:max[2]]","metadata":{"execution":{"iopub.status.busy":"2021-08-22T10:58:38.759853Z","iopub.execute_input":"2021-08-22T10:58:38.760276Z","iopub.status.idle":"2021-08-22T10:58:38.767335Z","shell.execute_reply.started":"2021-08-22T10:58:38.760238Z","shell.execute_reply":"2021-08-22T10:58:38.765531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Test","metadata":{}},{"cell_type":"code","source":"cuore = np.array(1).reshape(1,1,1)\npadded_array = np.pad(cuore,1)\nassert cropped_images(padded_array) == cuore","metadata":{"execution":{"iopub.status.busy":"2021-08-22T10:58:38.820944Z","iopub.execute_input":"2021-08-22T10:58:38.821342Z","iopub.status.idle":"2021-08-22T10:58:38.826815Z","shell.execute_reply.started":"2021-08-22T10:58:38.821310Z","shell.execute_reply":"2021-08-22T10:58:38.826059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Example","metadata":{}},{"cell_type":"code","source":"cohort = 'train'\ncase = '00000'\nmpMRI = 'FLAIR'\ndicom_files = read_dicom_files(cohort, case, mpMRI)\n\nimages = np.array([s.pixel_array for s in dicom_files])\nimages = cropped_images(images)\n\nfig = px.imshow(images, animation_frame=0, binary_string=True, labels=dict(animation_frame=\"scan\"), height=600)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-22T10:58:38.875828Z","iopub.execute_input":"2021-08-22T10:58:38.876492Z","iopub.status.idle":"2021-08-22T10:58:43.971294Z","shell.execute_reply.started":"2021-08-22T10:58:38.876455Z","shell.execute_reply":"2021-08-22T10:58:43.969907Z"},"trusted":true},"execution_count":null,"outputs":[]}]}