{"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":"import numpy as np\nmask = np.array([[1,0,1,0],\n                 [1,1,0,1],\n                 [1,1,0,0]])\nprint(mask.shape)\n#This code is rle encoding mask to string format.\ndef mask2rle(img):\n    '''efficient implementations of mask2rle\n    --\n    image: numpy array, 1-mask, 0-background\n    returns run length  as string formated\n    '''\n    #Flatten the pixels\n    pixels = img.T.flatten()\n    print( pixels)\n\n    #Concatenate pixels add starting and ending pixels as \"0\"\n    pixels = np.concatenate([[0], pixels, [0]])\n    print(pixels)\n\n    #All the pixels apart from the first one of concatenate pixels()\n    print(pixels[1:])\n\n    #All the pixels apart from the last one of concatenate pixels\n    print(pixels[:-1])\n\n    #If the comparison of the above array in the corresponding index is equal, it will return False. If not, it will return True\n    print(pixels[1:] != pixels[:-1])\n\n    #Finding the inex where \"True\".\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    print(\"Runs where True\",runs)\n\n    #subtact the first from the second  and replace index with subtract value\n    runs[1::2] -= runs[::2]\n    print(runs)#this is the array\n    #conver array to string format\n    return ' '.join(str(x) for x in runs)\nrle = mask2rle(mask)\nprint(\"Decoded string format\", rle)\n\n#This is the rle decoding part to get back the original value\ndef rle_decode(mask_rle: str = '', shape: tuple = (3, 4)):\n    '''\n    Decode rle encoded mask.\n    :param mask_rle: run-length as string formatted (start length)\n    :param shape: (height, width) of array to return\n    Returns numpy array, 1 - mask, 0 - background\n    '''\n    #Split the string to array of string\n    s = mask_rle.split()\n    print(\"S\", s)\n\n    #Find the starts and lengths of decoded string array\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    print(f\"Starts{starts} Lengths{lengths}\")\n\n    #This is the changing from 1 based indexing to zero based indexing\n    starts -= 1\n    print(\"Starts\", starts)\n\n    #if we add starts and lengths, we get the ends\n    ends = starts + lengths\n    print(\"Ends\", ends)\n\n    #Create an empty image array with the same shape of original image\n    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)\n    print(\"Empty image\",img)\n\n    #Get the original mask\n    #for lo(starts) and hi(ends) in pair\n    for lo, hi in zip(starts, ends):\n        #set 1 from start to ends. Note the output is a little tricky\n        img[lo:hi] = 1\n    print(\"Original Flatten Array\",img)\n    print(\"Original mask\\n\",img.reshape(shape, order='F'))\n    return img.reshape(shape, order='F')\n\nrle_decode(rle)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-03T01:43:38.845118Z","iopub.execute_input":"2022-09-03T01:43:38.845663Z","iopub.status.idle":"2022-09-03T01:43:38.880872Z","shell.execute_reply.started":"2022-09-03T01:43:38.845619Z","shell.execute_reply":"2022-09-03T01:43:38.879304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}