{"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":"I went ahead and created bounding boxes for each inklabel mask to perform local comparisons. The bounding boxes were saved in a .xml file and are extracted and visualized. They are accessed later to cutout the specific label to show differences per layer between ink vs no ink labels.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.patches as patches\nimport matplotlib.pyplot as plt\nfrom PIL import Image, ImageDraw\nimport os\nfrom PIL import Image\nfrom IPython.display import clear_output\nimport time\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport ipywidgets as widgets\nfrom IPython.display import display\nfrom ipywidgets import interactive\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-05-06T01:48:04.217930Z","iopub.execute_input":"2023-05-06T01:48:04.218510Z","iopub.status.idle":"2023-05-06T01:48:04.227587Z","shell.execute_reply.started":"2023-05-06T01:48:04.218452Z","shell.execute_reply":"2023-05-06T01:48:04.225865Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xml.etree.ElementTree as ET\n\ndef extract_bboxes(xml_file):\n    # Load and parse the XML file\n    tree = ET.parse(xml_file)\n    root = tree.getroot()\n\n    bboxes = []\n\n    # Iterate through 'object' elements in the XML\n    for obj in root.findall('object'):\n        # Find 'bndbox' element in each object\n        bndbox = obj.find('bndbox')\n        if bndbox is not None:\n            # Extract xmin, ymin, xmax, ymax values\n            xmin = float(bndbox.find('xmin').text)\n            ymin = float(bndbox.find('ymin').text)\n            xmax = float(bndbox.find('xmax').text)\n            ymax = float(bndbox.find('ymax').text)\n\n            # Save the bounding box coordinates as a tuple\n            bboxes.append((xmin, ymin, xmax, ymax))\n\n    return bboxes\n\n# Replace 'path/to/your/xml/file.xml' with the actual path to your XML file\nxml_file = '/kaggle/input/vesuvius-ink-bounding-boxes/inklabels1.xml'\nbounding_boxes = extract_bboxes(xml_file)\n\n# Print the extracted bounding boxes\n#for bbox in bounding_boxes:\n    #print(f'Bounding box: xmin={bbox[0]}, ymin={bbox[1]}, xmax={bbox[2]}, ymax={bbox[3]}')\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-06T01:48:04.230593Z","iopub.execute_input":"2023-05-06T01:48:04.231050Z","iopub.status.idle":"2023-05-06T01:48:04.245335Z","shell.execute_reply.started":"2023-05-06T01:48:04.231012Z","shell.execute_reply":"2023-05-06T01:48:04.243857Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_bboxes(image_path, bboxes):\n    # Open the image and convert it to RGB mode\n    image = Image.open(image_path).convert('RGB')\n    image_np = np.array(image)\n\n    # Create a matplotlib figure and axes\n    fig, ax = plt.subplots(1)\n    ax.imshow(image_np)\n\n    # Draw each bounding box\n    for bbox in bboxes:\n        xmin, ymin, xmax, ymax = bbox\n        rect = patches.Rectangle((xmin, ymin), xmax-xmin, ymax-ymin,\n                                 linewidth=2, edgecolor='r', facecolor='none')\n        ax.add_patch(rect)\n\n    # Show the image with bounding boxes drawn\n    plt.show()\n    \nimage_path = '/kaggle/input/vesuvius-challenge-ink-detection/train/1/inklabels.png'\ndraw_bboxes(image_path, bounding_boxes)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T01:48:04.247110Z","iopub.execute_input":"2023-05-06T01:48:04.247854Z","iopub.status.idle":"2023-05-06T01:48:10.133819Z","shell.execute_reply.started":"2023-05-06T01:48:04.247807Z","shell.execute_reply":"2023-05-06T01:48:10.132812Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder_paths = [\n    '/kaggle/input/vesuvius-challenge-ink-detection/train/1/surface_volume/',\n    '/kaggle/input/vesuvius-challenge-ink-detection/train/2/surface_volume/',\n    '/kaggle/input/vesuvius-challenge-ink-detection/train/3/surface_volume/',\n    '/kaggle/input/vesuvius-challenge-ink-detection/test/a/surface_volume/',\n    '/kaggle/input/vesuvius-challenge-ink-detection/test/b/surface_volume/']","metadata":{"execution":{"iopub.status.busy":"2023-05-06T01:48:10.135217Z","iopub.execute_input":"2023-05-06T01:48:10.135771Z","iopub.status.idle":"2023-05-06T01:48:10.141505Z","shell.execute_reply.started":"2023-05-06T01:48:10.135737Z","shell.execute_reply":"2023-05-06T01:48:10.140349Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_layers(folder_path):\n    filenames = sorted(os.listdir(folder_path))\n    tiff_files = [filename for filename in filenames if filename.endswith('.tif')]\n    layers = []\n    for tiff_file in tiff_files:\n        file_path = os.path.join(folder_path, tiff_file)\n        layer = Image.open(file_path)\n        layers.append(layer)\n    mask = Image.open(folder_path[:-15]+\"inklabels.png\")\n\n    return layers,mask","metadata":{"execution":{"iopub.status.busy":"2023-05-06T01:48:10.144552Z","iopub.execute_input":"2023-05-06T01:48:10.144962Z","iopub.status.idle":"2023-05-06T01:48:10.158950Z","shell.execute_reply.started":"2023-05-06T01:48:10.144927Z","shell.execute_reply":"2023-05-06T01:48:10.157768Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def return_histograms(layers):\n    # Convert the image to a NumPy array\n    histograms = []\n    for layer in layers:\n        #layer = np.array(layer)\n        histograms.append(layer.ravel())\n        \n    return histograms","metadata":{"execution":{"iopub.status.busy":"2023-05-06T01:48:10.160982Z","iopub.execute_input":"2023-05-06T01:48:10.161476Z","iopub.status.idle":"2023-05-06T01:48:10.173133Z","shell.execute_reply.started":"2023-05-06T01:48:10.161430Z","shell.execute_reply":"2023-05-06T01:48:10.171978Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_histogram(index, ink, scroll):\n    plt.figure(figsize=(10, 6))\n    plt.hist(ink[index], bins=40, alpha=0.5, label=\"Ink\",density=True)\n    plt.hist(scroll[index], bins=40, alpha=0.5, label=\"Scroll\",density=True)\n    plt.title(f'Histograms (Slider Value: {index})')\n    plt.xlabel('Values')\n    plt.ylabel('Frequency')\n    plt.legend()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-06T01:48:10.175073Z","iopub.execute_input":"2023-05-06T01:48:10.175705Z","iopub.status.idle":"2023-05-06T01:48:10.187982Z","shell.execute_reply.started":"2023-05-06T01:48:10.175644Z","shell.execute_reply":"2023-05-06T01:48:10.187061Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def separate_by_label(histograms,mask):\n    mask = mask.ravel()\n    \n    ink_idx = np.where(mask==1)\n    scroll_idx = np.where(mask==0)\n    \n    scroll = []\n    ink = []\n    \n    for i,histogram in enumerate(histograms):\n        ink_ = histogram[ink_idx]\n        scroll_ = histogram[scroll_idx]\n        \n        ink_zero_idx = np.where(ink_>0)\n        scroll_zero_idx = np.where(scroll_>0)\n        \n        scroll.append(scroll_[scroll_zero_idx])\n        ink.append(ink_[ink_zero_idx])\n        \n    return scroll,ink","metadata":{"execution":{"iopub.status.busy":"2023-05-06T01:48:10.189307Z","iopub.execute_input":"2023-05-06T01:48:10.190417Z","iopub.status.idle":"2023-05-06T01:48:10.200668Z","shell.execute_reply.started":"2023-05-06T01:48:10.190377Z","shell.execute_reply":"2023-05-06T01:48:10.199498Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convertlayers(layers):\n    nplayers = []\n    for layer in layers:\n        nplayers.append(np.array(layer))\n    return nplayers","metadata":{"execution":{"iopub.status.busy":"2023-05-06T02:01:01.143108Z","iopub.execute_input":"2023-05-06T02:01:01.143609Z","iopub.status.idle":"2023-05-06T02:01:01.150996Z","shell.execute_reply.started":"2023-05-06T02:01:01.143564Z","shell.execute_reply":"2023-05-06T02:01:01.148907Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cut(layers,mask,bbox):\n    cutouts = []\n    temp = []\n    for box in bbox:\n        temp.append(int(box))\n    \n    bbox = temp\n    for i in range(0,65):\n        cutout = layers[i][bbox[1] : bbox[3], bbox[0] : bbox[2]]\n        cutouts.append(cutout)\n    \n    mask = np.array(mask)\n    mask = mask[bbox[1] : bbox[3], bbox[0] : bbox[2]]\n    return cutouts,mask","metadata":{"execution":{"iopub.status.busy":"2023-05-06T02:08:22.017448Z","iopub.execute_input":"2023-05-06T02:08:22.017926Z","iopub.status.idle":"2023-05-06T02:08:22.026491Z","shell.execute_reply.started":"2023-05-06T02:08:22.017887Z","shell.execute_reply":"2023-05-06T02:08:22.025207Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"layers,mask = load_layers(folder_paths[0])","metadata":{"execution":{"iopub.status.busy":"2023-05-06T01:48:10.216277Z","iopub.execute_input":"2023-05-06T01:48:10.217050Z","iopub.status.idle":"2023-05-06T01:50:08.386644Z","shell.execute_reply.started":"2023-05-06T01:48:10.217011Z","shell.execute_reply":"2023-05-06T01:50:08.385454Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"layers = convertlayers(layers)","metadata":{"_kg_hide-input":true,"jupyter":{"source_hidden":true}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Choose which label you want by indexing bounding boxes in cut (see below)","metadata":{}},{"cell_type":"code","source":"cutouts,mask_cutout = cut(layers,mask,bounding_boxes[12])\nhistograms = return_histograms(cutouts)\nscroll_,ink_ = separate_by_label(histograms,mask_cutout)\n#del layers,mask","metadata":{"execution":{"iopub.status.busy":"2023-05-06T02:11:35.160105Z","iopub.execute_input":"2023-05-06T02:11:35.160566Z","iopub.status.idle":"2023-05-06T02:11:36.872117Z","shell.execute_reply.started":"2023-05-06T02:11:35.160529Z","shell.execute_reply":"2023-05-06T02:11:36.870720Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cutouts[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-05-06T02:11:38.753796Z","iopub.execute_input":"2023-05-06T02:11:38.754282Z","iopub.status.idle":"2023-05-06T02:11:38.762454Z","shell.execute_reply.started":"2023-05-06T02:11:38.754226Z","shell.execute_reply":"2023-05-06T02:11:38.761337Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(mask_cutout, cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-06T02:11:40.419452Z","iopub.execute_input":"2023-05-06T02:11:40.419926Z","iopub.status.idle":"2023-05-06T02:11:40.833899Z","shell.execute_reply.started":"2023-05-06T02:11:40.419889Z","shell.execute_reply":"2023-05-06T02:11:40.832670Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a slider for the image indices\nhistogram_index_slider = widgets.IntSlider(min=0, max=len(histograms) - 1, step=1, value=0, description='Slice Index',density=True)\n\n# Display the interactive plot\ninteractive(plot_histogram, scroll = widgets.fixed(scroll_), ink = widgets.fixed(ink_),index=histogram_index_slider)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T02:11:43.841112Z","iopub.execute_input":"2023-05-06T02:11:43.842601Z","iopub.status.idle":"2023-05-06T02:11:44.374357Z","shell.execute_reply.started":"2023-05-06T02:11:43.842541Z","shell.execute_reply":"2023-05-06T02:11:44.373047Z"},"_kg_hide-input":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]}]}