{"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":"# Image adaptive equalization by CLAHE\n\nThis notebook is created from [[日本語/Eng]Edge Detection](https://www.kaggle.com/code/utm529fg/eng-edge-detection)","metadata":{}},{"cell_type":"markdown","source":"## Adaptive equalization\nRather than applying a single transformation to all pixels in an image, adaptive histogram equalization applies transformations to local regions in an image. As a result, adaptive equalization allows contrast to be enhanced to different extents in different regions based on the regions' intensity histograms.","metadata":{}},{"cell_type":"markdown","source":"# 1. Prepare","metadata":{}},{"cell_type":"code","source":"import os\nimport random\nimport cv2\nimport tifffile\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-30T14:31:42.220581Z","iopub.execute_input":"2022-08-30T14:31:42.221016Z","iopub.status.idle":"2022-08-30T14:31:42.227379Z","shell.execute_reply.started":"2022-08-30T14:31:42.220984Z","shell.execute_reply":"2022-08-30T14:31:42.226186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-30T14:03:24.903440Z","iopub.execute_input":"2022-08-30T14:03:24.903806Z","iopub.status.idle":"2022-08-30T14:03:25.267634Z","shell.execute_reply.started":"2022-08-30T14:03:24.903774Z","shell.execute_reply":"2022-08-30T14:03:25.266306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://www.kaggle.com/code/pestipeti/decoding-rle-masks/notebook\ndef mask2rle(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels= img.T.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\n\ndef rle2mask(mask_rle, shape=(3000,3000)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0::2], s[1::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T","metadata":{"execution":{"iopub.status.busy":"2022-08-30T14:03:25.269922Z","iopub.execute_input":"2022-08-30T14:03:25.270522Z","iopub.status.idle":"2022-08-30T14:03:25.284908Z","shell.execute_reply.started":"2022-08-30T14:03:25.270460Z","shell.execute_reply":"2022-08-30T14:03:25.282984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check organ kinds 臓器の種類確認\norgan_count = df_train['organ'].value_counts()\norgan_count.plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T14:28:20.852186Z","iopub.execute_input":"2022-08-30T14:28:20.852672Z","iopub.status.idle":"2022-08-30T14:28:21.068621Z","shell.execute_reply.started":"2022-08-30T14:28:20.852637Z","shell.execute_reply":"2022-08-30T14:28:21.067306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Adaptive equalization","metadata":{}},{"cell_type":"code","source":"def show_image(organ):\n    idx = random.randint(0, organ_count[organ])\n    fig, ax = plt.subplots(1,3,figsize=(30,30))\n    df_organ = df_train[df_train['organ']==organ].reset_index()\n    \n    # input image\n    image = cv2.imread(f\"../input/hubmap-organ-segmentation/train_images/{df_organ.id[idx]}.tiff\")\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    \n    #CLAHE\n    img_clahe = []\n    for i in range(3):\n        img_clahe.append(cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)).apply(image[:,:,i]).copy())\n        \n    img_clahe = np.array(img_clahe).transpose(1,2,0)\n    \n    # mask\n    mask = rle2mask(df_organ.rle[idx],shape=(df_organ.img_height[idx],df_organ.img_width[idx]))\n    \n    # show\n    display(pd.DataFrame(df_organ.loc[idx,['id','organ','age','sex']]).T)\n    ax[0].imshow(image,cmap='gray')\n    ax[0].set_title(\"image\")\n    ax[0].axis(\"off\")\n    ax[1].imshow(img_clahe,cmap='gray')\n    ax[1].set_title(\"CLAHE Filter\")\n    ax[1].axis(\"off\")\n    ax[2].imshow(img_clahe,cmap='gray')\n    ax[2].imshow(mask,alpha=0.3)\n    ax[2].set_title(\"CLAHE Filter + FTU mask\")\n    ax[2].axis(\"off\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T14:34:26.273558Z","iopub.execute_input":"2022-08-30T14:34:26.273982Z","iopub.status.idle":"2022-08-30T14:34:26.286717Z","shell.execute_reply.started":"2022-08-30T14:34:26.273947Z","shell.execute_reply":"2022-08-30T14:34:26.285283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Visualization","metadata":{}},{"cell_type":"code","source":"# Select index\nshow_image('kidney')\nprint('-'*50)\nshow_image('prostate')\nprint('-'*50)\nshow_image('largeintestine')\nprint('-'*50)\nshow_image('spleen')\nprint('-'*50)\nshow_image('lung')","metadata":{"execution":{"iopub.status.busy":"2022-08-30T14:34:26.909652Z","iopub.execute_input":"2022-08-30T14:34:26.910062Z","iopub.status.idle":"2022-08-30T14:34:55.789718Z","shell.execute_reply.started":"2022-08-30T14:34:26.910028Z","shell.execute_reply":"2022-08-30T14:34:55.788305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insight**\n","metadata":{}},{"cell_type":"markdown","source":"# 4. Vizualize Some Train Data","metadata":{}},{"cell_type":"code","source":"n_sample = 5","metadata":{"execution":{"iopub.status.busy":"2022-08-30T14:31:53.254739Z","iopub.execute_input":"2022-08-30T14:31:53.255656Z","iopub.status.idle":"2022-08-30T14:31:53.261060Z","shell.execute_reply.started":"2022-08-30T14:31:53.255613Z","shell.execute_reply":"2022-08-30T14:31:53.259689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4-1. kidney","metadata":{}},{"cell_type":"code","source":"organ='kidney'\nfor _ in range(n_sample):\n    show_image(organ)\n    print('-'*50)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T14:31:54.260467Z","iopub.execute_input":"2022-08-30T14:31:54.261686Z","iopub.status.idle":"2022-08-30T14:32:22.433482Z","shell.execute_reply.started":"2022-08-30T14:31:54.261641Z","shell.execute_reply":"2022-08-30T14:32:22.432289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4-2. large intestine","metadata":{}},{"cell_type":"code","source":"organ='largeintestine'\nfor _ in range(n_sample):\n    show_image(organ)\n    print('-'*50)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T13:35:43.852267Z","iopub.status.idle":"2022-08-30T13:35:43.852714Z","shell.execute_reply.started":"2022-08-30T13:35:43.852506Z","shell.execute_reply":"2022-08-30T13:35:43.852528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4-3. lung ","metadata":{}},{"cell_type":"code","source":"organ='lung'\nfor _ in range(n_sample):\n    show_image(organ)\n    print('-'*50)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T13:35:43.854157Z","iopub.status.idle":"2022-08-30T13:35:43.855043Z","shell.execute_reply.started":"2022-08-30T13:35:43.854388Z","shell.execute_reply":"2022-08-30T13:35:43.854416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4-4. spleen ","metadata":{}},{"cell_type":"code","source":"organ='spleen'\nfor _ in range(n_sample):\n    show_image(organ)\n    print('-'*50)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T13:35:43.857786Z","iopub.status.idle":"2022-08-30T13:35:43.858801Z","shell.execute_reply.started":"2022-08-30T13:35:43.858481Z","shell.execute_reply":"2022-08-30T13:35:43.858513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4-5. prostate ","metadata":{}},{"cell_type":"code","source":"organ='prostate'\nfor _ in range(n_sample):\n    show_image(organ)\n    print('-'*50)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T13:35:43.860594Z","iopub.status.idle":"2022-08-30T13:35:43.861569Z","shell.execute_reply.started":"2022-08-30T13:35:43.861262Z","shell.execute_reply":"2022-08-30T13:35:43.861292Z"},"trusted":true},"execution_count":null,"outputs":[]}]}