{"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 pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom PIL import Image\nimport tifffile as tiff \nfrom skimage.measure import label, regionprops\n","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:30:45.076471Z","iopub.execute_input":"2022-06-27T03:30:45.079156Z","iopub.status.idle":"2022-06-27T03:30:46.086900Z","shell.execute_reply.started":"2022-06-27T03:30:45.079082Z","shell.execute_reply":"2022-06-27T03:30:46.085482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/hubmap-organ-segmentation/train.csv\")\ndf['image_path'] = '../input/hubmap-organ-segmentation/train_images/'\ndf['image_path'] = df['image_path'].str.cat(df['id'].astype(str))\ndf['image_path'] = df['image_path'] + '.tiff'\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:11:59.644082Z","iopub.execute_input":"2022-06-27T03:11:59.644517Z","iopub.status.idle":"2022-06-27T03:11:59.823048Z","shell.execute_reply.started":"2022-06-27T03:11:59.644486Z","shell.execute_reply":"2022-06-27T03:11:59.821592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rleToMask(rleString,height,width):\n  rows,cols = height,width\n  rleNumbers = [int(numstring) for numstring in rleString.split(' ')]\n  rlePairs = np.array(rleNumbers).reshape(-1,2)\n  img = np.zeros(rows*cols,dtype=np.uint8)\n  for index,length in rlePairs:\n    index -= 1\n    img[index:index+length] = 255\n  img = img.reshape(cols,rows)\n  img = img.T\n  return img","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:07:49.521163Z","iopub.execute_input":"2022-06-27T03:07:49.521637Z","iopub.status.idle":"2022-06-27T03:07:49.530506Z","shell.execute_reply.started":"2022-06-27T03:07:49.521606Z","shell.execute_reply":"2022-06-27T03:07:49.529264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_image = 10044\n\nBASE_PATH = \"../input/hubmap-organ-segmentation/\"\nimg_1 = tiff.imread(BASE_PATH + \"train_images/\" + str(id_image) + \".tiff\")\nprint(img_1.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:12:09.727793Z","iopub.execute_input":"2022-06-27T03:12:09.729021Z","iopub.status.idle":"2022-06-27T03:12:10.155864Z","shell.execute_reply.started":"2022-06-27T03:12:09.728961Z","shell.execute_reply":"2022-06-27T03:12:10.154654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_1 = rleToMask(df[df[\"id\"]==10044][\"rle\"].iloc[-1], img_1.shape[1], img_1.shape[0])\nmask_1.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:12:16.179030Z","iopub.execute_input":"2022-06-27T03:12:16.179456Z","iopub.status.idle":"2022-06-27T03:12:16.209080Z","shell.execute_reply.started":"2022-06-27T03:12:16.179418Z","shell.execute_reply":"2022-06-27T03:12:16.207816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nplt.imshow(img_1)\nplt.imshow(mask_1, cmap='coolwarm', alpha=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:12:17.058205Z","iopub.execute_input":"2022-06-27T03:12:17.058670Z","iopub.status.idle":"2022-06-27T03:12:19.542438Z","shell.execute_reply.started":"2022-06-27T03:12:17.058636Z","shell.execute_reply":"2022-06-27T03:12:19.540742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Identify all the coordinates in this image\nlbl_0 = label(mask_1) \nprops = regionprops(lbl_0)\nlen(props)  ","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:32:31.901248Z","iopub.execute_input":"2022-06-27T03:32:31.901833Z","iopub.status.idle":"2022-06-27T03:32:32.266516Z","shell.execute_reply.started":"2022-06-27T03:32:31.901791Z","shell.execute_reply":"2022-06-27T03:32:32.265012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bboxes = [] ## Convert all the 9 items into bounding boxes so we can save these images for training\nfor prop in props:\n    bboxes.append([prop.bbox[0] - 30, prop.bbox[1] - 30, \n                   prop.bbox[2] + 30, prop.bbox[3] + 30]) ## Adding a little bit of extra image run","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:33:26.703942Z","iopub.execute_input":"2022-06-27T03:33:26.704492Z","iopub.status.idle":"2022-06-27T03:33:26.711222Z","shell.execute_reply.started":"2022-06-27T03:33:26.704444Z","shell.execute_reply":"2022-06-27T03:33:26.710113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nplt.imshow(img_1[bboxes[1][0]:bboxes[1][2], bboxes[1][1]:bboxes[1][3], :])\nplt.imshow(mask_1[bboxes[1][0]:bboxes[1][2], bboxes[1][1]:bboxes[1][3]], alpha=0.5, cmap='plasma')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:36:34.710701Z","iopub.execute_input":"2022-06-27T03:36:34.712673Z","iopub.status.idle":"2022-06-27T03:36:35.275639Z","shell.execute_reply.started":"2022-06-27T03:36:34.712598Z","shell.execute_reply":"2022-06-27T03:36:35.274385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bboxes","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:39:14.509413Z","iopub.execute_input":"2022-06-27T03:39:14.510820Z","iopub.status.idle":"2022-06-27T03:39:14.520333Z","shell.execute_reply.started":"2022-06-27T03:39:14.510748Z","shell.execute_reply":"2022-06-27T03:39:14.518889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(2, 4, figsize=(20, 8))\nval = 0\nfor i in range(2):\n    for j in range(4):\n        axes[i, j].imshow(img_1[bboxes[val][0]:bboxes[val][2], bboxes[val][1]:bboxes[val][3], :]); val += 1\n        axes[i, j].axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:40:26.460573Z","iopub.execute_input":"2022-06-27T03:40:26.461988Z","iopub.status.idle":"2022-06-27T03:40:27.547622Z","shell.execute_reply.started":"2022-06-27T03:40:26.461929Z","shell.execute_reply":"2022-06-27T03:40:27.546345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(2, 4, figsize=(20, 8))\nval = 0\nfor i in range(2):\n    for j in range(4):\n        axes[i, j].imshow(img_1[bboxes[val][0]:bboxes[val][2], bboxes[val][1]:bboxes[val][3], :])\n        axes[i, j].imshow(mask_1[bboxes[val][0]:bboxes[val][2], bboxes[val][1]:bboxes[val][3]], alpha=0.5, cmap='plasma')\n        axes[i, j].axis('off'); val += 1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T03:42:08.775008Z","iopub.execute_input":"2022-06-27T03:42:08.775474Z","iopub.status.idle":"2022-06-27T03:42:09.971454Z","shell.execute_reply.started":"2022-06-27T03:42:08.775433Z","shell.execute_reply":"2022-06-27T03:42:09.970209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}