{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "cf1480f1",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "0615494c",
            "cell_type": "markdown",
            "source": "Import the necessary modules and load the example image used for\nfiltering.\n",
            "metadata": {}
        },
        {
            "id": "02ef9494",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import datasets\nfrom scipy.ndimage import zoom, generic_filter\nimport matplotlib.pyplot as plt\nascent = zoom(datasets.ascent(), 0.5)",
            "outputs": []
        },
        {
            "id": "1de2d81a",
            "cell_type": "markdown",
            "source": "Compute a maximum filter with kernel size 5 by passing a simple NumPy\naggregation function as argument to `function`.\n",
            "metadata": {}
        },
        {
            "id": "81af2042",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "maximum_filter_result = generic_filter(ascent, np.amax, [5, 5])",
            "outputs": []
        },
        {
            "id": "57e80dab",
            "cell_type": "markdown",
            "source": "While a maximum filter could also directly be obtained using\n`maximum_filter`, `generic_filter` allows generic Python function or\n`scipy.LowLevelCallable` to be used as a filter. Here, we compute the\nrange between maximum and minimum value as an example for a kernel size\nof 5.\n",
            "metadata": {}
        },
        {
            "id": "1cca3089",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "def custom_filter(image):\n    return np.amax(image) - np.amin(image)\ncustom_filter_result = generic_filter(ascent, custom_filter, [5, 5])",
            "outputs": []
        },
        {
            "id": "df04bb15",
            "cell_type": "markdown",
            "source": "Plot the original and filtered images.\n",
            "metadata": {}
        },
        {
            "id": "84efc7da",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fig, axes = plt.subplots(3, 1, figsize=(3, 9))\nplt.gray()  # show the filtered result in grayscale\ntop, middle, bottom = axes\nfor ax in axes:\n    ax.set_axis_off()  # remove coordinate system\ntop.imshow(ascent)\ntop.set_title(\"Original image\")\nmiddle.imshow(maximum_filter_result)\nmiddle.set_title(\"Maximum filter, Kernel: 5x5\")\nbottom.imshow(custom_filter_result)\nbottom.set_title(\"Custom filter, Kernel: 5x5\")\nfig.tight_layout()",
            "outputs": []
        }
    ]
}