{"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":"# How to understand annotations / encoded pixels ?","metadata":{}},{"cell_type":"markdown","source":"lets understand how to generate masks, if EncodedPixels are given instead of mask images.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2021-12-14T04:56:43.589679Z","iopub.execute_input":"2021-12-14T04:56:43.589947Z","iopub.status.idle":"2021-12-14T04:56:43.593702Z","shell.execute_reply.started":"2021-12-14T04:56:43.589919Z","shell.execute_reply":"2021-12-14T04:56:43.593103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Shape of RGB image is (704,520,3). It means there are 704 X 520 = 3,66,080 locations.Its corresponding mask (704,520) is not given, instead they gave us EncodedPixels.**\n","metadata":{}},{"cell_type":"markdown","source":"**In the first image 0030fd0e6378 we have annotations as 118145 6 118849 7 119553 8 120257 8 120961 9 121665 10 122369 12 123074 13 123778 14 124482 15 12518... which means take 6 pixels starting from 118145 , take 7 pixels starting from 118849 , take 8 pixels starting from 119553 ....so on**","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv('../input/sartorius-cell-instance-segmentation/train.csv',nrows=1)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T04:56:49.165530Z","iopub.execute_input":"2021-12-14T04:56:49.166047Z","iopub.status.idle":"2021-12-14T04:56:49.187136Z","shell.execute_reply.started":"2021-12-14T04:56:49.166011Z","shell.execute_reply":"2021-12-14T04:56:49.186548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2021-12-14T04:57:05.752256Z","iopub.execute_input":"2021-12-14T04:57:05.752989Z","iopub.status.idle":"2021-12-14T04:57:05.774417Z","shell.execute_reply.started":"2021-12-14T04:57:05.752951Z","shell.execute_reply":"2021-12-14T04:57:05.773558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.annotation","metadata":{"execution":{"iopub.status.busy":"2021-12-14T04:57:08.583627Z","iopub.execute_input":"2021-12-14T04:57:08.583930Z","iopub.status.idle":"2021-12-14T04:57:08.593910Z","shell.execute_reply.started":"2021-12-14T04:57:08.583898Z","shell.execute_reply":"2021-12-14T04:57:08.593051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"en_pix=list(df.annotation)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T04:57:11.029828Z","iopub.execute_input":"2021-12-14T04:57:11.030913Z","iopub.status.idle":"2021-12-14T04:57:11.035598Z","shell.execute_reply.started":"2021-12-14T04:57:11.030863Z","shell.execute_reply":"2021-12-14T04:57:11.034713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"en_pix","metadata":{"execution":{"iopub.status.busy":"2021-12-14T04:57:13.248478Z","iopub.execute_input":"2021-12-14T04:57:13.248767Z","iopub.status.idle":"2021-12-14T04:57:13.255681Z","shell.execute_reply.started":"2021-12-14T04:57:13.248735Z","shell.execute_reply":"2021-12-14T04:57:13.254829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This is given Encoded Pixels, lets generate a mask using it.\n\nFirst lets split these numbers at space using split function:","metadata":{}},{"cell_type":"code","source":"print(len(en_pix[0].split(' ')))\nen_pix[0].split(' ')","metadata":{"execution":{"iopub.status.busy":"2021-12-14T04:57:39.431399Z","iopub.execute_input":"2021-12-14T04:57:39.431686Z","iopub.status.idle":"2021-12-14T04:57:39.439339Z","shell.execute_reply.started":"2021-12-14T04:57:39.431655Z","shell.execute_reply":"2021-12-14T04:57:39.438470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Lets convert these strings into integers using map function","metadata":{}},{"cell_type":"code","source":"rle = list(map(int, en_pix[0].split(' ')))\nrle","metadata":{"execution":{"iopub.status.busy":"2021-12-14T04:58:31.708013Z","iopub.execute_input":"2021-12-14T04:58:31.708273Z","iopub.status.idle":"2021-12-14T04:58:31.714793Z","shell.execute_reply.started":"2021-12-14T04:58:31.708246Z","shell.execute_reply":"2021-12-14T04:58:31.713997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"First number is starting pixel and next number is count from that starting pixel. Lets bring them into 2 seperate lists","metadata":{}},{"cell_type":"code","source":"pixel,pixel_count = [],[]\n[pixel.append(rle[i]) if i%2==0 else pixel_count.append(rle[i]) for i in range(0, len(rle))]\nprint('pixel starting points:\\n',pixel)\nprint('pixel counting:\\n', pixel_count)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T04:59:39.643961Z","iopub.execute_input":"2021-12-14T04:59:39.644258Z","iopub.status.idle":"2021-12-14T04:59:39.652871Z","shell.execute_reply.started":"2021-12-14T04:59:39.644226Z","shell.execute_reply":"2021-12-14T04:59:39.651911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Lets generate masked pixel locations where exactly the mask is there using above 2 lists.","metadata":{}},{"cell_type":"code","source":"rle_pixels = [list(range(pixel[i],pixel[i]+pixel_count[i])) for i in range(0, len(pixel))]\nprint('rle_pixels\\n:', rle_pixels)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:01:28.033050Z","iopub.execute_input":"2021-12-14T05:01:28.033370Z","iopub.status.idle":"2021-12-14T05:01:28.038942Z","shell.execute_reply.started":"2021-12-14T05:01:28.033338Z","shell.execute_reply":"2021-12-14T05:01:28.037857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now lets convert list of lists into a single list,","metadata":{}},{"cell_type":"code","source":"rle_mask_pixels = sum(rle_pixels,[]) \nprint('rle mask pixels:\\n', rle_mask_pixels)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:02:51.892749Z","iopub.execute_input":"2021-12-14T05:02:51.893090Z","iopub.status.idle":"2021-12-14T05:02:51.898432Z","shell.execute_reply.started":"2021-12-14T05:02:51.893057Z","shell.execute_reply":"2021-12-14T05:02:51.897796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So, at these locations, input image should be masked. Lets take a black mask and make it white at these pixel locations.\n\nLoad and display the input image:","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:05:37.234037Z","iopub.execute_input":"2021-12-14T05:05:37.234449Z","iopub.status.idle":"2021-12-14T05:05:37.632035Z","shell.execute_reply.started":"2021-12-14T05:05:37.234410Z","shell.execute_reply":"2021-12-14T05:05:37.631360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(cv2.imread('../input/sartorius-cell-instance-segmentation/train/0030fd0e6378.png'))","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:05:39.578316Z","iopub.execute_input":"2021-12-14T05:05:39.578601Z","iopub.status.idle":"2021-12-14T05:05:39.953651Z","shell.execute_reply.started":"2021-12-14T05:05:39.578570Z","shell.execute_reply":"2021-12-14T05:05:39.952469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_img = np.zeros((520*704,1), dtype=int)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:23:40.950774Z","iopub.execute_input":"2021-12-14T05:23:40.951743Z","iopub.status.idle":"2021-12-14T05:23:40.956888Z","shell.execute_reply.started":"2021-12-14T05:23:40.951685Z","shell.execute_reply":"2021-12-14T05:23:40.955906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Lets replace the above mentioned locations (rle_mask_pixels) with white pixels(255).","metadata":{}},{"cell_type":"code","source":"mask_img[rle_mask_pixels] = 255","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:23:45.350096Z","iopub.execute_input":"2021-12-14T05:23:45.350736Z","iopub.status.idle":"2021-12-14T05:23:45.355766Z","shell.execute_reply.started":"2021-12-14T05:23:45.350683Z","shell.execute_reply":"2021-12-14T05:23:45.354866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Reshape the array into input image size","metadata":{}},{"cell_type":"code","source":"l,b=cv2.imread('../input/sartorius-cell-instance-segmentation/train/0030fd0e6378.png').shape[0],cv2.imread('../input/sartorius-cell-instance-segmentation/train/0030fd0e6378.png').shape[1]\nmask = np.reshape(mask_img, (b, l)).T","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:23:47.905662Z","iopub.execute_input":"2021-12-14T05:23:47.906245Z","iopub.status.idle":"2021-12-14T05:23:47.930517Z","shell.execute_reply.started":"2021-12-14T05:23:47.906178Z","shell.execute_reply":"2021-12-14T05:23:47.929443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Lets display the final mask image:","metadata":{}},{"cell_type":"code","source":"plt.imshow(mask)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:23:50.294043Z","iopub.execute_input":"2021-12-14T05:23:50.295033Z","iopub.status.idle":"2021-12-14T05:23:50.565991Z","shell.execute_reply.started":"2021-12-14T05:23:50.294982Z","shell.execute_reply":"2021-12-14T05:23:50.564990Z"},"trusted":true},"execution_count":null,"outputs":[]}]}