{"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":"# Install Libs","metadata":{}},{"cell_type":"code","source":"!pip install /kaggle/input/pydicom-offline-installer/numpy-1.21.6-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl\n!pip install /kaggle/input/pydicom-offline-installer/pydicom-2.3.1-py3-none-any.whl\n!pip install /kaggle/input/pydicom-offline-installer/pylibjpeg-1.4.0-py3-none-any.whl\n!pip install /kaggle/input/pydicom-offline-installer/python_gdcm-3.0.20-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2022-11-29T19:28:53.58772Z","iopub.execute_input":"2022-11-29T19:28:53.588317Z","iopub.status.idle":"2022-11-29T19:31:05.044756Z","shell.execute_reply.started":"2022-11-29T19:28:53.588181Z","shell.execute_reply":"2022-11-29T19:31:05.042946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import dependencies","metadata":{}},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport os\nimport pandas as pd\nimport pydicom\n\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom tqdm.notebook import tqdm as tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-29T19:31:05.047723Z","iopub.execute_input":"2022-11-29T19:31:05.048148Z","iopub.status.idle":"2022-11-29T19:31:05.252204Z","shell.execute_reply.started":"2022-11-29T19:31:05.048107Z","shell.execute_reply":"2022-11-29T19:31:05.251174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Util","metadata":{}},{"cell_type":"code","source":"def read_xray(\n        path: str,\n        voi_lut: bool=False,\n        fix_monochrome: bool=True\n):\n    dicom = pydicom.read_file(path)\n\n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n\n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n\n    return data\n\ndef resize_img(\n    img: np.array,\n    image_size: int,\n):\n    height, width = img.shape[:2]\n    scale = image_size / min(width, height)\n    w = int(width * scale + 0.5)\n    h = int(height * scale + 0.5)\n    if scale > 1.0:\n        interpolation=cv2.INTER_CUBIC\n    else:\n        interpolation=cv2.INTER_AREA\n\n    return cv2.resize(img, (w, h), interpolation=interpolation)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T19:31:05.253952Z","iopub.execute_input":"2022-11-29T19:31:05.255121Z","iopub.status.idle":"2022-11-29T19:31:05.266759Z","shell.execute_reply.started":"2022-11-29T19:31:05.255069Z","shell.execute_reply":"2022-11-29T19:31:05.265278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save images","metadata":{}},{"cell_type":"code","source":"pos = 54 # 0-54\nsize = 512\n\ninput_dir = \"../input/rsna-breast-cancer-detection\"\noutput_dir = \"./\"\n\ndf = pd.read_csv(os.path.join(input_dir, \"train.csv\"))\nimage_ids = df[\"image_id\"].unique()\n\nif pos == 54: # 0-54\n    image_ids = image_ids[pos*1000:]\nelse:\n    image_ids = image_ids[pos*1000:(pos+1)*1000]\n\nfor image_id in tqdm(image_ids, dynamic_ncols=True):\n\n    records = df[(df[\"image_id\"] == image_id)]\n    patient_id = records[\"patient_id\"].unique()\n    assert len(patient_id) == 1\n\n    img = read_xray(os.path.join(input_dir, \"train_images\", str(patient_id[0]), f\"{image_id}.dcm\"))\n    cv2.imwrite(os.path.join(output_dir, f\"{image_id}.jpg\"), resize_img(img, size))","metadata":{"execution":{"iopub.status.busy":"2022-11-29T19:31:05.269187Z","iopub.execute_input":"2022-11-29T19:31:05.269544Z","iopub.status.idle":"2022-11-29T19:33:33.458489Z","shell.execute_reply.started":"2022-11-29T19:31:05.26951Z","shell.execute_reply":"2022-11-29T19:33:33.456615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}