{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "59ab2d2d",
            "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": "dba580e0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy import ndimage\nimport numpy as np\na = np.zeros((5,5), dtype=int)\na[1:-1, 1:-1] = 1; a[2,2] = 0\na",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0, 0],\n       [0, 1, 1, 1, 0],\n       [0, 1, 0, 1, 0],\n       [0, 1, 1, 1, 0],\n       [0, 0, 0, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "a8a44e55",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "# Closing removes small holes\nndimage.binary_closing(a).astype(int)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0, 0],\n       [0, 1, 1, 1, 0],\n       [0, 1, 1, 1, 0],\n       [0, 1, 1, 1, 0],\n       [0, 0, 0, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "174e3a85",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "# Closing is the erosion of the dilation of the input\nndimage.binary_dilation(a).astype(int)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 1, 1, 1, 0],\n       [1, 1, 1, 1, 1],\n       [1, 1, 1, 1, 1],\n       [1, 1, 1, 1, 1],\n       [0, 1, 1, 1, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "045c700b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ndimage.binary_erosion(ndimage.binary_dilation(a)).astype(int)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0, 0],\n       [0, 1, 1, 1, 0],\n       [0, 1, 1, 1, 0],\n       [0, 1, 1, 1, 0],\n       [0, 0, 0, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "4f485287",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a = np.zeros((7,7), dtype=int)\na[1:6, 2:5] = 1; a[1:3,3] = 0\na",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 1, 0, 1, 0, 0],\n       [0, 0, 1, 0, 1, 0, 0],\n       [0, 0, 1, 1, 1, 0, 0],\n       [0, 0, 1, 1, 1, 0, 0],\n       [0, 0, 1, 1, 1, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f1f5abc4",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "# In addition to removing holes, closing can also\n# coarsen boundaries with fine hollows.\nndimage.binary_closing(a).astype(int)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 1, 0, 1, 0, 0],\n       [0, 0, 1, 1, 1, 0, 0],\n       [0, 0, 1, 1, 1, 0, 0],\n       [0, 0, 1, 1, 1, 0, 0],\n       [0, 0, 1, 1, 1, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f513c9bb",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ndimage.binary_closing(a, structure=np.ones((2,2))).astype(int)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 1, 1, 1, 0, 0],\n       [0, 0, 1, 1, 1, 0, 0],\n       [0, 0, 1, 1, 1, 0, 0],\n       [0, 0, 1, 1, 1, 0, 0],\n       [0, 0, 1, 1, 1, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}