{"cells":[{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"import os\nimport pydicom as dcm\nimport glob\nimport numpy as np\nimport pandas as pd\nimport math\nimport matplotlib.pyplot as plt\nplt.style.use('ggplot')\nimport seaborn as sns\nimport torch\nfrom sklearn.linear_model import Ridge\nimport random\nfrom tqdm.notebook import tqdm\nfrom sklearn.model_selection import StratifiedKFold, GroupKFold, KFold\nfrom sklearn.metrics import mean_squared_error\nimport category_encoders as ce\nfrom PIL import Image\nimport cv2\nimport lightgbm as lgb\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport seaborn as sns\np = sns.color_palette()\nimport plotly.express as px","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Function for reading DICOM","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def dicom_to_image(filename):\n    im = dcm.dcmread(filename)\n    img = im.pixel_array\n    img[img == -2000] = 0\n    return img","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Dark Images\n\nThanks to this [discussion](https://www.kaggle.com/c/osic-pulmonary-fibrosis-progression/discussion/170861) we are aware that some scans are dark, for instance as below.","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"files_darkimages = glob.glob('../input/osic-pulmonary-fibrosis-progression/train/ID00105637202208831864134/*.dcm')\nprint(\"Patiend files :\",len(files_darkimages))\nf, plots = plt.subplots(4, 5, sharex='col', sharey='row', figsize=(10, 8))\nfor i in range(20):\n    plots[i // 5, i % 5].axis('off')\n    plots[i // 5, i % 5].imshow(dicom_to_image(files_darkimages[i]), cmap=plt.cm.bone)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"markdown","source":"# Dark images - fixed\n\nThere seems two ways to fix this as mentioned [here](http://uwmsk.org/jupyter/Jupyter_DICOM_toolbox.html)\n\n1. `equalize_hist`\n2. `equalize_adapthist`\n\nLet us try both of these","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from skimage import exposure\ndef dicom_to_image2(filename):\n    im = dcm.dcmread(filename)\n    img = im.pixel_array\n    img = exposure.equalize_hist(img)\n    return img\n\ndef dicom_to_image3(filename):\n    im = dcm.dcmread(filename)\n    img = im.pixel_array\n    img = exposure.equalize_adapthist(img)\n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f, plots = plt.subplots(4, 5, sharex='col', sharey='row', figsize=(10, 8))\nfor i in range(20):\n    plots[i // 5, i % 5].axis('off')\n    plots[i // 5, i % 5].imshow(dicom_to_image2(files_darkimages[i]), cmap=plt.cm.bone)\nplt.title(\"Dark images fixed using equalize_hist\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f, plots = plt.subplots(4, 5, sharex='col', sharey='row', figsize=(10, 8))\nfor i in range(20):\n    plots[i // 5, i % 5].axis('off')\n    plots[i // 5, i % 5].imshow(dicom_to_image3(files_darkimages[i]), cmap=plt.cm.bone)\nplt.title(\"Dark images fixed using equalize_adapthist\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"While the images with equalise_hist are more clear, it is not recommended over the other. Hence we might have to use both these options for our experiments during training.\n\nPlease let me know your thoughts on this. Please upvote if you like my work.","execution_count":null}],"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":4,"nbformat_minor":4}