{"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 IPython.display import clear_output\nimport time\nimport ipywidgets as widgets\nfrom IPython.display import display\nfrom ipywidgets import interactive\nimport xml.etree.ElementTree as ET","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-06T03:02:19.844956Z","iopub.execute_input":"2023-05-06T03:02:19.845416Z","iopub.status.idle":"2023-05-06T03:02:19.851436Z","shell.execute_reply.started":"2023-05-06T03:02:19.845385Z","shell.execute_reply":"2023-05-06T03:02:19.850496Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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\ndef 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\ndef 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\n\ndef 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\n\ndef 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()\n    \ndef 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\n\ndef convertlayers(layers):\n    nplayers = []\n    for layer in layers:\n        nplayers.append(np.array(layer))\n    return nplayers\n\ndef 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-06T03:02:19.853140Z","iopub.execute_input":"2023-05-06T03:02:19.853693Z","iopub.status.idle":"2023-05-06T03:02:19.872285Z","shell.execute_reply.started":"2023-05-06T03:02:19.853664Z","shell.execute_reply":"2023-05-06T03:02:19.871042Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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    \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-06T03:02:19.873663Z","iopub.execute_input":"2023-05-06T03:02:19.874014Z","iopub.status.idle":"2023-05-06T03:02:26.119995Z","shell.execute_reply.started":"2023-05-06T03:02:19.873986Z","shell.execute_reply":"2023-05-06T03:02:26.118696Z"},"_kg_hide-input":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-06T03:02:26.121480Z","iopub.execute_input":"2023-05-06T03:02:26.121870Z","iopub.status.idle":"2023-05-06T03:02:26.127520Z","shell.execute_reply.started":"2023-05-06T03:02:26.121835Z","shell.execute_reply":"2023-05-06T03:02:26.126490Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"layers,mask = load_layers(folder_paths[0])\nlayers = convertlayers(layers)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T03:02:26.130232Z","iopub.execute_input":"2023-05-06T03:02:26.130856Z","iopub.status.idle":"2023-05-06T03:04:14.790921Z","shell.execute_reply.started":"2023-05-06T03:02:26.130825Z","shell.execute_reply":"2023-05-06T03:04:14.789692Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Choose which bounding box when indexing in function 'cut'","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-06T03:04:14.792449Z","iopub.execute_input":"2023-05-06T03:04:14.792779Z","iopub.status.idle":"2023-05-06T03:04:16.472401Z","shell.execute_reply.started":"2023-05-06T03:04:14.792752Z","shell.execute_reply":"2023-05-06T03:04:16.471460Z"},"_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-06T03:04:16.473726Z","iopub.execute_input":"2023-05-06T03:04:16.474192Z","iopub.status.idle":"2023-05-06T03:04:16.864989Z","shell.execute_reply.started":"2023-05-06T03:04:16.474150Z","shell.execute_reply":"2023-05-06T03:04:16.864061Z"},"_kg_hide-input":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-06T03:04:16.866366Z","iopub.execute_input":"2023-05-06T03:04:16.866689Z","iopub.status.idle":"2023-05-06T03:04:17.481067Z","shell.execute_reply.started":"2023-05-06T03:04:16.866663Z","shell.execute_reply":"2023-05-06T03:04:17.480002Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]}]}