{"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":"# 1. Prepare 準備","metadata":{}},{"cell_type":"code","source":"import os\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-28T12:58:23.470753Z","iopub.execute_input":"2022-08-28T12:58:23.471395Z","iopub.status.idle":"2022-08-28T12:58:23.479248Z","shell.execute_reply.started":"2022-08-28T12:58:23.471347Z","shell.execute_reply":"2022-08-28T12:58:23.477635Z"},"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-28T11:30:09.259359Z","iopub.execute_input":"2022-08-28T11:30:09.261010Z","iopub.status.idle":"2022-08-28T11:30:09.666020Z","shell.execute_reply.started":"2022-08-28T11:30:09.260938Z","shell.execute_reply":"2022-08-28T11:30:09.664772Z"},"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-28T11:29:43.323351Z","iopub.execute_input":"2022-08-28T11:29:43.323781Z","iopub.status.idle":"2022-08-28T11:29:43.334858Z","shell.execute_reply.started":"2022-08-28T11:29:43.323744Z","shell.execute_reply":"2022-08-28T11:29:43.333683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check organ kinds 臓器の種類確認\ndf_train['organ'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-08-28T13:21:19.930240Z","iopub.execute_input":"2022-08-28T13:21:19.930657Z","iopub.status.idle":"2022-08-28T13:21:20.115890Z","shell.execute_reply.started":"2022-08-28T13:21:19.930621Z","shell.execute_reply":"2022-08-28T13:21:20.114862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Edge Detection エッジ検出","metadata":{}},{"cell_type":"code","source":"def show_image(idx,organ):\n    fig, ax = plt.subplots(1,4,figsize=(20,20))\n    df_organ = df_train[df_train['organ']==organ].reset_index()\n    \n    # input gray\n    image = cv2.imread(f\"../input/hubmap-organ-segmentation/train_images/{df_organ.id[idx]}.tiff\",0)\n    \n    # sobel filter\n    img_sobel = np.sqrt(cv2.Sobel(image, cv2.CV_32F, 1, 0, ksize=3) ** 2 + cv2.Sobel(image, cv2.CV_32F, 0, 1, ksize=3) ** 2)\n    \n    # canny filter\n    med_val = np.median(image)\n    sigma = 0.33  # 0.33\n    min_val = int(max(0, (1.0 - sigma) * med_val))\n    max_val = int(max(255, (1.0 + sigma) * med_val))\n    img_canny = cv2.Canny(image, min_val, max_val)\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(\"Gray Scale\")\n    ax[0].axis(\"off\")\n    ax[1].imshow(img_sobel,cmap='gray')\n    ax[1].set_title(\"Sobel Filter\")\n    ax[1].axis(\"off\")\n    ax[2].imshow(img_canny,cmap='gray')\n    ax[2].set_title(\"Canny Filter\")\n    ax[2].axis(\"off\")\n    ax[3].imshow(img_canny,cmap='gray')\n    ax[3].imshow(mask,alpha=0.3)\n    ax[3].set_title(\"Canny Filter + FTU mask\")\n    ax[3].axis(\"off\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-28T13:20:16.650168Z","iopub.execute_input":"2022-08-28T13:20:16.650585Z","iopub.status.idle":"2022-08-28T13:20:16.663624Z","shell.execute_reply.started":"2022-08-28T13:20:16.650548Z","shell.execute_reply":"2022-08-28T13:20:16.662458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Visualization 可視化","metadata":{}},{"cell_type":"code","source":"# Select index 適当な番号を選択\nshow_image(0, 'kidney')\nprint('-'*50)\nshow_image(0, 'prostate')\nprint('-'*50)\nshow_image(0, 'largeintestine')\nprint('-'*50)\nshow_image(0, 'spleen')\nprint('-'*50)\nshow_image(0, 'lung')","metadata":{"execution":{"iopub.status.busy":"2022-08-28T13:25:06.990417Z","iopub.execute_input":"2022-08-28T13:25:06.990912Z","iopub.status.idle":"2022-08-28T13:25:31.767712Z","shell.execute_reply.started":"2022-08-28T13:25:06.990873Z","shell.execute_reply":"2022-08-28T13:25:31.766563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insight**\n- In some images, especially kidneys, the detected edges and FTU boundaries coincide.<br>\n画像によっては、検出したエッジとFTUの境界が一致している。特に腎臓。\n- In some images, especially spleens, I can find little information on FTU.<br>\n画像によっては、FTUの情報がほとんど分からない。特に脾臓。","metadata":{}},{"cell_type":"markdown","source":"# 4. Vizualize All Train Data 全ての学習データ可視化","metadata":{}},{"cell_type":"markdown","source":"## 4-1. kidney 腎臓","metadata":{}},{"cell_type":"code","source":"organ='kidney'\nfor i in range(sum(df_train['organ']==organ)):\n    show_image(i,organ)\n    print('-'*50)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4-2. large intestine 大腸","metadata":{}},{"cell_type":"code","source":"organ='largeintestine'\nfor i in range(sum(df_train['organ']==organ)):\n    show_image(i,organ)\n    print('-'*50)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4-3. lung 肺臓","metadata":{}},{"cell_type":"code","source":"organ='lung'\nfor i in range(sum(df_train['organ']==organ)):\n    show_image(i,organ)\n    print('-'*50)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4-4. spleen 脾臓","metadata":{}},{"cell_type":"code","source":"organ='spleen'\nfor i in range(sum(df_train['organ']==organ)):\n    show_image(i,organ)\n    print('-'*50)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4-5. prostate 前立腺","metadata":{}},{"cell_type":"code","source":"organ='prostate'\nfor i in range(sum(df_train['organ']==organ)):\n    show_image(i,organ)\n    print('-'*50)","metadata":{},"execution_count":null,"outputs":[]}]}