{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipfml.processing.segmentation import divide_in_blocks\n",
    "from ipfml.processing import transform\n",
    "from ipfml.filters import convolution as conv\n",
    "from ipfml.filters import kernels\n",
    "from ipfml import utils\n",
    "\n",
    "from PIL import Image\n",
    "from scipy import signal\n",
    "from skimage import color\n",
    "import scipy.stats as stats\n",
    "import seaborn as sns\n",
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from numpy.linalg import svd\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_folder = \"../dataset\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data augmentation analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Utils functions definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compute_images_path(dict_data):\n",
    "    \n",
    "    scene = dict_data['name']\n",
    "    prefix = dict_data['prefix']\n",
    "    indices = dict_data['indices']\n",
    "    \n",
    "    images_path = []\n",
    "    for index in indices:\n",
    "        path = os.path.join(data_folder, os.path.join(scene, prefix + index + \".png\"))\n",
    "        print(path)\n",
    "        images_path.append(path)\n",
    "    return images_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_images_zones(dict_data, images_path):\n",
    "    \n",
    "    id_zone = dict_data['zone']\n",
    "    \n",
    "    zones_img = []\n",
    "    for path in images_path:\n",
    "        img = Image.open(path)\n",
    "        zones = divide_in_blocks(img, (200, 200))\n",
    "        zones_img.append(zones[id_zone])\n",
    "        \n",
    "    return zones_img"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scenes information data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# start 00020 - ref 00900 - step 10\n",
    "dict_appart = {'name': 'Appart1opt02', \n",
    "               'prefix': 'appartAopt_', \n",
    "               'indices': [\"00050\", \"00100\", \"00200\", \"00300\", \"00400\", \"00500\", \"00700\", \"00900\"],\n",
    "               'zone': 9}\n",
    "\n",
    "# start 00050 - ref 01200 - step 10\n",
    "dict_cuisine = {'name': 'Cuisine01', \n",
    "               'prefix': 'cuisine01_', \n",
    "               'indices': [\"00050\", \"00100\", \"00200\", \"00300\", \"00400\", \"00600\", \"00700\",\"00900\", \"01200\"],\n",
    "               'zone': 6}\n",
    "\n",
    "# start 00020 - ref 00950 - step 10\n",
    "dict_sdb_c = {'name': 'SdbCentre', \n",
    "               'prefix': 'SdB2_', \n",
    "               'indices': [\"00020\", \"00400\", \"00950\"],\n",
    "               'zone': 3}\n",
    "\n",
    "# start 00020 - ref 00950 - step 10\n",
    "dict_sdb_d = {'name': 'SdbDroite', \n",
    "               'prefix': 'SdB2_D_', \n",
    "               'indices': [\"00020\", \"00400\", \"00950\"],\n",
    "               'zone': 3}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Definition of parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../dataset/Appart1opt02/appartAopt_00050.png\n",
      "../dataset/Appart1opt02/appartAopt_00100.png\n",
      "../dataset/Appart1opt02/appartAopt_00200.png\n",
      "../dataset/Appart1opt02/appartAopt_00300.png\n",
      "../dataset/Appart1opt02/appartAopt_00400.png\n",
      "../dataset/Appart1opt02/appartAopt_00500.png\n",
      "../dataset/Appart1opt02/appartAopt_00700.png\n",
      "../dataset/Appart1opt02/appartAopt_00900.png\n",
      "../dataset/Cuisine01/cuisine01_00050.png\n",
      "../dataset/Cuisine01/cuisine01_00100.png\n",
      "../dataset/Cuisine01/cuisine01_00200.png\n",
      "../dataset/Cuisine01/cuisine01_00300.png\n",
      "../dataset/Cuisine01/cuisine01_00400.png\n",
      "../dataset/Cuisine01/cuisine01_00600.png\n",
      "../dataset/Cuisine01/cuisine01_00700.png\n",
      "../dataset/Cuisine01/cuisine01_00900.png\n",
      "../dataset/Cuisine01/cuisine01_01200.png\n"
     ]
    }
   ],
   "source": [
    "images_path_appart = compute_images_path(dict_appart)\n",
    "images_path_cuisine = compute_images_path(dict_cuisine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "zones_appart = get_images_zones(dict_appart, images_path_appart)\n",
    "zones_cuisine = get_images_zones(dict_cuisine, images_path_cuisine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from skimage.restoration import denoise_nl_means, estimate_sigma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_mask_noise(noise):\n",
    "    \n",
    "    sigma_est = np.mean(estimate_sigma(noise, multichannel=True))\n",
    "    \n",
    "    print(f\"estimated noise standard deviation = {sigma_est}\")\n",
    "\n",
    "    patch_kw = dict(patch_size=5,      # 5x5 patches\n",
    "                    patch_distance=6,  # 13x13 search area\n",
    "                    multichannel=True)\n",
    "\n",
    "    # slow algorithm\n",
    "    denoise = denoise_nl_means(noise, h=1.15 * sigma_est, fast_mode=False,\n",
    "                               **patch_kw)\n",
    "    \n",
    "    denoise = np.array(denoise, 'uint8')\n",
    "    noise_mask = np.abs(denoise - noise)\n",
    "    \n",
    "    return noise_mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "def display_zones(dict_scene, zones, rows, columns):\n",
    "\n",
    "    fig=plt.figure(figsize=(30, 20))\n",
    "    \n",
    "    indices = dict_scene['indices']\n",
    "    \n",
    "    for i, img in enumerate(zones):\n",
    "        \n",
    "        ax1 = fig.add_subplot(rows, columns, (i+1))\n",
    "        ax1.title.set_text(indices[i])\n",
    "\n",
    "        # Turn off tick labels\n",
    "        ax1.set_yticklabels([])\n",
    "        ax1.set_xticklabels([])\n",
    "                   \n",
    "        plt.imshow(img, vmin=0, vmax=255)\n",
    "    \n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "estimated noise standard deviation = 4.171717928817519\n",
      "estimated noise standard deviation = 2.972292307861928\n",
      "estimated noise standard deviation = 2.0959178907964704\n",
      "estimated noise standard deviation = 1.7670837942187327\n",
      "estimated noise standard deviation = 1.5476241408150022\n",
      "estimated noise standard deviation = 1.3998392878503598\n",
      "estimated noise standard deviation = 1.2145140105371637\n",
      "estimated noise standard deviation = 1.1016169631568047\n"
     ]
    }
   ],
   "source": [
    "zones_mask = []\n",
    "\n",
    "for zone in zones_appart:\n",
    "    zone = np.array(zone)\n",
    "    denoised_zone = get_mask_noise(zone)\n",
    "    zones_mask.append(denoised_zone)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 756388 characters]\n",
      "text/plain": [
       "<Figure size 2160x1440 with 8 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_zones(dict_appart, zones_mask, 2, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "zone_mask_reshape = zones_mask[-1].reshape(3, 200, 200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f55d12f2ef0>"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 28880 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(zone_mask_reshape[0], cmap='gray', vmin=0, vmax=255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f55d11831d0>"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 32168 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(zone_mask_reshape[1], cmap='gray', vmin=0, vmax=255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f55d1272160>"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 30316 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(zone_mask_reshape[2], cmap='gray', vmin=0, vmax=255)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "thesis-venv",
   "language": "python",
   "name": "thesis-venv"
  },
  "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.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
