{"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":"# Blood Vessel Mask to RLE","metadata":{"papermill":{"duration":0.005342,"end_time":"2023-10-31T14:51:40.547123","exception":false,"start_time":"2023-10-31T14:51:40.541781","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"After generating a predicted mask image from a test image, you can submit it by adjusting mask image to the size of original image and then performing RLE conversion.","metadata":{"papermill":{"duration":0.004008,"end_time":"2023-10-31T14:51:40.555603","exception":false,"start_time":"2023-10-31T14:51:40.551595","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport os\nfrom PIL import Image\nimport random\n%matplotlib inline","metadata":{"papermill":{"duration":0.315716,"end_time":"2023-10-31T14:51:40.883713","exception":false,"start_time":"2023-10-31T14:51:40.567997","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-11T03:05:03.553141Z","iopub.execute_input":"2023-11-11T03:05:03.553523Z","iopub.status.idle":"2023-11-11T03:05:03.561849Z","shell.execute_reply.started":"2023-11-11T03:05:03.553495Z","shell.execute_reply":"2023-11-11T03:05:03.560486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path0 ='/kaggle/input/blood-vessel-segmentation/train/kidney_1_voi/images/0040.tif' \nmpath0 ='/kaggle/input/blood-vessel-segmentation/train/kidney_1_voi/labels/0040.tif' ","metadata":{"execution":{"iopub.status.busy":"2023-11-11T03:05:03.563687Z","iopub.execute_input":"2023-11-11T03:05:03.564908Z","iopub.status.idle":"2023-11-11T03:05:03.571425Z","shell.execute_reply.started":"2023-11-11T03:05:03.564866Z","shell.execute_reply":"2023-11-11T03:05:03.570100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(path0)\nfile=path0.split('/')[-1]\nplt.title(file)\nplt.imshow(img)\nplt.axis('off')\nplt.show()\n\nmimg = cv2.imread(mpath0)\nfile=mpath0.split('/')[-1]\nplt.title(file)\nplt.imshow(mimg)\nplt.axis('off')\nplt.show()\n\nprint(img.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-11T03:05:03.573855Z","iopub.execute_input":"2023-11-11T03:05:03.574238Z","iopub.status.idle":"2023-11-11T03:05:05.084832Z","shell.execute_reply.started":"2023-11-11T03:05:03.574207Z","shell.execute_reply":"2023-11-11T03:05:05.083488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mimg.min(),mimg.max()","metadata":{"papermill":{"duration":0.014533,"end_time":"2023-10-31T14:51:41.021937","exception":false,"start_time":"2023-10-31T14:51:41.007404","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-11T03:05:05.087362Z","iopub.execute_input":"2023-11-11T03:05:05.087839Z","iopub.status.idle":"2023-11-11T03:05:05.099268Z","shell.execute_reply.started":"2023-11-11T03:05:05.087796Z","shell.execute_reply":"2023-11-11T03:05:05.097958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# these functions are given in https://www.kaggle.com/code/inversion/contrails-rle-submission\n\ndef rle_encode(x, fg_val=1):    \n    dots = np.where(\n        x.T.flatten() == fg_val)[0]  # .T sets Fortran order down-then-right\n    run_lengths = []\n    prev = -2\n    for b in dots:\n        if b > prev + 1:\n            run_lengths.extend((b+1, 0))\n        run_lengths[-1] += 1\n        prev = b\n    return run_lengths\n\n# Represent the RLE for an empty mask as 1 0.\ndef list_to_string(x):\n    if x: # non-empty list\n        s = str(x).replace(\"[\", \"\").replace(\"]\", \"\").replace(\",\", \"\")\n    else:\n        s = '1 0'#'-'\n    return s\n\n# Represent the RLE for an empty mask as 1 0.\ndef rle_decode(mask_rle, shape=(256, 256)):\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    if mask_rle != '1 0': #'-'\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        for lo, hi in zip(starts, ends):\n            img[lo:hi] = 1\n    return img.reshape(shape, order='F')  # Needed to align to RLE direction","metadata":{"execution":{"iopub.status.busy":"2023-11-11T03:05:05.100807Z","iopub.execute_input":"2023-11-11T03:05:05.101116Z","iopub.status.idle":"2023-11-11T03:05:05.113928Z","shell.execute_reply.started":"2023-11-11T03:05:05.101082Z","shell.execute_reply":"2023-11-11T03:05:05.112808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# rle_encode","metadata":{}},{"cell_type":"code","source":"rle0=rle_encode(mimg//255)\nrle1=list_to_string(rle0)\nprint(rle1[0:2000])","metadata":{"execution":{"iopub.status.busy":"2023-11-11T03:24:10.422022Z","iopub.execute_input":"2023-11-11T03:24:10.422404Z","iopub.status.idle":"2023-11-11T03:24:10.553112Z","shell.execute_reply.started":"2023-11-11T03:24:10.422376Z","shell.execute_reply":"2023-11-11T03:24:10.551656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# rle_decode","metadata":{}},{"cell_type":"code","source":"mimg2=rle_decode(rle1,shape=(img.shape[0],img.shape[1]))\nplt.imshow(mimg2)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-11T03:06:30.121642Z","iopub.execute_input":"2023-11-11T03:06:30.122061Z","iopub.status.idle":"2023-11-11T03:06:30.502483Z","shell.execute_reply.started":"2023-11-11T03:06:30.122031Z","shell.execute_reply":"2023-11-11T03:06:30.501431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.021393,"end_time":"2023-10-31T14:51:47.503365","exception":false,"start_time":"2023-10-31T14:51:47.481972","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}