{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "b1ef648a",
            "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": "b47009b8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy import ndimage\nimport numpy as np\ninput = np.zeros((8, 8), dtype=int)\ninput[2, 2] = 1\nmask = np.zeros((8, 8), dtype=int)\nmask[1:4, 1:4] = mask[4, 4]  = mask[6:8, 6:8] = 1\ninput",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 1, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "db112e4e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "mask",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 1, 1, 1, 0, 0, 0, 0],\n       [0, 1, 1, 1, 0, 0, 0, 0],\n       [0, 1, 1, 1, 0, 0, 0, 0],\n       [0, 0, 0, 0, 1, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 1, 1],\n       [0, 0, 0, 0, 0, 0, 1, 1]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "b4fe8073",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ndimage.binary_propagation(input, mask=mask).astype(int)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 1, 1, 1, 0, 0, 0, 0],\n       [0, 1, 1, 1, 0, 0, 0, 0],\n       [0, 1, 1, 1, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d80e10a2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ndimage.binary_propagation(input, mask=mask,\\\nstructure=np.ones((3,3))).astype(int)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 1, 1, 1, 0, 0, 0, 0],\n       [0, 1, 1, 1, 0, 0, 0, 0],\n       [0, 1, 1, 1, 0, 0, 0, 0],\n       [0, 0, 0, 0, 1, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "4abbf1ab",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "# Comparison between opening and erosion+propagation\na = np.zeros((6,6), dtype=int)\na[2:5, 2:5] = 1; a[0, 0] = 1; a[5, 5] = 1\na",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[1, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0],\n       [0, 0, 1, 1, 1, 0],\n       [0, 0, 1, 1, 1, 0],\n       [0, 0, 1, 1, 1, 0],\n       [0, 0, 0, 0, 0, 1]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c89a5c91",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ndimage.binary_opening(a).astype(int)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 1, 0, 0],\n       [0, 0, 1, 1, 1, 0],\n       [0, 0, 0, 1, 0, 0],\n       [0, 0, 0, 0, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "39dfb243",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "b = ndimage.binary_erosion(a)\nb.astype(int)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 1, 0, 0],\n       [0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "415b744c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "ndimage.binary_propagation(b, mask=a).astype(int)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 0, 0, 0, 0, 0],\n       [0, 0, 0, 0, 0, 0],\n       [0, 0, 1, 1, 1, 0],\n       [0, 0, 1, 1, 1, 0],\n       [0, 0, 1, 1, 1, 0],\n       [0, 0, 0, 0, 0, 0]])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}