{"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":"gpu","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":6984590,"sourceType":"datasetVersion","datasetId":4014175},{"sourceId":7280378,"sourceType":"datasetVersion","datasetId":4221234},{"sourceId":7289631,"sourceType":"datasetVersion","datasetId":4227609},{"sourceId":7289632,"sourceType":"datasetVersion","datasetId":4227610}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport torch\nfrom torch.utils.data import Dataset, random_split, DataLoader\nfrom torchvision import transforms\nimport torch.nn.functional as F\nimport torch.nn as nn\nimport torch.optim as optim\n\nfrom PIL import Image, ImageChops, ImageDraw, ImageFilter\nImage.MAX_IMAGE_PIXELS = None\n\n%matplotlib inline\nimport matplotlib.pyplot as plt\n\nimport os\n\nimport skimage\nimport skimage.measure","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-30T22:44:03.220805Z","iopub.execute_input":"2023-12-30T22:44:03.221088Z","iopub.status.idle":"2023-12-30T22:44:07.849987Z","shell.execute_reply.started":"2023-12-30T22:44:03.221063Z","shell.execute_reply":"2023-12-30T22:44:07.849005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nRed: Tumor\nGreen: Stroma (healthy tissue)\nBlue: Necrosis (dead or dying non-cancerous tissue)\n\"\"\"\n\n\n# Display image\ndef delete_empty_spaces(img):\n    threshold = 150\n    \n    img = np.array(img).astype('uint8')\n\n    # Find columns where all cells are below the threshold\n    columns_to_remove = np.all(img < threshold, axis=0)\n    rows_to_remove = np.all(img < threshold, axis=1)\n\n    # Remove the columns\n    img = img[:, ~columns_to_remove]\n\n    # Remove the rows\n    img = img[~rows_to_remove, :]\n\n    return Image.fromarray(img)\n\n\ndef smooth_mask(mask_array, sigma=2, repeat=1):\n    # Apply Gaussian blur to the binary mask\n    mask_array = Image.fromarray(mask_array.astype('uint8'))\n    for i in range(repeat):\n        mask_array = mask_array.filter(ImageFilter.GaussianBlur(sigma))\n    return np.array(mask_array)\n\n\ndef extract_and_save_surfaces(image_path, mask_path, output_folder, resize):\n    # Create the output folder if it doesn't exist\n    if not os.path.exists(output_folder):\n        os.makedirs(output_folder)\n\n    # Open the image and mask\n    with Image.open(image_path).convert('L').resize(resize) as img, Image.open(mask_path).resize(resize) as mask:\n        # Convert the image and mask to NumPy arrays\n        img_array = np.array(img)\n        mask_array = np.array(mask)\n        \n        binary_masks = [[255, 0, 0], [0, 255, 0], [0, 0, 255]]\n        output_folders = ['tumor', 'stroma', 'necrosis']\n        for i in range(3):\n            folder_path = os.path.join(output_folder, output_folders[i])\n            # Create the output folder if it doesn't exist\n            if not os.path.exists(folder_path):\n                os.makedirs(folder_path)\n                                  \n            # Create a binary mask based on red areas in the mask\n            binary_mask = np.all(mask_array == binary_masks[i], axis=-1)\n\n            # Smooth the binary mask\n            smoothed_mask = smooth_mask(binary_mask)\n\n            # Label connected components in the smoothed binary mask\n            labeled_mask = skimage.measure.label(smoothed_mask)\n\n            # Extract and save each connected component as a separate PNG\n            for label in range(1, np.max(labeled_mask) + 1):\n                # Create a mask for the current connected component\n                component_mask = (labeled_mask == label)\n\n                # Extract surfaces from the original image using the component mask\n                surfaces = img_array.copy()\n                surfaces[~component_mask] = 0\n\n                # Create a new image from the extracted surfaces\n                surfaces_image = Image.fromarray(surfaces)\n\n                # Delete empty spacessurfaces\n                surfaces_image = delete_empty_spaces(surfaces_image)\n\n                # Save the extracted surfaces image\n                filename_without_extension, file_extension = os.path.splitext(os.path.basename(image_path))\n                # Remove small surfaces\n                size_limit = 100*200\n                if surfaces_image.size[0] * surfaces_image.size[1] < size_limit:\n                    continue\n\n#                 # Save surface\n#                 output_path = os.path.join(folder_path, f\"{filename_without_extension}_{label}.png\")\n#                 surfaces_image.save(output_path)\n\n                # Save flipped surface\n                for angle in [a for a in range(0, 360, 60)]:\n                    output_path = os.path.join(folder_path, f\"{filename_without_extension}_{label}_angle{angle}.png\")\n                    flipped_image = surfaces_image.rotate(angle)\n                    flipped_image.save(output_path)\n                    \n#                     plt.imshow(flipped_image, cmap='gray')\n#                     plt.title(f\"Surface {label} flipped by {angle}\")\n#                     plt.show()\n\n#                 # Display the original image, mask, smoothed mask, and extracted surfaces\n#                 plt.figure(figsize=(15, 5))\n#                 plt.subplot(1, 4, 1)\n#                 plt.imshow(img, cmap='gray')\n#                 plt.title(f\"Original Image {filename_without_extension}\")\n\n#                 plt.subplot(1, 4, 2)\n#                 plt.imshow(smoothed_mask)\n#                 plt.title(\"Mask\")\n\n#                 plt.subplot(1, 4, 3)\n#                 plt.imshow(smoothed_mask, cmap='gray')\n#                 plt.title(\"Smoothed Mask\")\n\n#                 plt.subplot(1, 4, 4)\n#                 plt.imshow(surfaces_image, cmap='gray')\n#                 plt.title(f\"Extracted Surface {label}\")\n\n#                 plt.show()\n\n\ndirectory_path1 = '/kaggle/input/ubc-train-imgs-8192x8192-part1'\ndirectory_path2 = '/kaggle/input/ubc-train-imgs-8192x8192-part2'\noutput_folder = \"/kaggle/working/\"\n\nfile_list1 = os.listdir(directory_path1)\nfile_list2 = os.listdir(directory_path2)\n\n# Iterate over the list of files\nfor file_name in file_list1:\n    # Construct the full path to the file\n    file_path = os.path.join(directory_path1, file_name)\n    mask_path = f\"/kaggle/input/ubc-supplemental-masks-4096x4096/{file_name}\"\n    \n    try:\n        if os.path.exists(file_path) and os.path.exists(mask_path):\n            extract_and_save_surfaces(file_path, mask_path, output_folder, resize=(8192, 8192))\n    except Exception as e:\n        print(f\"Error processing file {file_name}: {e}\")\n\n# Iterate over the list of files\nfor file_name in file_list2:\n    # Construct the full path to the file\n    file_path = os.path.join(directory_path2, file_name)\n    mask_path = f\"/kaggle/input/ubc-supplemental-masks-4096x4096/{file_name}\"\n                                      \n    try:\n        if os.path.exists(file_path) and os.path.exists(mask_path):\n            extract_and_save_surfaces(file_path, mask_path, output_folder, resize=(8192, 8192))   \n    except Exception as e:\n        print(f\"Error processing file {file_name}: {e}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-29T10:59:09.921313Z","iopub.execute_input":"2023-12-29T10:59:09.921968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def show_img(img1, img2):    \n#     # Display the original and resized images\n#     fig, axs = plt.subplots(1, 3, figsize=(12, 8))\n\n#     # Original image\n#     axs[0].imshow(img1, cmap='gray')\n#     axs[0].axis('off')\n#     axs[0].set_title('Original Image')\n\n#     # Resized image\n#     axs[1].imshow(img2)\n#     axs[1].axis('off')\n#     axs[1].set_title('Mask')\n    \n#     # Sum of images\n#     img3 = ImageChops.add(img1, img2)\n#     axs[2].imshow(img3)\n#     axs[2].axis('off')\n#     axs[2].set_title('Sum')\n\n#     plt.show()\n#     return None\n\n# image_id = 1101\n\n# condition = train_df[\"image_id\"] == image_id\n# img_path1  = train_df.loc[condition, \"image_path\"].tolist()[0]\n# print(img_path1)\n# img1 = Image.open(img_path1) #.convert('L')\n# img1 = img1.resize((256, 256))\n\n# img_path2  = train_df.loc[condition, \"mask_path\"].tolist()[0]\n# print(img_path2)\n# img2 = Image.open(img_path2)\n# img2 = img2.resize((256, 256))\n\n# show_img(img1, img2)","metadata":{"execution":{"iopub.status.busy":"2023-12-29T10:57:24.448083Z","iopub.status.idle":"2023-12-29T10:57:24.448478Z","shell.execute_reply.started":"2023-12-29T10:57:24.448303Z","shell.execute_reply":"2023-12-29T10:57:24.448322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# \"\"\"\n# Red: Tumor\n# Green: Stroma (healthy tissue)\n# Blue: Necrosis (dead or dying non-cancerous tissue)\n# \"\"\"\n\n# # Display image\n# def delete_empty_spaces(img):\n#     threshold = 150\n    \n#     img = np.array(img).astype('uint8')\n\n#     # Find columns where all cells are below the threshold\n#     columns_to_remove = np.all(img < threshold, axis=0)\n#     rows_to_remove = np.all(img < threshold, axis=1)\n\n#     # Remove the columns\n#     img = img[:, ~columns_to_remove]\n\n#     # Remove the rows\n#     img = img[~rows_to_remove, :]\n\n#     return Image.fromarray(img)\n\n# def smooth_mask(mask_array, sigma=2, repeat=2):\n#     # Apply Gaussian blur to the binary mask\n#     mask_array = Image.fromarray(mask_array.astype('uint8'))\n#     for i in range(repeat):\n#         mask_array = mask_array.filter(ImageFilter.GaussianBlur(sigma))\n#     return np.array(mask_array)\n\n# def extract_and_save_surfaces(image_path, mask_path, output_folder, resize):\n#     # Create the output folder if it doesn't exist\n#     if not os.path.exists(output_folder):\n#         os.makedirs(output_folder)\n\n#     # Open the image and mask\n#     with Image.open(image_path).convert('L').resize(resize) as img, Image.open(mask_path).resize(resize) as mask:\n#         # Convert the image and mask to NumPy arrays\n#         img_array = np.array(img)\n#         mask_array = np.array(mask)\n\n#         # Create a binary mask based on red areas in the mask\n#         binary_mask = np.all(mask_array == [255, 0, 0], axis=-1)\n\n#         # Smooth the binary mask\n#         smoothed_mask = smooth_mask(binary_mask)\n\n#         # Label connected components in the smoothed binary mask\n#         labeled_mask = skimage.measure.label(smoothed_mask)\n# #         labeled_mask = remove_small_objects(labeled_mask, min_size=10)\n\n#         # Extract and save each connected component as a separate PNG\n#         for label in range(1, np.max(labeled_mask) + 1):\n#             # Create a mask for the current connected component\n#             component_mask = (labeled_mask == label)\n\n#             # Extract surfaces from the original image using the component mask\n#             surfaces = img_array.copy()\n#             surfaces[~component_mask] = 0\n\n#             # Create a new image from the extracted surfaces\n#             surfaces_image = Image.fromarray(surfaces)\n            \n#             # Delete empty spacessurfaces\n#             surfaces_image = delete_empty_spaces(surfaces_image)\n            \n#             # Save the extracted surfaces image\n#             output_path = os.path.join(output_folder, f\"extracted_surface_{label}.png\")\n#             surfaces_image.save(output_path)\n            \n#             # Display the original image, mask, smoothed mask, and extracted surfaces\n#             plt.figure(figsize=(15, 5))\n#             plt.subplot(1, 4, 1)\n#             plt.imshow(img, cmap='gray')\n#             plt.title(\"Original Image\")\n\n#             plt.subplot(1, 4, 2)\n#             plt.imshow(mask)\n#             plt.title(\"Mask\")\n\n#             plt.subplot(1, 4, 3)\n#             plt.imshow(smoothed_mask, cmap='gray')\n#             plt.title(\"Smoothed Mask\")\n\n#             plt.subplot(1, 4, 4)\n#             plt.imshow(surfaces_image, cmap='gray')\n#             plt.title(f\"Extracted Surface {label}\")\n\n#             plt.show()\n\n# # Specify the paths to the original image, mask, and the folder containing small patches\n# image_id = 1020\n# image_path = f\"/kaggle/input/ubc-train-images-4096x4096/{image_id}.png\"\n# mask_path = f\"/kaggle/input/ubc-supplemental-masks-4096x4096/{image_id}.png\"\n\n# # Specify the output folder for patches\n# output_folder = \"/kaggle/working/\"\n\n# extract_and_save_surfaces(image_path, mask_path, output_folder, resize=(8192, 8192))","metadata":{"execution":{"iopub.status.busy":"2023-12-29T10:57:24.449753Z","iopub.status.idle":"2023-12-29T10:57:24.45009Z","shell.execute_reply.started":"2023-12-29T10:57:24.449928Z","shell.execute_reply":"2023-12-29T10:57:24.449944Z"},"trusted":true},"execution_count":null,"outputs":[]}]}