{"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-24T21:16:47.549608Z","iopub.execute_input":"2022-06-24T21:16:47.550907Z","iopub.status.idle":"2022-06-24T21:16:47.666750Z","shell.execute_reply.started":"2022-06-24T21:16:47.550849Z","shell.execute_reply":"2022-06-24T21:16:47.665422Z"},"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-24T21:16:47.670120Z","iopub.execute_input":"2022-06-24T21:16:47.670591Z","iopub.status.idle":"2022-06-24T21:16:47.681368Z","shell.execute_reply.started":"2022-06-24T21:16:47.670552Z","shell.execute_reply":"2022-06-24T21:16:47.679944Z"},"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-24T21:16:47.683582Z","iopub.execute_input":"2022-06-24T21:16:47.684505Z","iopub.status.idle":"2022-06-24T21:16:47.700192Z","shell.execute_reply.started":"2022-06-24T21:16:47.684453Z","shell.execute_reply":"2022-06-24T21:16:47.699312Z"},"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-24T21:16:47.723559Z","iopub.execute_input":"2022-06-24T21:16:47.724204Z","iopub.status.idle":"2022-06-24T21:16:47.728923Z","shell.execute_reply.started":"2022-06-24T21:16:47.724167Z","shell.execute_reply":"2022-06-24T21:16:47.728078Z"},"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\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[y:y+max(h, w), x:x+max(h, w)], mask[y:y+max(h, w), x:x+max(h, w)]\n    ","metadata":{"execution":{"iopub.status.busy":"2022-06-24T21:16:47.809009Z","iopub.execute_input":"2022-06-24T21:16:47.810085Z","iopub.status.idle":"2022-06-24T21:16:47.819827Z","shell.execute_reply.started":"2022-06-24T21:16:47.810040Z","shell.execute_reply":"2022-06-24T21:16:47.818527Z"},"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 f in os.listdir(images):\n    fname = f[:-4]\n    try:\n        image, mask = crop_image(fname + \".jpg\", fname + \".bmp\")\n        \n        tf.keras.utils.save_img(os.path.join(save_dir + \"/images\", fname + '.jpg'), image)\n        tf.keras.utils.save_img(os.path.join(save_dir + \"/masks\", fname + '.jpg'), mask)\n    except:\n        continue","metadata":{"execution":{"iopub.status.busy":"2022-06-24T21:43:56.237503Z","iopub.execute_input":"2022-06-24T21:43:56.238207Z","iopub.status.idle":"2022-06-24T21:44:43.189017Z","shell.execute_reply.started":"2022-06-24T21:43:56.238156Z","shell.execute_reply":"2022-06-24T21:44:43.187420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}