{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"This is just Theo Viel's method https://www.kaggle.com/code/theoviel/dicom-resized-png-jpg","metadata":{}},{"cell_type":"code","source":"!pip install -qU python-gdcm pydicom pylibjpeg","metadata":{"execution":{"iopub.status.busy":"2024-06-10T11:51:00.068579Z","iopub.execute_input":"2024-06-10T11:51:00.068935Z","iopub.status.idle":"2024-06-10T11:51:15.356215Z","shell.execute_reply.started":"2024-06-10T11:51:00.068908Z","shell.execute_reply":"2024-06-10T11:51:15.354623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport glob\nimport gdcm\nimport pydicom\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom tqdm.notebook import tqdm\nfrom joblib import Parallel, delayed","metadata":{"execution":{"iopub.status.busy":"2024-06-10T11:51:15.358457Z","iopub.execute_input":"2024-06-10T11:51:15.358839Z","iopub.status.idle":"2024-06-10T11:51:17.046644Z","shell.execute_reply.started":"2024-06-10T11:51:15.358803Z","shell.execute_reply":"2024-06-10T11:51:17.045431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = glob.glob(\"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images/*/*/*.dcm\")\n\nlen(train_images) #147218","metadata":{"execution":{"iopub.status.busy":"2024-06-10T11:52:08.492419Z","iopub.execute_input":"2024-06-10T11:52:08.493195Z","iopub.status.idle":"2024-06-10T11:52:41.836152Z","shell.execute_reply.started":"2024-06-10T11:52:08.493155Z","shell.execute_reply":"2024-06-10T11:52:41.834959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for f in tqdm(train_images[:3]):\n    patient = f.split('/')[-2]\n    image = f.split('/')[-1][:-4]\n\n    dicom = pydicom.dcmread(f)\n    img = dicom.pixel_array\n\n    img = (img - img.min()) / (img.max() - img.min())\n\n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        img = 1 - img\n        \n    plt.figure(figsize=(15, 15))\n    plt.imshow(img, cmap=\"gray\")\n    plt.title(f\"{patient} {image}\")\n    plt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2024-06-10T11:52:56.425516Z","iopub.execute_input":"2024-06-10T11:52:56.425918Z","iopub.status.idle":"2024-06-10T11:52:58.677089Z","shell.execute_reply.started":"2024-06-10T11:52:56.425889Z","shell.execute_reply":"2024-06-10T11:52:58.675923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SAVE_FOLDER = \"output/\"\nSIZE = 512\nEXTENSION = \"png\"\n\nos.makedirs(SAVE_FOLDER, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-10T11:53:30.760926Z","iopub.execute_input":"2024-06-10T11:53:30.761319Z","iopub.status.idle":"2024-06-10T11:53:30.767746Z","shell.execute_reply.started":"2024-06-10T11:53:30.761289Z","shell.execute_reply":"2024-06-10T11:53:30.765963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process(f, size=256, save_folder=\"\", extension=\"png\"):\n    patient = f.split('/')[-2]\n    image = f.split('/')[-1][:-4]\n\n    dicom = pydicom.dcmread(f)\n    img = dicom.pixel_array\n\n    img = (img - img.min()) / (img.max() - img.min())\n\n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        img = 1 - img\n\n    img = cv2.resize(img, (size, size))\n\n    cv2.imwrite(save_folder + f\"{patient}_{image}.{extension}\", (img * 255).astype(np.uint8))","metadata":{"execution":{"iopub.status.busy":"2024-06-10T11:54:09.686252Z","iopub.execute_input":"2024-06-10T11:54:09.686652Z","iopub.status.idle":"2024-06-10T11:54:09.694140Z","shell.execute_reply.started":"2024-06-10T11:54:09.686626Z","shell.execute_reply":"2024-06-10T11:54:09.692805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = Parallel(n_jobs=4)(\n    delayed(process)(uid, size=SIZE, save_folder=SAVE_FOLDER, extension=EXTENSION)\n    for uid in tqdm(train_images)\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-10T11:54:10.703075Z","iopub.execute_input":"2024-06-10T11:54:10.703521Z","iopub.status.idle":"2024-06-10T11:54:12.144372Z","shell.execute_reply.started":"2024-06-10T11:54:10.703487Z","shell.execute_reply":"2024-06-10T11:54:12.142873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}