{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f98b56b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import os\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "036ecd56",
   "metadata": {},
   "outputs": [],
   "source": [
    "stream = cv2.VideoCapture(os.path.join('assets', 'tracking.avi'))\n",
    "_, frame = stream.read()\n",
    "stream.release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8f9a01de",
   "metadata": {},
   "outputs": [],
   "source": [
    "frame = np.array(Image.open(os.path.join('..', 'assets', 'background_light_20fps_gerbil.png')))\n",
    "bg = np.array(Image.open(os.path.join('..', 'assets', 'background_light_20fps.png')))\n",
    "#bg = np.array(im_frame).reshape((1024, 1280, 3)).astype(np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5c3dcd99",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x88f7ee0>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 180972 characters]\n",
      "text/plain": [
       "<Figure size 1008x864 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, axes = plt.subplots(1, 2, figsize=(14, 12))\n",
    "axes[0].imshow(frame)\n",
    "axes[1].imshow(bg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "67430800",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x898a340>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 340304 characters]\n",
      "text/plain": [
       "<Figure size 1008x1008 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "substracted = cv2.absdiff(frame, bg )\n",
    "fig, ax = plt.subplots(figsize=(14, 14))\n",
    "ax.imshow(substracted)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "01f5de89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x5a53b50>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 235452 characters]\n",
      "text/plain": [
       "<Figure size 1008x1008 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "grey = cv2.cvtColor(substracted, cv2.COLOR_BGR2GRAY)\n",
    "fig, ax = plt.subplots(figsize=(14, 14))\n",
    "ax.imshow(grey)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "3d8bf69e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x1ec86d5fc70>"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 76532 characters]\n",
      "text/plain": [
       "<Figure size 1008x1008 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "kernel_size = (25,25)\n",
    "blurred = cv2.GaussianBlur(grey, kernel_size, 0)\n",
    "fig, ax = plt.subplots(figsize=(14, 14))\n",
    "ax.imshow(blurred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "db59bdc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x1ec870b21c0>"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 15044 characters]\n",
      "text/plain": [
       "<Figure size 1008x1008 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# https://docs.opencv.org/4.5.2/d7/d4d/tutorial_py_thresholding.html\n",
    "_, thresh = cv2.threshold(blurred, 20, 255, cv2.THRESH_BINARY)\n",
    "fig, ax = plt.subplots(figsize=(14, 14))\n",
    "ax.imshow(thresh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "5f1d494b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x1ec89a6ba60>"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 11116 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# https://docs.opencv.org/4.5.2/d4/d73/tutorial_py_contours_begin.html\n",
    "contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)\n",
    "contour = contours[np.argmax(list(map(cv2.contourArea, contours)))]\n",
    "\n",
    "M = cv2.moments(contour)\n",
    "x = int(M['m10'] / M['m00'])\n",
    "y = int(M['m01'] / M['m00'])\n",
    "x, y\n",
    "\n",
    "plt.scatter(contour[:, 0, 0], contour[:, 0, 1])\n",
    "plt.scatter(x, y, color='red')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d9aa250",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
