{"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\nfrom matplotlib import pyplot as plt\nimport pandas as pd\nimport tqdm\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:21:14.862139Z","iopub.execute_input":"2021-11-13T05:21:14.862851Z","iopub.status.idle":"2021-11-13T05:21:15.173692Z","shell.execute_reply.started":"2021-11-13T05:21:14.862658Z","shell.execute_reply":"2021-11-13T05:21:15.172539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Images are among the most common and popular representations of data. Digital images are used for professional and personal use ranging from official documents to social media. Thus, any Organization or individual needs to store and share a large number of images. One of the most common issues associated with using images is the potentially large file-size of the image. Advancements in image acquisition technology and an increase in the popularity of digital content means that images now have very high resolutions and high quality, inevitably leading to an increase in size. Image compression has become one of the most important parts of image processing these days due to this. The goal is to achieve the least size possible for an image while not compromising on the quality of the image, that gives us the perfect balance.","metadata":{}},{"cell_type":"markdown","source":"#### Understanding the Algorithm\nLet's take the input string as \"wwwwbbbwwwwwbbbbb\". At each step we identify the next character, count it's subsequent occurences and append the character followed by it's number of occurances to the code.\n","metadata":{}},{"cell_type":"markdown","source":"#### Psuedo Code\n1. Start with an empty string for the code, and start traversing the string.\n2. Pick the first character and append it to the code.\n3. Now count the number of subsequent occurances of the currently chosen character and append it to the code.\n4. When you encounter a different character, repeat the above steps.\n5. In a similar manner, traverse the entire string and generate the code.","metadata":{}},{"cell_type":"markdown","source":"#### Decoding\nEven decoding the output is quite straightforward. Whenever you encounter a number, add the character preceeding it that many number of times in your final string.\nFor example, A3D5G7 gets decoded to AAADDDDDGGGGGGG.","metadata":{}},{"cell_type":"markdown","source":"#### Decoding Masks for this competition","metadata":{}},{"cell_type":"code","source":"data = pd.read_csv('../input/sartorius-cell-instance-segmentation/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:21:15.175551Z","iopub.execute_input":"2021-11-13T05:21:15.175831Z","iopub.status.idle":"2021-11-13T05:21:15.868420Z","shell.execute_reply.started":"2021-11-13T05:21:15.175786Z","shell.execute_reply":"2021-11-13T05:21:15.867244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:21:15.870002Z","iopub.execute_input":"2021-11-13T05:21:15.870301Z","iopub.status.idle":"2021-11-13T05:21:15.900170Z","shell.execute_reply.started":"2021-11-13T05:21:15.870270Z","shell.execute_reply":"2021-11-13T05:21:15.899128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_names = data['id'].unique()","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:21:15.902567Z","iopub.execute_input":"2021-11-13T05:21:15.902999Z","iopub.status.idle":"2021-11-13T05:21:15.922241Z","shell.execute_reply.started":"2021-11-13T05:21:15.902955Z","shell.execute_reply":"2021-11-13T05:21:15.921072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_decode(mask_rle, shape, color=1):\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.float32)\n    for lo, hi in zip(starts, ends):\n        img[lo : hi] = color\n    return img.reshape(shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:21:15.923718Z","iopub.execute_input":"2021-11-13T05:21:15.924508Z","iopub.status.idle":"2021-11-13T05:21:15.934736Z","shell.execute_reply.started":"2021-11-13T05:21:15.924458Z","shell.execute_reply":"2021-11-13T05:21:15.933425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum = 0\nfor file in tqdm.tqdm(file_names):\n    main_image = np.zeros(520*704, dtype=np.float32)\n    main_image = main_image.reshape((520,704))\n    #main_image = np.zeros([520,704])\n    get_all_codes = data[data['id'] == file]\n    get_all_codes = get_all_codes.reset_index()\n    for d in range(len(get_all_codes)):\n        #print(get_all_codes.loc[d]['id'])\n        location = './'\n        try:\n            im = rle_decode(get_all_codes.loc[d]['annotation'],(get_all_codes.loc[d]['height'],get_all_codes.loc[d]['width']))\n            main_image = cv2.bitwise_or(main_image,im)\n        except Exception as e:\n            sum+=1\n    # save masks as png image\n    cv2.imwrite(location+file+'.png',main_image)\nprint(sum)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:21:15.937245Z","iopub.execute_input":"2021-11-13T05:21:15.937545Z","iopub.status.idle":"2021-11-13T05:22:32.580767Z","shell.execute_reply.started":"2021-11-13T05:21:15.937508Z","shell.execute_reply":"2021-11-13T05:22:32.580104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread('./0a6ecc5fe78a.png',0)\nplt.imshow(image,'gray')","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:22:32.582125Z","iopub.execute_input":"2021-11-13T05:22:32.582357Z","iopub.status.idle":"2021-11-13T05:22:32.889810Z","shell.execute_reply.started":"2021-11-13T05:22:32.582328Z","shell.execute_reply":"2021-11-13T05:22:32.888744Z"},"trusted":true},"execution_count":null,"outputs":[]}]}