{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import numpy.fft as fp\n",
    "import matplotlib.pyplot as plt\n",
    "from PIL import Image\n",
    "import cv2\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 167428 characters]",
      "text/plain": [
       "<Figure size 700x400 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 183704 characters]",
      "text/plain": [
       "<Figure size 700x400 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "im = Image.open('vegetables.jpg')\n",
    "img = np.array(im)\n",
    "img1 = img[250:280, 50:100, :]\n",
    "\n",
    "plt.figure(figsize=(7, 4))\n",
    "plt.subplot(121); plt.imshow(img); plt.title('Original image')\n",
    "plt.subplot(122); plt.imshow(img1); plt.title('Reference piece')\n",
    "plt.show()\n",
    "\n",
    "img1_R = np.mean(img1[:,:,0])\n",
    "img1_G = np.mean(img1[:,:,1])\n",
    "img1_B = np.mean(img1[:,:,2])\n",
    "\n",
    "d0 = np.mean(np.sqrt(((img1[:, :, 0] - img1_R) ** 2) +\n",
    "                     ((img1[:, :, 1] - img1_G) ** 2) +\n",
    "                     ((img1[:, :, 2] - img1_B) ** 2)))\n",
    "\n",
    "d = np.sqrt(((img[:, :, 0] - img1_R) ** 2) +\n",
    "            ((img[:, :, 1] - img1_G) ** 2) +\n",
    "            ((img[:, :, 2] - img1_B) ** 2))\n",
    "\n",
    "d[d < d0] = 0\n",
    "d[d > d0] = 255\n",
    "\n",
    "plt.figure(figsize=(7, 4))\n",
    "plt.subplot(121); plt.imshow(img); plt.title('Original image')\n",
    "plt.subplot(122); plt.imshow(d, cmap='gray'); plt.title('Binarized image')\n",
    "plt.show()"
   ]
  }
 ],
 "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.10.5"
  },
  "vscode": {
   "interpreter": {
    "hash": "369f2c481f4da34e4445cda3fffd2e751bd1c4d706f27375911949ba6bb62e1c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
