{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":6917177,"sourceType":"datasetVersion","datasetId":3889865},{"sourceId":7001173,"sourceType":"datasetVersion","datasetId":3881967}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Image normalization with histogram matching**\n\n* In this competition, the train and test image is collected from various hospital. So images have variations in color caused by different scanning equipment, staining methods, and tissue reactivity. \n* One method addressing this challenge is normalizing the image.\n* In this notebook, I introduce the lightweight normalizing method, **Histogram matching**.\n* Histogram matching was used in the solution of past competition related in histological classification task and segmentation task.\n\n* HuBMAP + HPA - Hacking the Human Body - 3rd place solution (https://www.kaggle.com/competitions/hubmap-organ-segmentation/discussion/354683)\n\n\n## Please Upvote if you Find this Useful :)","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport random\nfrom PIL import Image\nfrom skimage.exposure import match_histograms","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-11-21T12:18:45.138732Z","iopub.execute_input":"2023-11-21T12:18:45.139214Z","iopub.status.idle":"2023-11-21T12:18:45.149224Z","shell.execute_reply.started":"2023-11-21T12:18:45.139154Z","shell.execute_reply":"2023-11-21T12:18:45.147733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Histogram Matching","metadata":{}},{"cell_type":"code","source":"ref_path = \"/kaggle/input/tiles-of-cancer-2048px-scale-0-25/1020/000040_5-3.png\"\nref_img = np.array(Image.open(ref_path))\n\nplt.imshow(ref_img)\nplt.title(\"Reference Image\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T12:19:59.588485Z","iopub.execute_input":"2023-11-21T12:19:59.588927Z","iopub.status.idle":"2023-11-21T12:20:00.038342Z","shell.execute_reply.started":"2023-11-21T12:19:59.588894Z","shell.execute_reply":"2023-11-21T12:20:00.037336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths = [\n        \"/kaggle/input/tiles-of-cancer-2048px-scale-0-25/11417/000020_5-2.png\",\n        \"/kaggle/input/tiles-of-cancer-2048px-scale-0-25/10469/000044_3-3.png\",\n        \"/kaggle/input/tiles-of-cancer-2048px-scale-0-25/12442/000050_9-2.png\",\n        ]\n\nfig, ax = plt.subplots(2, 3, figsize=(14,6))\n\nfor i in range(3):\n    bef_img = np.array(Image.open(paths[i]))\n    aft_img = match_histograms(bef_img, ref_img, channel_axis=-1)\n    \n    ax[0, i].imshow(bef_img)\n    ax[0, i].set_title(\"before normalization\")\n    ax[1, i].imshow(aft_img)\n    ax[1, i].set_title(\"after normalization\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T12:24:47.122892Z","iopub.execute_input":"2023-11-21T12:24:47.123374Z","iopub.status.idle":"2023-11-21T12:24:48.688795Z","shell.execute_reply.started":"2023-11-21T12:24:47.123338Z","shell.execute_reply":"2023-11-21T12:24:48.68771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}