{"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":"# Simple overlap-removing function\n\nHello community,\n\nBelow you can find a 3-line overlap-removing function. \nIt takes two 2D numpy arrays as arguments and returns the first one (`mask`) with all the pixeles that overlapped with the second parameter (`other_mask`) removed.\n\n\nIt is just a modified version of the one used in the kernel [🦠 Sartorius - Starter Torch Mask R-CNN [LB=0.056]](https://www.kaggle.com/julian3833/sartorius-starter-torch-mask-r-cnn-lb-0-056).\n\n\nPlease **do upvote** if you find this useful.","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\ndef remove_overlapping_pixels(mask, other_mask):\n    if np.sum(np.logical_and(mask, other_mask)) > 0:\n        mask[np.logical_and(mask, other_mask)] = 0\n    return mask","metadata":{"execution":{"iopub.status.busy":"2021-10-19T21:33:58.219475Z","iopub.execute_input":"2021-10-19T21:33:58.219874Z","iopub.status.idle":"2021-10-19T21:33:58.23451Z","shell.execute_reply.started":"2021-10-19T21:33:58.219795Z","shell.execute_reply":"2021-10-19T21:33:58.233491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Usage demonstration","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nm1 = np.zeros((520, 704))\nm1[100:200, 100:200] = 1\nplt.imshow(m1);","metadata":{"execution":{"iopub.status.busy":"2021-10-19T21:33:58.239186Z","iopub.execute_input":"2021-10-19T21:33:58.239674Z","iopub.status.idle":"2021-10-19T21:33:58.567219Z","shell.execute_reply.started":"2021-10-19T21:33:58.239639Z","shell.execute_reply":"2021-10-19T21:33:58.566052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m2 = np.zeros((520, 704))\nm2[180:280, 180:280] = 1\nplt.imshow(m2);","metadata":{"execution":{"iopub.status.busy":"2021-10-19T21:33:58.568969Z","iopub.execute_input":"2021-10-19T21:33:58.569848Z","iopub.status.idle":"2021-10-19T21:33:58.874705Z","shell.execute_reply.started":"2021-10-19T21:33:58.56978Z","shell.execute_reply":"2021-10-19T21:33:58.873727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(m1)\nplt.imshow(m2, alpha=0.5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-19T21:33:58.877443Z","iopub.execute_input":"2021-10-19T21:33:58.87785Z","iopub.status.idle":"2021-10-19T21:33:59.183144Z","shell.execute_reply.started":"2021-10-19T21:33:58.877784Z","shell.execute_reply":"2021-10-19T21:33:59.182366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(m1[170:210, 170:210])\nplt.imshow(m2[170:210, 170:210], alpha=0.5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-19T21:33:59.184691Z","iopub.execute_input":"2021-10-19T21:33:59.185233Z","iopub.status.idle":"2021-10-19T21:33:59.395098Z","shell.execute_reply.started":"2021-10-19T21:33:59.185188Z","shell.execute_reply":"2021-10-19T21:33:59.39442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m1_clean = remove_overlapping_pixels(m1, m2)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T21:33:59.39637Z","iopub.execute_input":"2021-10-19T21:33:59.396723Z","iopub.status.idle":"2021-10-19T21:33:59.403807Z","shell.execute_reply.started":"2021-10-19T21:33:59.396691Z","shell.execute_reply":"2021-10-19T21:33:59.402649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(m1_clean);","metadata":{"execution":{"iopub.status.busy":"2021-10-19T21:34:39.777263Z","iopub.execute_input":"2021-10-19T21:34:39.777615Z","iopub.status.idle":"2021-10-19T21:34:40.058208Z","shell.execute_reply.started":"2021-10-19T21:34:39.777578Z","shell.execute_reply":"2021-10-19T21:34:40.057521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(m2);","metadata":{"execution":{"iopub.status.busy":"2021-10-19T21:34:45.09653Z","iopub.execute_input":"2021-10-19T21:34:45.097132Z","iopub.status.idle":"2021-10-19T21:34:45.387475Z","shell.execute_reply.started":"2021-10-19T21:34:45.097079Z","shell.execute_reply":"2021-10-19T21:34:45.386539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(m1_clean)\nplt.imshow(m2, alpha=0.5)\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2021-10-19T21:33:59.406116Z","iopub.execute_input":"2021-10-19T21:33:59.40665Z","iopub.status.idle":"2021-10-19T21:33:59.708192Z","shell.execute_reply.started":"2021-10-19T21:33:59.406465Z","shell.execute_reply":"2021-10-19T21:33:59.707415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(m1_clean[170:210, 170:210])\nplt.imshow(m2[170:210, 170:210], alpha=0.5)\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2021-10-19T21:33:59.709985Z","iopub.execute_input":"2021-10-19T21:33:59.710539Z","iopub.status.idle":"2021-10-19T21:33:59.920614Z","shell.execute_reply.started":"2021-10-19T21:33:59.710489Z","shell.execute_reply":"2021-10-19T21:33:59.919922Z"},"trusted":true},"execution_count":null,"outputs":[]}]}