{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":6654895,"sourceType":"datasetVersion","datasetId":3840540}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import cv2 \nimport random\nimport os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-11-16T19:51:07.924187Z","iopub.execute_input":"2023-11-16T19:51:07.924631Z","iopub.status.idle":"2023-11-16T19:51:08.595202Z","shell.execute_reply.started":"2023-11-16T19:51:07.924595Z","shell.execute_reply":"2023-11-16T19:51:08.59393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_WSI = '/kaggle/input/UBC-OCEAN/test_images/41.png'\nTEST_PATCHES = '/kaggle/working/'\nTRAIN_PATCHES = '/kaggle/input/ucbo-tiles-256-1/256_10143/'\nTEST_CSV = '/kaggle/input/UBC-OCEAN/test.csv'","metadata":{"execution":{"iopub.status.busy":"2023-11-16T19:51:08.597463Z","iopub.execute_input":"2023-11-16T19:51:08.597972Z","iopub.status.idle":"2023-11-16T19:51:08.603198Z","shell.execute_reply.started":"2023-11-16T19:51:08.597936Z","shell.execute_reply":"2023-11-16T19:51:08.601938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Function for creating Patches from WSI","metadata":{"execution":{"iopub.status.busy":"2023-11-16T19:50:16.49734Z","iopub.execute_input":"2023-11-16T19:50:16.497912Z","iopub.status.idle":"2023-11-16T19:50:16.512844Z","shell.execute_reply.started":"2023-11-16T19:50:16.497881Z","shell.execute_reply":"2023-11-16T19:50:16.511608Z"}}},{"cell_type":"code","source":"def wsi_to_patches(TEST_WSI, TEST_PATCHES, patch_size, testr_csv):\n    # Read the test CSV file to get the image IDs\n    df = pd.read_csv(testr_csv)\n    image_ids = df['image_id']\n\n    # Create output folder if it doesn't exist\n    if not os.path.exists(TEST_PATCHES):\n        os.makedirs(TEST_PATCHES)\n\n    # Load WSI image\n    wsi_image = cv2.imread(TEST_WSI)\n    height, width, _ = wsi_image.shape  # Get the dimensions (height, width)\n\n    # Define patch size and overlap\n    overlap = 0.1\n    patch_width = patch_height = patch_size\n\n    # Generate patches\n    for image_id in image_ids:\n        if not os.path.exists(os.path.join(TEST_PATCHES, str(image_id))):\n            os.makedirs(os.path.join(TEST_PATCHES, str(image_id)))\n        for y in range(0, height - patch_height, int(patch_height * (1 - overlap))):\n            for x in range(0, width - patch_width, int(patch_width * (1 - overlap))):\n                patch = wsi_image[y:y+patch_height, x:x+patch_width]\n                \n                # Calculate the percentage of non-black and non-white pixels\n                total_pixels = patch.size\n                \n                non_black_white_pixels = np.sum((patch > 50) & (patch < 240))\n                non_bw_percentage = (non_black_white_pixels / total_pixels) * 100\n                \n                if non_bw_percentage >= 90:\n                    cv2.imwrite(os.path.join(TEST_PATCHES, str(image_id), f\"{image_id}_{x}_{y}.png\"), patch)","metadata":{"execution":{"iopub.status.busy":"2023-11-16T19:51:08.604771Z","iopub.execute_input":"2023-11-16T19:51:08.60519Z","iopub.status.idle":"2023-11-16T19:51:08.616003Z","shell.execute_reply.started":"2023-11-16T19:51:08.605157Z","shell.execute_reply":"2023-11-16T19:51:08.615024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wsi_to_patches(TEST_WSI, TEST_PATCHES, 256, TEST_CSV)","metadata":{"execution":{"iopub.status.busy":"2023-11-16T19:51:08.617133Z","iopub.execute_input":"2023-11-16T19:51:08.618068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATCHES_FOLDER = '/kaggle/working/41'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def count_patches(patch_folder):\n    patch_files = os.listdir(patch_folder)\n    num_patches = len(patch_files)\n    return num_patches\n\n# Example usage:\nnum_patches = count_patches(PATCHES_FOLDER)\nprint(f'Number of patches generated: {num_patches}')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_patches(patch_folder):\n    patch_files = os.listdir(patch_folder)\n    np.random.shuffle(patch_files)\n    # Display up to 100 patches\n    num_patches_to_display = min(len(patch_files), 100)\n\n    fig, axes = plt.subplots(10, 10, figsize=(15, 15))\n\n    for i in range(num_patches_to_display):\n        patch_path = os.path.join(patch_folder, patch_files[i])\n        patch = plt.imread(patch_path)\n        ax = axes[i // 10, i % 10]\n        ax.imshow(patch)\n        ax.axis('off')\n\n    # Remove empty subplots if there are fewer than 100 patches\n    for i in range(num_patches_to_display, 100):\n        fig.delaxes(axes.flatten()[i])\n\n    plt.tight_layout()\n    plt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_patches(PATCHES_FOLDER)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread('/kaggle/working/41/41_22540_10810.png')\nimg2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nplt.imshow(img2)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}