{"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":"In this competition, provided images are extremely large and most of the area is empty space.\n[Tiling approach](https://www.kaggle.com/code/analokamus/a-fast-tile-generation) can alleviate this problem.\nIn this notebook, I try to \"compress\" images to reduce empty space via seam carving.\nSeam carving is an algorithm for content-aware image resizing, where we can reduce (or increase) the size of an image by repeatedly carving out \"seams\" in one direction.\nHere, a seam is defined as an optimal 8-connected path of pixels on a single image from top to bottom, or left to right, where optimality is defined by an image energy function.\nI really like this algorithm because of effectiveness despite its simplicity.\n\n[1] S. Avidan and A. Shamir, \"Seam Carving for Content-Aware Image Resizing,\" in ACM ToG, 2007.","metadata":{}},{"cell_type":"code","source":"# My fork version enables us to terminate removing seams if the energy of the seam becomes greater than the threshold value\n# Orignal implementation: https://github.com/li-plus/seam-carving\n! pip install git+https://github.com/yu4u/seam-carving.git","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-11T16:59:44.971025Z","iopub.execute_input":"2022-08-11T16:59:44.975117Z","iopub.status.idle":"2022-08-11T17:00:01.993455Z","shell.execute_reply.started":"2022-08-11T16:59:44.974981Z","shell.execute_reply":"2022-08-11T17:00:01.991333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\nfrom PIL import Image\nImage.MAX_IMAGE_PIXELS = None\nimport matplotlib.pyplot as plt\nimport seam_carving\nseam_carving.carve.MAX_MEAN_ENERGY = 10.0","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:21:37.697289Z","iopub.execute_input":"2022-08-11T16:21:37.697686Z","iopub.status.idle":"2022-08-11T16:21:37.816906Z","shell.execute_reply.started":"2022-08-11T16:21:37.69765Z","shell.execute_reply":"2022-08-11T16:21:37.815225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img_path in sorted(Path(\"../input/mayo-clinic-strip-ai/train\").glob(\"*.tif\"))[:10]:\n    img = Image.open(img_path)\n    img.thumbnail((2048, 2048))\n    dst = seam_carving.resize(img, (100, 100))\n    fig = plt.figure(figsize=(12, 12))\n    ax = fig.add_subplot(1, 2, 1)\n    ax.set_title(f\"{img_path.stem} original\")\n    ax.imshow(img)\n    ax = fig.add_subplot(1, 2, 2)\n    ax.set_title(f\"{img_path.stem} resized\")\n    ax.imshow(dst)\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T16:26:15.802367Z","iopub.execute_input":"2022-08-11T16:26:15.804001Z","iopub.status.idle":"2022-08-11T16:28:31.702646Z","shell.execute_reply.started":"2022-08-11T16:26:15.80395Z","shell.execute_reply":"2022-08-11T16:28:31.701642Z"},"trusted":true},"execution_count":null,"outputs":[]}]}