{"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":"# Run Length Encoding: Annotation Data to Mask Image\nThis notebook refferred to INVERSION's notebook'Run Length Decoding - Quick Start'.<br/>\nhttps://www.kaggle.com/inversion/run-length-decoding-quick-start","metadata":{}},{"cell_type":"markdown","source":"### What is Run-length encoding (RLE)?\n```\nRun-length encoding (RLE) is a form of lossless data compression in which runs of data (sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count, rather than as the original run. This is most efficient on data that contains many such runs, for example, simple graphic images such as icons, line drawings, Conway's Game of Life, and animations. For files that do not have many runs, RLE could increase the file size. (from Wikipedia)\n```","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2021-11-12T08:25:35.614435Z","iopub.execute_input":"2021-11-12T08:25:35.615487Z","iopub.status.idle":"2021-11-12T08:25:35.622163Z","shell.execute_reply.started":"2021-11-12T08:25:35.615420Z","shell.execute_reply":"2021-11-12T08:25:35.620964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = os.listdir('../input/sartorius-cell-instance-segmentation/train')\nprint(len(train))","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2021-11-12T08:25:35.624484Z","iopub.execute_input":"2021-11-12T08:25:35.624858Z","iopub.status.idle":"2021-11-12T08:25:35.642744Z","shell.execute_reply.started":"2021-11-12T08:25:35.624809Z","shell.execute_reply":"2021-11-12T08:25:35.641464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\ndef rle_decode(mask_rle, shape=(520,704)):   # (height,width) \n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\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    \n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape) ","metadata":{"_uuid":"6122ccb9e58bfac6fa5e11c86121e78d9e5151b1","execution":{"iopub.status.busy":"2021-11-12T08:25:35.644129Z","iopub.execute_input":"2021-11-12T08:25:35.644651Z","iopub.status.idle":"2021-11-12T08:25:35.652643Z","shell.execute_reply.started":"2021-11-12T08:25:35.644547Z","shell.execute_reply":"2021-11-12T08:25:35.651859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks = pd.read_csv('../input/sartorius-cell-instance-segmentation/train.csv')\nmasks","metadata":{"_uuid":"206104f888afa9c62a0bbcb45229f58111094f18","execution":{"iopub.status.busy":"2021-11-12T08:25:35.653825Z","iopub.execute_input":"2021-11-12T08:25:35.654948Z","iopub.status.idle":"2021-11-12T08:25:36.076698Z","shell.execute_reply.started":"2021-11-12T08:25:35.654907Z","shell.execute_reply":"2021-11-12T08:25:36.075931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ImageId = '0030fd0e6378'\nimg = cv2.imread('../input/sartorius-cell-instance-segmentation/train/' + ImageId+'.png')\nprint(img.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-12T08:25:36.078514Z","iopub.execute_input":"2021-11-12T08:25:36.078892Z","iopub.status.idle":"2021-11-12T08:25:36.094636Z","shell.execute_reply.started":"2021-11-12T08:25:36.078862Z","shell.execute_reply":"2021-11-12T08:25:36.093710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axarr = plt.subplots(figsize=(5,5))\naxarr.axis('off')\naxarr.imshow(img)\nplt.tight_layout(h_pad=0.1, w_pad=0.1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-12T08:25:36.095942Z","iopub.execute_input":"2021-11-12T08:25:36.096746Z","iopub.status.idle":"2021-11-12T08:25:36.329565Z","shell.execute_reply.started":"2021-11-12T08:25:36.096704Z","shell.execute_reply":"2021-11-12T08:25:36.328379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_masks = masks[masks['id']==ImageId]['annotation'].tolist()\nprint(len(img_masks))\nprint(img_masks[0])","metadata":{"execution":{"iopub.status.busy":"2021-11-12T08:25:36.331164Z","iopub.execute_input":"2021-11-12T08:25:36.331427Z","iopub.status.idle":"2021-11-12T08:25:36.350919Z","shell.execute_reply.started":"2021-11-12T08:25:36.331398Z","shell.execute_reply":"2021-11-12T08:25:36.349790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_masks = np.zeros((520,704))    #(height,width) \nfor mask in img_masks:\n    all_masks += rle_decode(mask)\n\nprint(all_masks.shape)","metadata":{"_uuid":"63e18e8573dbb3fe1d3ff1d72b6dc756e067d43a","execution":{"iopub.status.busy":"2021-11-12T08:25:36.352135Z","iopub.execute_input":"2021-11-12T08:25:36.352479Z","iopub.status.idle":"2021-11-12T08:25:36.541911Z","shell.execute_reply.started":"2021-11-12T08:25:36.352447Z","shell.execute_reply":"2021-11-12T08:25:36.540735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axarr = plt.subplots(1,3, figsize=(15,40))\n\naxarr[0].axis('off')\naxarr[1].axis('off')\naxarr[2].axis('off')\n\naxarr[0].imshow(img)\naxarr[1].imshow(all_masks)\naxarr[2].imshow(img)\naxarr[2].imshow(all_masks, alpha=0.2)\n\nplt.tight_layout(h_pad=0.1, w_pad=0.1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-12T08:25:36.543402Z","iopub.execute_input":"2021-11-12T08:25:36.543699Z","iopub.status.idle":"2021-11-12T08:25:37.067518Z","shell.execute_reply.started":"2021-11-12T08:25:36.543668Z","shell.execute_reply":"2021-11-12T08:25:37.066227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}