{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "c87b4180",
            "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": "8fab33c7",
            "cell_type": "markdown",
            "source": "Correlation is the process of moving a filter mask often referred to\nas kernel over the image and computing the sum of products at each location.\n",
            "metadata": {}
        },
        {
            "id": "95c5c4a8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.ndimage import correlate\nimport numpy as np\ninput_img = np.arange(25).reshape(5,5)\nprint(input_img)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "[[ 0  1  2  3  4]\n[ 5  6  7  8  9]\n[10 11 12 13 14]\n[15 16 17 18 19]\n[20 21 22 23 24]]"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "e373d9d9",
            "cell_type": "markdown",
            "source": "Define a kernel (weights) for correlation. In this example, it is for sum of\ncenter and up, down, left and right next elements.\n",
            "metadata": {}
        },
        {
            "id": "c9831b54",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "weights = [[0, 1, 0],\n           [1, 1, 1],\n           [0, 1, 0]]",
            "outputs": []
        },
        {
            "id": "2bde42d8",
            "cell_type": "markdown",
            "source": "We can calculate a correlation result:\nFor example, element ``[2,2]`` is ``7 + 11 + 12 + 13 + 17 = 60``.\n",
            "metadata": {}
        },
        {
            "id": "f90cfc1b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "correlate(input_img, weights)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[  6,  10,  15,  20,  24],\n    [ 26,  30,  35,  40,  44],\n    [ 51,  55,  60,  65,  69],\n    [ 76,  80,  85,  90,  94],\n    [ 96, 100, 105, 110, 114]])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}