{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport numpy as np \nimport pandas as pd\nimport cv2\nimport skimage.io\nfrom skimage.transform import resize, rescale\n\nfrom multiprocessing import Pool\n\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plot","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# https://www.kaggle.com/c/prostate-cancer-grade-assessment/discussion/145618\ndef crop_white(image: np.ndarray) -> np.ndarray:\n    assert image.shape[2] == 3\n    assert image.dtype == np.uint8\n    ys, = (image.min((1, 2)) != 255).nonzero()\n    xs, = (image.min(0).min(1) != 255).nonzero()\n    if len(xs) == 0 or len(ys) == 0:\n        return image\n    return image[ys.min():ys.max() + 1, xs.min():xs.max() + 1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def crop_white_with_mask(image: np.ndarray, mask: np.ndarray) -> (np.ndarray, np.ndarray):\n    assert image.shape[2] == 3\n    assert image.dtype == np.uint8\n    ys, = (image.min((1, 2)) != 255).nonzero()\n    xs, = (image.min(0).min(1) != 255).nonzero()\n    if len(xs) == 0 or len(ys) == 0:\n        return image, mask\n    return image[ys.min():ys.max() + 1, xs.min():xs.max() + 1], mask[ys.min():ys.max() + 1, xs.min():xs.max() + 1]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Load dataframe"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_labels = pd.read_csv('/kaggle/input/prostate-cancer-grade-assessment/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_labels.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_dir = '/kaggle/input/prostate-cancer-grade-assessment/train_images/'\nmask_dir = '/kaggle/input/prostate-cancer-grade-assessment/train_label_masks/'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Start here"},{"metadata":{"trusted":true},"cell_type":"code","source":"save_dir = \"/kaggle/train_images/\"\nsave_mask_dir = '/kaggle/train_label_masks/'\nos.makedirs(save_dir, exist_ok=True)\nos.makedirs(save_mask_dir, exist_ok=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#for img_id in train_labels.image_id:\ndef crop_white_save(img_id):\n    load_path = data_dir + img_id + '.tiff'\n    save_path = save_dir + img_id + '.png'\n    load_path_mask = mask_dir + img_id + '_mask.tiff'\n    save_path_mask = save_mask_dir + img_id + '_mask.png'\n    \n    biopsy = skimage.io.MultiImage(load_path)\n    if os.path.exists(load_path_mask):\n        biopsy_mask = skimage.io.MultiImage(load_path_mask)\n        # out = cv2.resize(biopsy[-1], (biopsy[-1].shape[1] // 4, biopsy[-1].shape[0] // 4))\n        out, mask_out = crop_white_with_mask(biopsy[-1],biopsy_mask[-1])\n        cv2.imwrite(save_path, out)\n        cv2.imwrite(save_path_mask, mask_out)\n        return 1\n    else:\n        out = crop_white(biopsy[-1])\n        cv2.imwrite(save_path, out)\n        return 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with Pool(processes=4) as pool:\n    has_mask = list(\n        tqdm(pool.imap(crop_white_save, list(train_labels.image_id)), total = len(train_labels.image_id))\n    )\nprint('%d / %d has mask.'%(sum(has_mask),len(has_mask)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!tar -czf train_images.tar.gz ../train_images/*.png\n!tar -czf train_label_masks.tar.gz ../train_label_masks/*.png","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}