{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "34bdcb56",
   "metadata": {},
   "source": [
    "## Reading image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6a443a5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5c06eab5",
   "metadata": {},
   "outputs": [],
   "source": [
    "img = plt.imread(\"smile.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3adade41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fa40ebde1c0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 117240 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2c82cabc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(386, 446, 3)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img.shape\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1da6e499",
   "metadata": {},
   "outputs": [],
   "source": [
    "gray_img = np.mean(img, axis = 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5203839b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fa40c318dc0>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 77240 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(gray_img, cmap = 'gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35b574ec",
   "metadata": {},
   "source": [
    "## Defining convolution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f2dc1309",
   "metadata": {},
   "outputs": [],
   "source": [
    "def convolution(img, kernel):\n",
    "    h, w = img.shape\n",
    "    kh, kw = kernel.shape\n",
    "    assert kh == kw and kh % 2 == 1 # make sure that the number of rows and columns is odd.\n",
    "    bw = (kh - 1) // 2\n",
    "    padded_img = np.zeros((h+2*bw, w+2*bw))\n",
    "    padded_img[bw:bw+h, bw:bw+w] = img\n",
    "    output = np.zeros_like(img)\n",
    "    for i in range(h):\n",
    "        for j in range(w):\n",
    "            output[i, j] = np.sum(kernel * padded_img[i:i+kh, j:j+kw])\n",
    "    return output"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d4f796b",
   "metadata": {},
   "source": [
    "## Experiment 1.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0a510177",
   "metadata": {},
   "outputs": [],
   "source": [
    "kernel1 = np.ones((7, 7)) * (1/49)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ddc80b23",
   "metadata": {},
   "outputs": [],
   "source": [
    "output1 = convolution(gray_img, kernel1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4dd24163",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fa40c14e5e0>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 56956 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(output1, cmap = 'gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc2d4c61",
   "metadata": {},
   "source": [
    "## Experiment 1.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "41f16903",
   "metadata": {},
   "outputs": [],
   "source": [
    "kernel2 = np.array(((-1, 0, 1), (-2, 0, 2), (-1, 0, 1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1fe8fd69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1,  0,  1],\n",
       "       [-2,  0,  2],\n",
       "       [-1,  0,  1]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kernel2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c790b7f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "kernel3 = - kernel2.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2c0652d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  1],\n",
       "       [ 0,  0,  0],\n",
       "       [-1, -2, -1]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kernel3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "988ff02f",
   "metadata": {},
   "outputs": [],
   "source": [
    "output1 = convolution(gray_img, kernel2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "743edf54",
   "metadata": {},
   "outputs": [],
   "source": [
    "output2 = convolution(gray_img, kernel3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a8eea008",
   "metadata": {},
   "outputs": [],
   "source": [
    "output = np.sqrt(output1**2 + output2**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "cf2c65da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fa40c0c0bb0>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 75092 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(output, cmap = 'gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae1f0862",
   "metadata": {},
   "source": [
    "## Application of FFT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "646b4cb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "freq = np.fft.fft2(gray_img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "08f18562",
   "metadata": {},
   "outputs": [],
   "source": [
    "freq = np.fft.fftshift(freq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "48c9ddc5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fa40c032e20>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 76096 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(np.log(np.abs(freq)+1), cmap = 'gray')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ac43a244",
   "metadata": {},
   "outputs": [],
   "source": [
    "mid  = [i//2 for i in freq.shape]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "af173720",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[193, 223]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "32b9646a",
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = np.ones_like(freq)\n",
    "r = 20\n",
    "mask[mid[0]-r:mid[0]+r, mid[1]-r:mid[1]+r] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f3861142",
   "metadata": {},
   "outputs": [],
   "source": [
    "freq *= mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "76ab7c0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fa406ff4970>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 79188 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(np.log(np.abs(freq)+1), cmap = 'gray')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "702f29ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "freq = np.fft.ifftshift(freq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "db5303c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "output3 = np.fft.ifft2(freq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1d31bf96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fa406f62e50>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 83476 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "make sure that the number of rows and columns is odd.plt.imshow(np.abs(output3), cmap = 'gray')"
   ]
  }
 ],
 "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.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
