{"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":"The code below selects tiles for each image and mask based on the maximum number of tissue pixels.\n\n![](https://i.ibb.co/RzSWP56/convert.png)","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport skimage.io\nfrom tqdm.notebook import tqdm\nimport zipfile\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-05T19:34:09.481836Z","iopub.execute_input":"2023-05-05T19:34:09.482522Z","iopub.status.idle":"2023-05-05T19:34:10.123498Z","shell.execute_reply.started":"2023-05-05T19:34:09.482468Z","shell.execute_reply":"2023-05-05T19:34:10.122201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN = '../input/prostate-cancer-grade-assessment/train_images/'\nMASKS = '../input/prostate-cancer-grade-assessment/train_label_masks/'\nOUT_TRAIN = 'tiles.zip'\nOUT_MASKS = 'masks.zip'\nsz = 2**8\nN = 6**2","metadata":{"execution":{"iopub.status.busy":"2023-05-05T19:34:10.125354Z","iopub.execute_input":"2023-05-05T19:34:10.12563Z","iopub.status.idle":"2023-05-05T19:34:10.131975Z","shell.execute_reply.started":"2023-05-05T19:34:10.125602Z","shell.execute_reply":"2023-05-05T19:34:10.130764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_tiles(img):\n    result = []\n    shape = img.shape\n    # Compute the amount of padding necessary to be able to divide the image in non-overlapping tiles of size sz\n    pad0,pad1 = (sz - shape[0]%sz)%sz, (sz - shape[1]%sz)%sz\n    # Pad image such that we could fit non-overlapping tiles of size tile_size over the image\n    # Note that the padding value is 255 opposed to 0 padding, as the background is white (255)\n    img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],\n                constant_values=255)\n    img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3) \n    img = img.transpose(0,2,1,3,4).reshape(-1,sz,sz,3) # Reshape to: tiles x size x size x 3 (RGB)\n    if len(img) < N: # If there are fewer tiles available than requested\n        # Then pad in the tiles dimension to add empty tiles!\n        img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)\n    idxs = np.argsort(img.reshape(img.shape[0],-1).sum(-1))[:N] # Get indices that would sort the tiles based on amount of foreground pixels & their darkness\n    img = img[idxs]\n    for i in range(len(img)):\n        result.append({'img':img[i], 'idx':i})\n    return result","metadata":{"execution":{"iopub.status.busy":"2023-05-05T19:34:10.133727Z","iopub.execute_input":"2023-05-05T19:34:10.134146Z","iopub.status.idle":"2023-05-05T19:34:10.146129Z","shell.execute_reply.started":"2023-05-05T19:34:10.134109Z","shell.execute_reply":"2023-05-05T19:34:10.145123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names = [name[:-5] for name in os.listdir(TRAIN)]\nwith zipfile.ZipFile(OUT_TRAIN, 'w') as img_out:\n    for name in tqdm(names):\n        img = skimage.io.MultiImage(os.path.join(TRAIN,name+'.tiff'))[1]\n        tiles = get_tiles(img)\n        for t in tiles:\n            img,idx = t['img'],t['idx']\n#             img = cv2.imencode('.png',cv2.cvtColor(img, cv2.COLOR_RGB2BGR))[1]\n            img = cv2.imencode('.png',img)[1]\n            img_out.writestr(f'{name}/tile_{idx}.png', img)","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2023-05-05T19:34:10.147413Z","iopub.execute_input":"2023-05-05T19:34:10.148132Z","iopub.status.idle":"2023-05-05T19:57:48.915245Z","shell.execute_reply.started":"2023-05-05T19:34:10.148091Z","shell.execute_reply":"2023-05-05T19:57:48.913959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom IPython.display import FileLink \nos.chdir(r'/kaggle/working')\nFileLink(r'tiles.zip')","metadata":{"execution":{"iopub.status.busy":"2023-05-05T19:57:48.917807Z","iopub.execute_input":"2023-05-05T19:57:48.918112Z","iopub.status.idle":"2023-05-05T19:57:48.926473Z","shell.execute_reply.started":"2023-05-05T19:57:48.91808Z","shell.execute_reply":"2023-05-05T19:57:48.925428Z"},"trusted":true},"execution_count":null,"outputs":[]}]}