{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nfrom tensorflow import keras\n\nimport os\nfrom matplotlib import pyplot as plt\nimport cv2\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        break\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-25T01:04:43.588072Z","iopub.execute_input":"2022-06-25T01:04:43.588814Z","iopub.status.idle":"2022-06-25T01:04:48.535684Z","shell.execute_reply.started":"2022-06-25T01:04:43.588714Z","shell.execute_reply":"2022-06-25T01:04:48.533526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def remove_nerves(image):\n    img = tf.keras.utils.array_to_img(image)\n    # fig = plt.figure(figsize=(50,20))\n    # plt.title('Original'); plt.imshow(img); plt.show()\n    orig_size = np.array(img).shape[:2]\n    orig_size = (orig_size[1], orig_size[0])\n    # print(orig_size)\n    img = cv2.resize(np.array(img),(1024,1024))\n    img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)\n    # plt.subplot(1, 5, 1)\n    # plt.imshow(img)\n    # plt.axis('off')\n    # plt.title('Original')\n\n    # convert image to grayScale\n    grayScale = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n    # plt.subplot(1, 5, 2)\n    # plt.imshow(grayScale)\n    # plt.axis('off')\n    # plt.title('GrayScale')\n\n    # kernel for morphologyEx\n    kernel = cv2.getStructuringElement(1,(17,17))\n\n\n    # apply MORPH_BLACKHAT to grayScale image\n    blackhat = cv2.morphologyEx(grayScale, cv2.MORPH_BLACKHAT, kernel)\n    # plt.subplot(1, 5, 3)\n    # plt.imshow(blackhat)\n    # plt.axis('off')\n    # plt.title('blackhat')\n\n    # apply thresholding to blackhat\n    _,threshold = cv2.threshold(blackhat,10,255,cv2.THRESH_BINARY)\n    # plt.subplot(1, 5, 4)\n    # plt.imshow(threshold)\n    # plt.axis('off')\n    # plt.title('threshold')\n\n    # inpaint with original image and threshold image\n    final_image = cv2.inpaint(img,threshold,1,cv2.INPAINT_TELEA)\n    # plt.subplot(1, 5, 5)\n    # plt.imshow(cv2.cvtColor(final_image, cv2.COLOR_BGR2RGB))\n    # plt.axis('off')\n    # plt.title('final_image')\n\n    # plt.plot()\n    final_image = cv2.cvtColor(final_image, cv2.COLOR_BGR2RGB)\n    final_image = cv2.resize(final_image,orig_size)\n    # plt.title('No Nerve'); plt.imshow(final_image); plt.show()\n    return final_image#.astype(np.float64)/255.0","metadata":{"execution":{"iopub.status.busy":"2022-06-25T01:04:48.539071Z","iopub.execute_input":"2022-06-25T01:04:48.539859Z","iopub.status.idle":"2022-06-25T01:04:48.552038Z","shell.execute_reply.started":"2022-06-25T01:04:48.539829Z","shell.execute_reply":"2022-06-25T01:04:48.550943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR = '/kaggle/input/eurecom-aml-2021-challenge-2/refuge_data/refuge_data/train/'\nos.listdir(TRAIN_DIR)","metadata":{"execution":{"iopub.status.busy":"2022-06-25T16:36:25.148923Z","iopub.execute_input":"2022-06-25T16:36:25.149903Z","iopub.status.idle":"2022-06-25T16:36:25.229646Z","shell.execute_reply.started":"2022-06-25T16:36:25.149799Z","shell.execute_reply":"2022-06-25T16:36:25.228238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = os.path.join(TRAIN_DIR, 'images')\nmasks = os.path.join(TRAIN_DIR, 'gts')","metadata":{"execution":{"iopub.status.busy":"2022-06-25T01:04:48.718076Z","iopub.execute_input":"2022-06-25T01:04:48.718454Z","iopub.status.idle":"2022-06-25T01:04:48.723496Z","shell.execute_reply.started":"2022-06-25T01:04:48.718419Z","shell.execute_reply":"2022-06-25T01:04:48.722616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\ndef crop_image(image_path, mask_path, remove_nerves=False):\n    image = cv2.cvtColor(cv2.imread(os.path.join(images, image_path)), cv2.COLOR_BGR2RGB)\n    raw_mask = np.array(Image.open(os.path.join(masks, mask_path), mode='r'))\n    mask = raw_mask.copy()\n    raw_mask[raw_mask == 128] = 1\n    raw_mask[raw_mask == 0] = 2\n    raw_mask[raw_mask == 255] = 0\n\n    \n    # invert mask colors\n    mask[mask == 255] = 100\n    mask[mask == 128] = 0\n    mask[mask == 0] = 1\n    mask[mask == 100] = 0\n    ret,thresh = cv2.threshold(mask,0, 1, 0)\n    contours,hierarchy = cv2.findContours(thresh, 1, 2)\n    cnt = contours[0]\n    x,y,w,h = cv2.boundingRect(cnt)\n    return image, raw_mask\n    ","metadata":{"execution":{"iopub.status.busy":"2022-06-25T01:04:50.900656Z","iopub.execute_input":"2022-06-25T01:04:50.900989Z","iopub.status.idle":"2022-06-25T01:04:50.911199Z","shell.execute_reply.started":"2022-06-25T01:04:50.900959Z","shell.execute_reply":"2022-06-25T01:04:50.910292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_dir = \"./train_cropped\"\n!mkdir \"train_cropped\"\n!mkdir \"train_cropped/images\"\n!mkdir \"train_cropped/masks\"\n\nfor ind, f in enumerate(os.listdir(images)):\n    fname = f[:-4]\n    image, mask = crop_image(fname + \".jpg\", fname + \".bmp\")\n    #print(ind)\n    msk = Image.fromarray(mask.astype('uint8'), 'L')\n    msk.save(os.path.join(save_dir + \"/masks\", fname + '.png'))\n#     im = Image.fromarray(image)\n#     im.save(os.path.join(save_dir + \"/images\", fname + '.jpg'))","metadata":{"execution":{"iopub.status.busy":"2022-06-25T01:05:14.903025Z","iopub.execute_input":"2022-06-25T01:05:14.90362Z","iopub.status.idle":"2022-06-25T01:06:35.475347Z","shell.execute_reply.started":"2022-06-25T01:05:14.903584Z","shell.execute_reply":"2022-06-25T01:06:35.474192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/train_cropped\n%ls images/","metadata":{"execution":{"iopub.status.busy":"2022-06-25T01:07:37.182982Z","iopub.execute_input":"2022-06-25T01:07:37.183416Z","iopub.status.idle":"2022-06-25T01:07:37.857502Z","shell.execute_reply.started":"2022-06-25T01:07:37.183376Z","shell.execute_reply":"2022-06-25T01:07:37.856428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/train_cropped\nfrom IPython.display import FileLinks\n#FileLinks(r'masks')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}