{"cells":[{"metadata":{"papermill":{"duration":0.009763,"end_time":"2020-11-06T16:58:40.767285","exception":false,"start_time":"2020-11-06T16:58:40.757522","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Converting Polygon Masks to Pixel Masks\nBy Leah Scherschel and Yingnan Ju - Indiana University"},{"metadata":{"papermill":{"duration":0.008709,"end_time":"2020-11-06T16:58:40.78523","exception":false,"start_time":"2020-11-06T16:58:40.776521","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Import packages"},{"metadata":{"execution":{"iopub.execute_input":"2020-11-06T16:58:40.809932Z","iopub.status.busy":"2020-11-06T16:58:40.808978Z","iopub.status.idle":"2020-11-06T16:58:41.241192Z","shell.execute_reply":"2020-11-06T16:58:41.240289Z"},"papermill":{"duration":0.446847,"end_time":"2020-11-06T16:58:41.241319","exception":false,"start_time":"2020-11-06T16:58:40.794472","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import cv2\nimport json\nimport os\nimport csv\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom PIL import Image, ImageDraw","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.008739,"end_time":"2020-11-06T16:58:41.259739","exception":false,"start_time":"2020-11-06T16:58:41.251","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Load polygon mask"},{"metadata":{"execution":{"iopub.execute_input":"2020-11-06T16:58:41.285352Z","iopub.status.busy":"2020-11-06T16:58:41.28437Z","iopub.status.idle":"2020-11-06T16:58:41.297318Z","shell.execute_reply":"2020-11-06T16:58:41.29648Z"},"papermill":{"duration":0.028777,"end_time":"2020-11-06T16:58:41.297466","exception":false,"start_time":"2020-11-06T16:58:41.268689","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"json_filename = '../input/hubmap-kidney-segmentation/train/0486052bb.json'\nread_file = open(json_filename, \"r\") \ndata = json.load(read_file)\ndata[0]\n\npolys = []\nfor index in range(data.__len__()):\n    geom = np.array(data[index]['geometry']['coordinates'])\n    polys.append(geom)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.008973,"end_time":"2020-11-06T16:58:41.317713","exception":false,"start_time":"2020-11-06T16:58:41.30874","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### The shape of the image, h * w"},{"metadata":{"execution":{"iopub.execute_input":"2020-11-06T16:58:41.342395Z","iopub.status.busy":"2020-11-06T16:58:41.341488Z","iopub.status.idle":"2020-11-06T16:58:41.344787Z","shell.execute_reply":"2020-11-06T16:58:41.344055Z"},"papermill":{"duration":0.017736,"end_time":"2020-11-06T16:58:41.344946","exception":false,"start_time":"2020-11-06T16:58:41.32721","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"shape = (25784, 34937)\n","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.008986,"end_time":"2020-11-06T16:58:41.363457","exception":false,"start_time":"2020-11-06T16:58:41.354471","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Reference code for converting polygon vector into pixel masks"},{"metadata":{"papermill":{"duration":0.008965,"end_time":"2020-11-06T16:58:41.381813","exception":false,"start_time":"2020-11-06T16:58:41.372848","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Method 1"},{"metadata":{"execution":{"iopub.execute_input":"2020-11-06T16:58:41.411384Z","iopub.status.busy":"2020-11-06T16:58:41.41061Z","iopub.status.idle":"2020-11-06T16:58:49.535749Z","shell.execute_reply":"2020-11-06T16:58:49.53647Z"},"papermill":{"duration":8.145439,"end_time":"2020-11-06T16:58:49.536636","exception":false,"start_time":"2020-11-06T16:58:41.391197","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"mask_1 = np.zeros(shape)\nfor i in range(len(polys)):\n    cv2.fillPoly(mask_1, polys[i], i+1)\n\nplt.imshow(mask_1)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.010643,"end_time":"2020-11-06T16:58:49.558554","exception":false,"start_time":"2020-11-06T16:58:49.547911","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Method 2"},{"metadata":{"execution":{"iopub.execute_input":"2020-11-06T16:58:49.5908Z","iopub.status.busy":"2020-11-06T16:58:49.589972Z","iopub.status.idle":"2020-11-06T16:58:54.766004Z","shell.execute_reply":"2020-11-06T16:58:54.765388Z"},"papermill":{"duration":5.196571,"end_time":"2020-11-06T16:58:54.766124","exception":false,"start_time":"2020-11-06T16:58:49.569553","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"Image.MAX_IMAGE_PIXELS = None\nimg = Image.new('L', (shape[1], shape[0]), 0)  # (w, h)\nfor i in range(len(polys)):\n    poly = polys[i]\n    ImageDraw.Draw(img).polygon(tuple(map(tuple, poly[0])), outline=i + 1, fill=i + 1) \n\nmask_2 = np.array(img)\nplt.imshow(mask_2)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}