{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipfml import processing\n",
    "from ipfml import utils\n",
    "from ipfml import metrics\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": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_folder = \"../fichiersSVD_light\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SVD reconstruction analysis on Synthesis Images "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Utils functions definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "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": 4,
   "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 = processing.divide_in_blocks(img, (200, 200))\n",
    "        zones_img.append(zones[id_zone])\n",
    "        \n",
    "    return zones_img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def display_svd_reconstruction(interval, zones):\n",
    "    \n",
    "    output_images = []\n",
    "    begin, end = interval\n",
    "    for zone in zones:\n",
    "        lab_img = metrics.get_LAB_L(zone)\n",
    "        lab_img = np.array(lab_img, 'uint8')\n",
    "        \n",
    "        U, s, V = svd(lab_img, full_matrices=True)\n",
    "        \n",
    "        smat = np.zeros((end-begin, end-begin), dtype=complex)\n",
    "        smat[:, :] = np.diag(s[begin:end])\n",
    "        output_img = np.dot(U[:, begin:end],  np.dot(smat, V[begin:end, :]))\n",
    "        \n",
    "        print(output_img)\n",
    "        print(np.allclose(lab_img, output_img))\n",
    "        \n",
    "        output_img = np.array(output_img, 'uint8')\n",
    "        output_images.append(Image.fromarray(output_img))\n",
    "        \n",
    "    return output_images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def display_images(dict_data, rec_images):\n",
    "    \n",
    "    indices = dict_data['indices']\n",
    "    scene = dict_data['name']\n",
    "    \n",
    "    fig=plt.figure(figsize=(15, 8))\n",
    "    columns = len(zones)\n",
    "    rows = 1\n",
    "    for i in range(1, columns*rows +1):\n",
    "        index = i - 1\n",
    "        fig.add_subplot(rows, columns, i)\n",
    "        plt.imshow(rec_images[index], label=scene + '_' + str(indices[index]))\n",
    "        img_path = 'tmp_images/' + dict_data['prefix'] + 'zone'+ str(current_dict['zone']) + '_reconstruct_' + str(indices[index]) + '.png'\n",
    "        Image.fromarray(np.asarray(rec_images[index], 'uint8')).save(img_path)\n",
    "    plt.show()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def diff_between_images(noisy, ref):\n",
    "    noisy = np.asarray(noisy)\n",
    "    ref = np.asarray(ref)\n",
    "    \n",
    "    return ref - noisy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def display_sv_data(dict_data, zones_data, interval, reduced=True):\n",
    "    \n",
    "    scene_name = dict_data['name']\n",
    "    image_indices = dict_data['indices']\n",
    "    zone_indice = dict_data['zone']\n",
    "    \n",
    "    plt.figure(figsize=(25, 20))\n",
    "    \n",
    "    sv_data = []\n",
    "    begin, end = interval\n",
    "    for id_img, zone in enumerate(zones_data):\n",
    "        zone = np.asarray(zone)\n",
    "        print(zone.shape)\n",
    "        U, s, V = metrics.get_SVD(zone)\n",
    "\n",
    "        data = s[begin:end]\n",
    "        \n",
    "        label_plt = 'Zone ' + str(zone_indice)\n",
    "        \n",
    "        if reduced:\n",
    "            label_plt += ' reduced info '\n",
    "        \n",
    "        label_plt += 'of ' + scene_name + '_' + str(image_indices[id_img])\n",
    "        \n",
    "        plt.plot(data, label=label_plt)\n",
    "    \n",
    "    plt.legend(fontsize=18)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scenes information data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# start 00020 - ref 00900 - step 10\n",
    "dict_appart = {'name': 'Appart1opt02', \n",
    "               'prefix': 'appartAopt_', \n",
    "               'indices': [\"00050\",\"00250\",\"00400\",\"00550\"],\n",
    "               'zone': 9}\n",
    "\n",
    "# start 00050 - ref 01200 - step 10\n",
    "dict_cuisine = {'name': 'Cuisine01', \n",
    "               'prefix': 'cuisine01_', \n",
    "               'indices': [\"00050\", \"00400\", \"01200\"],\n",
    "               'zone': 3}\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": "markdown",
   "metadata": {},
   "source": [
    "Here we define parameters for the rest of this study :\n",
    "- the scene used\n",
    "- the reconstructed interval (give reduced information from SVD decomposition) \n",
    "- the displayed interval of SVD values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "current_dict = dict_appart\n",
    "displayed_interval = (50, 200)\n",
    "reconstructed_interval = (90, 200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../fichiersSVD_light/Appart1opt02/appartAopt_00050.png\n",
      "../fichiersSVD_light/Appart1opt02/appartAopt_00250.png\n",
      "../fichiersSVD_light/Appart1opt02/appartAopt_00400.png\n",
      "../fichiersSVD_light/Appart1opt02/appartAopt_00550.png\n"
     ]
    }
   ],
   "source": [
    "images_path = compute_images_path(current_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 25356 characters]\n",
      "text/plain": [
       "<PIL.Image.Image image mode=L size=200x200 at 0x7F887EC88D30>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zones = get_images_zones(current_dict, images_path)\n",
    "\n",
    "# save each zone\n",
    "for id, zone in enumerate(zones):\n",
    "    img_name = current_dict['prefix'] + 'zone'+ str(current_dict['zone']) + '_' + current_dict['indices'][id] + '.png'\n",
    "    zone.save('tmp_images/' + img_name)\n",
    "\n",
    "Image.fromarray(np.array(metrics.get_LAB_L(zones[0]), 'uint8'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.28866806+0.j -0.29094643+0.j -0.02445111+0.j ... -1.71491631+0.j\n",
      "   1.31940459+0.j  0.20760834+0.j]\n",
      " [ 0.16505233+0.j -0.88660669+0.j  0.01207778+0.j ...  1.94463722+0.j\n",
      "  -0.3744958 +0.j  0.12481831+0.j]\n",
      " [ 0.31383035+0.j  0.19614328+0.j -0.80474439+0.j ... -0.20867496+0.j\n",
      "   0.89348205+0.j -1.22883763+0.j]\n",
      " ...\n",
      " [-0.29156436+0.j  0.4335953 +0.j  0.85923396+0.j ... -1.17487605+0.j\n",
      "   0.34562434+0.j -0.08012247+0.j]\n",
      " [-0.97248126+0.j  0.58088408+0.j  0.80787694+0.j ...  0.16508712+0.j\n",
      "   0.53716664+0.j -0.70613476+0.j]\n",
      " [-0.48270753+0.j  0.66080143+0.j -0.27728865+0.j ...  4.37054627+0.j\n",
      "  -2.99107566+0.j -1.36264181+0.j]]\n",
      "False\n",
      "[[-0.14994963+0.j  0.43581967+0.j -0.09190862+0.j ... -0.47969561+0.j\n",
      "  -0.31652857+0.j  0.30262608+0.j]\n",
      " [ 0.01319542+0.j  0.01037736+0.j  0.29467907+0.j ...  0.03544648+0.j\n",
      "   0.34455839+0.j -0.19115319+0.j]\n",
      " [ 0.43129111+0.j -0.05703119+0.j -0.22410688+0.j ...  0.17455232+0.j\n",
      "  -0.29794485+0.j -0.80585839+0.j]\n",
      " ...\n",
      " [ 0.0750385 +0.j -0.339218  +0.j -0.17416698+0.j ... -0.51390049+0.j\n",
      "   0.0564363 +0.j -0.60959079+0.j]\n",
      " [ 0.77811602+0.j -0.31998583+0.j  0.46730785+0.j ...  0.06615055+0.j\n",
      "   0.15089096+0.j -0.22678515+0.j]\n",
      " [-0.82048234+0.j  0.76636464+0.j  0.71594635+0.j ...  0.09440693+0.j\n",
      "  -0.23353799+0.j  0.01779662+0.j]]\n",
      "False\n",
      "[[-0.39305315+0.j  0.49224518+0.j  0.29601356+0.j ...  0.01127512+0.j\n",
      "  -0.10423584+0.j  0.06975986+0.j]\n",
      " [ 0.41531705+0.j  0.26697852+0.j -0.279724  +0.j ... -0.03714471+0.j\n",
      "   0.38702772+0.j  0.4653913 +0.j]\n",
      " [ 0.51768945+0.j -0.32177793+0.j -0.14353861+0.j ... -0.24212765+0.j\n",
      "  -0.769503  +0.j  0.10558355+0.j]\n",
      " ...\n",
      " [-0.09209805+0.j -0.23732152+0.j  0.01504225+0.j ... -0.15299212+0.j\n",
      "  -0.18385593+0.j -0.42473101+0.j]\n",
      " [ 0.1928277 +0.j  0.03598631+0.j  0.28088947+0.j ...  0.39723347+0.j\n",
      "   0.1740769 +0.j -0.5499541 +0.j]\n",
      " [-0.28353879+0.j  0.25881022+0.j  0.10260432+0.j ... -0.02750239+0.j\n",
      "   0.15752769+0.j  0.00618296+0.j]]\n",
      "False\n",
      "[[-0.34402807+0.j  0.48052861+0.j -0.04360792+0.j ... -0.41827487+0.j\n",
      "  -0.08100242+0.j -0.07418079+0.j]\n",
      " [ 0.05860211+0.j  0.37737418+0.j  0.06256756+0.j ...  0.07455817+0.j\n",
      "   0.67607892+0.j -0.02710275+0.j]\n",
      " [ 0.60949103+0.j -0.26758225+0.j -0.45425518+0.j ... -0.04550779+0.j\n",
      "  -0.29770192+0.j -0.23770528+0.j]\n",
      " ...\n",
      " [ 0.05990853+0.j -0.31177371+0.j  0.19126769+0.j ... -1.00075706+0.j\n",
      "   0.0778097 +0.j  0.1440076 +0.j]\n",
      " [ 0.02525568+0.j  0.3879124 +0.j -0.61830617+0.j ...  0.29764022+0.j\n",
      "   0.14079526+0.j  0.1223828 +0.j]\n",
      " [-0.02198725+0.j  0.24167839+0.j  0.04588613+0.j ...  0.16560765+0.j\n",
      "  -0.18489246+0.j -0.51338235+0.j]]\n",
      "False\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/jbuisine/.pyenv/versions/thesis-venv/lib/python3.6/site-packages/ipykernel_launcher.py:18: ComplexWarning: Casting complex values to real discards the imaginary part\n"
     ]
    }
   ],
   "source": [
    "reconstructed_images = display_svd_reconstruction(reconstructed_interval, zones)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Overview information for each images (with samples : \\[\"00200\",\"00250\",\"00300\",\"00350\"\\]) reconstructed with reduced information. Images are displayed following this samples list from left to right."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 43436 characters]\n",
      "text/plain": [
       "<Figure size 1080x576 with 4 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_images(current_dict, reconstructed_images)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display of SV values obtained to compare"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### With reduction of reconstructed images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(200, 200)\n",
      "(200, 200)\n",
      "(200, 200)\n",
      "(200, 200)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 139932 characters]\n",
      "text/plain": [
       "<Figure size 1800x1440 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "display_sv_data(current_dict, reconstructed_images, displayed_interval)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Without reduction of information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(200, 200)\n",
      "(200, 200)\n",
      "(200, 200)\n",
      "(200, 200)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 152440 characters]\n",
      "text/plain": [
       "<Figure size 1800x1440 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "zones_lab = [Image.fromarray(np.array(metrics.get_LAB_L(z), 'uint8')) for z in zones]\n",
    "display_sv_data(current_dict, zones_lab, displayed_interval, reduced=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Noise mask analysis\n",
    "Next part to explore (extraction of noise filter).."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 310,
   "metadata": {},
   "outputs": [],
   "source": [
    "# start 00020 - ref 00900 - step 10\n",
    "dict_appart = {'name': 'Appart1opt02', \n",
    "               'prefix': 'appartAopt_', \n",
    "               'indices': [\"00020\", \"00900\"],\n",
    "               'zone': 3}\n",
    "\n",
    "# start 00050 - ref 01200 - step 10\n",
    "dict_cuisine = {'name': 'Cuisine01', \n",
    "               'prefix': 'cuisine01_', \n",
    "               'indices': [\"00050\", \"00400\", \"01200\"],\n",
    "               'zone': 3}\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": "code",
   "execution_count": 311,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask_current_dict = dict_appart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 312,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../fichiersSVD_light/Appart1opt02/appartAopt_00020.png\n",
      "../fichiersSVD_light/Appart1opt02/appartAopt_00900.png\n"
     ]
    }
   ],
   "source": [
    "mask_images_path = compute_images_path(mask_current_dict)\n",
    "mask_zones = get_images_zones(mask_current_dict, mask_images_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 313,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 112364 characters]\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=200x200 at 0x7F24A7553278>"
      ]
     },
     "execution_count": 313,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image.fromarray(diff_between_images(mask_zones[0], mask_zones[1]))"
   ]
  }
 ],
 "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
}
