{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":7166074,"sourceType":"datasetVersion","datasetId":4139657},{"sourceId":7177767,"sourceType":"datasetVersion","datasetId":4148215},{"sourceId":7180949,"sourceType":"datasetVersion","datasetId":4150530}],"dockerImageVersionId":30615,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\ndata=pd.read_csv('/kaggle/input/ubc-cancer/ubc-cancer-data/modified_train_data.csv')\ndata","metadata":{"execution":{"iopub.status.busy":"2023-12-12T07:11:24.323933Z","iopub.execute_input":"2023-12-12T07:11:24.324324Z","iopub.status.idle":"2023-12-12T07:11:24.344331Z","shell.execute_reply.started":"2023-12-12T07:11:24.324294Z","shell.execute_reply":"2023-12-12T07:11:24.343416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\ndef visualize_overlay(image_path, mask_path, save_path):\n    # Read image and mask\n    image = cv2.imread(image_path)\n    mask = cv2.imread(mask_path)\n\n    # Convert BGR to RGB\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    \n    # Get image dimensions\n    height, width, _ = image.shape\n\n    # Resize mask to match image dimensions\n    mask = cv2.resize(mask, (width, height))\n\n    # Create a mask with red, green, and blue channels\n    red_mask = np.zeros_like(mask)\n    red_mask[:, :, 0] = mask[:, :, 2]  # Red channel\n\n    green_mask = np.zeros_like(mask)\n    green_mask[:, :, 1] = mask[:, :, 1]  # Green channel\n\n    blue_mask = np.zeros_like(mask)\n    blue_mask[:, :, 2] = mask[:, :, 0]  # Blue channel\n\n    # Combine masks\n    combined_mask = red_mask + green_mask + blue_mask\n\n    # Create the figure without displaying it with adjusted aspect ratio\n    fig, ax = plt.subplots(figsize=(6, 6))\n\n    # Overlay the mask on the image\n    ax.imshow(image)\n    ax.imshow(combined_mask, alpha=0.3)\n\n    # Create the directory if it does not exist\n    os.makedirs(os.path.dirname(save_path), exist_ok=True)\n\n    # Save the overlay image with the 'image_id' as the filename\n    plt.savefig(save_path)\n    plt.close()\n\n# Iterate through rows in the DataFrame and save overlay images\nfor index, row in data.iterrows():\n    # Construct a save path for the overlay image\n    save_path = f'kaggle/working/overlay_images/{row[\"image_id\"]}.png'\n    visualize_overlay(row['image_path'], row['mask_path'], save_path)\n\n# Select five rows from the DataFrame for visualization\nsample_data = data.sample(5, random_state=42)\n\n# Visualize only five overlay images with adjusted aspect ratio\nfor index, row in sample_data.iterrows():\n    # Construct a path for the overlay image\n    path = f'kaggle/working/overlay_images/{row[\"image_id\"]}.png'\n\n    # Read and display the image with adjusted aspect ratio\n    img = plt.imread(path)\n    plt.figure(figsize=(8, 8))\n    plt.imshow(img)\n    plt.title(f'Visualization for {row[\"image_id\"]}')\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-12T07:11:24.345843Z","iopub.execute_input":"2023-12-12T07:11:24.346136Z","iopub.status.idle":"2023-12-12T07:12:22.217974Z","shell.execute_reply.started":"2023-12-12T07:11:24.346111Z","shell.execute_reply":"2023-12-12T07:12:22.217065Z"},"trusted":true},"execution_count":null,"outputs":[]}]}