{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Build image pyramids\n\nThe ``pyramid_gaussian`` function takes an image and yields successive images\nshrunk by a constant scale factor. Image pyramids are often used, e.g., to\nimplement algorithms for denoising, texture discrimination, and scale-invariant\ndetection.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\n\nfrom skimage import data\nfrom skimage.transform import pyramid_gaussian\n\n\nimage = data.astronaut()\nrows, cols, dim = image.shape\npyramid = tuple(pyramid_gaussian(image, downscale=2, channel_axis=-1))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Generate a composite image for visualization\n\nFor visualization, we generate a composite image with the same number of rows\nas the source image but with ``cols + pyramid[1].shape[1]`` columns. We then\nhave space to stack all of the dowsampled images to the right of the\noriginal.\n\nNote: The sum of the number of rows in all dowsampled images in the pyramid\nmay sometimes exceed the original image size in cases when image.shape[0] is\nnot a power of two. We expand the number of rows in the composite slightly as\nnecessary to account for this. Expansion beyond the number of rows in the\noriginal will also be necessary to cover cases where downscale < 2.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# determine the total number of rows and columns for the composite\ncomposite_rows = max(rows, sum(p.shape[0] for p in pyramid[1:]))\ncomposite_cols = cols + pyramid[1].shape[1]\ncomposite_image = np.zeros((composite_rows, composite_cols, 3), dtype=np.double)\n\n# store the original to the left\ncomposite_image[:rows, :cols, :] = pyramid[0]\n\n# stack all downsampled images in a column to the right of the original\ni_row = 0\nfor p in pyramid[1:]:\n    n_rows, n_cols = p.shape[:2]\n    composite_image[i_row : i_row + n_rows, cols : cols + n_cols] = p\n    i_row += n_rows\n\nfig, ax = plt.subplots()\nax.imshow(composite_image)\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}