{ "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", "import os\n", "import math\n", "\n", "from scipy.signal import medfilt2d, wiener, cwt\n", "import pywt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "data_folder = \"../fichiersSVD_light\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Wavelet filter analysis on zones of 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", " 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", " zones_indices = dict_data['zones']\n", " zones_img = []\n", " \n", " for path in images_path:\n", " img = Image.open(path)\n", " zones = processing.divide_in_blocks(img, (200, 200))\n", " \n", " zones_list = []\n", " \n", " for id_zone in zones_indices:\n", " zones_list.append(zones[id_zone])\n", " \n", " zones_img.append(zones_list)\n", " \n", " return zones_img" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def display_sv_data(dict_data, zones_data, interval, _norm=False):\n", " \n", " scene_name = dict_data['name']\n", " image_indices = dict_data['indices']\n", " zones_indices = dict_data['zones']\n", " colors = ['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9']\n", " \n", " plt.figure(figsize=(25, 20))\n", " \n", " sv_data = []\n", " begin, end = interval\n", " for id_img, zones in enumerate(zones_data):\n", " \n", " for id_zone, zone in enumerate(zones):\n", " U, s, V = processing.get_LAB_L_SVD(zone)\n", " \n", " data = s[begin:end]\n", " \n", " if _norm:\n", " data = utils.normalize_arr(data)\n", " \n", " plt.plot(data, \n", " color=colors[id_zone], \n", " label='Zone ' + str(zones_indices[id_zone]) + ' of ' + scene_name + '_' + str(image_indices[id_img]))\n", " \n", " plt.legend(fontsize=18)\n", " plt.show()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Useful function\n", "\n", "def get_highest_values(arr, n):\n", " return np.array(arr).argsort()[-n:][::-1]\n", "\n", "def get_lowest_values(arr, n):\n", " return np.array(arr).argsort()[::-1][-n:][::-1]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "def get_sv_std(arr):\n", " arr = np.array(arr)\n", " images = [arr]\n", " \n", " # Apply list of filter on arr\n", " images.append(medfilt2d(arr, [3, 3]))\n", " images.append(medfilt2d(arr, [5, 5]))\n", " images.append(wiener(arr, [3, 3]))\n", " images.append(wiener(arr, [5, 5]))\n", " \n", " coeffs2 = pywt.dwt2(arr, 'haar')\n", " LL, (LH, HL, HH) = coeffs2\n", " \n", " print(LL.shape)\n", " print(LH.shape)\n", " \n", " images.append(LL)\n", " \n", " sv_vector = []\n", " # for each new image apply SVD and get SV \n", " for img in images:\n", " s = metrics.get_SVD_s(img)\n", " sv_vector.append(s)\n", " \n", " sv_array = np.array(sv_vector)\n", " print(sv_array.shape)\n", " \n", " _, len = sv_array.shape\n", " \n", " sv_std = []\n", " \n", " # normalize each SV vectors and compute standard deviation for each sub vectors\n", " for i in range(len):\n", " sv_array[:, i] = utils.normalize_arr(sv_array[:, i])\n", " sv_std.append(np.std(sv_array[:, i]))\n", " \n", " \n", " return np.array(sv_std)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Scenes zones 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': [\"00020\", \"00200\", \"00900\"],\n", " 'zones': [3, 6]}\n", "\n", "# start 00050 - ref 01200 - step 10\n", "dict_cuisine = {'name': 'Cuisine01', \n", " 'prefix': 'cuisine01_', \n", " 'indices': [\"00050\", \"00400\", \"01200\"],\n", " 'zones': [3, 6]}\n", "\n", "# start 00020 - ref 00950 - step 10\n", "dict_sdb_c = {'name': 'SdbCentre', \n", " 'prefix': 'SdB2_', \n", " 'indices': [\"00020\", \"00400\", \"00950\"],\n", " 'zones': [3, 6]}\n", "\n", "# start 00020 - ref 00950 - step 10\n", "dict_sdb_d = {'name': 'SdbDroite', \n", " 'prefix': 'SdB2_D_', \n", " 'indices': [\"00020\", \"00400\", \"00950\"],\n", " 'zones': [2, 3, 10, 13]}" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "current_dict = dict_appart\n", "interval = (30, 200)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "../fichiersSVD_light/Appart1opt02/appartAopt_00020.png\n", "../fichiersSVD_light/Appart1opt02/appartAopt_00200.png\n", "../fichiersSVD_light/Appart1opt02/appartAopt_00900.png\n" ] } ], "source": [ "images_path = compute_images_path(current_dict)\n", "zones_data = get_images_zones(current_dict, images_path)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(100, 100)\n", "(100, 100)\n", "(6,)\n" ] }, { "ename": "ValueError", "evalue": "not enough values to unpack (expected 2, got 1)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mlab_image\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmetrics\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_LAB_L\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mzones_data\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0msv_std_vector\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_sv_std\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlab_image\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mget_sv_std\u001b[0;34m(arr)\u001b[0m\n\u001b[1;32m 26\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msv_array\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 28\u001b[0;31m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlen\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msv_array\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 29\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0msv_std\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: not enough values to unpack (expected 2, got 1)" ] } ], "source": [ "lab_image = metrics.get_LAB_L(zones_data[0][0])\n", "\n", "sv_std_vector = get_sv_std(lab_image)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4.56454151e-12, 5.98443803e-01, 2.92380551e-01, 3.11862186e+00,\n", " 1.83881976e+00, 1.32616590e+00, 2.31162957e+00, 3.26004542e+00,\n", " 3.69558983e+00, 4.40479644e+00, 5.86361202e+00, 4.57210378e+00,\n", " 4.01233699e+00, 9.91121754e+00, 1.49335463e+01, 1.02598281e+01,\n", " 9.33149634e+00, 9.02596321e+00, 7.37994999e+00, 6.44933954e+00,\n", " 1.41221182e+01, 6.92716771e+00, 6.66692086e+00, 1.96695779e+02,\n", " 1.35313092e+01, 4.82088913e+00, 1.30004193e+01, 1.26877063e+01,\n", " 1.19550988e+01, 1.54599579e+01, 1.12188124e+01, 8.25570286e+00,\n", " 8.47955315e+00, 1.20754110e+01, 7.76856416e+00, 1.55686219e+01,\n", " 1.13219704e+01, 4.78254950e+01, 1.61024565e+01, 1.05388706e+01,\n", " 5.00185479e+01, 1.50232416e+01, 4.66821321e+01, 5.16188334e+01,\n", " 4.94272850e+00, 5.06946699e+01, 4.60741401e+01, 4.95388007e+01,\n", " 1.62546781e+01, 1.70462990e+01, 1.66268801e+01, 4.64068807e+01,\n", " 4.87707118e+01, 5.18047108e+01, 2.42465168e+01, 1.75765638e+01,\n", " 4.80224924e+01, 4.53850435e+01, 2.49940296e+01, 2.32956634e+01,\n", " 2.44845973e+01, 1.98151482e+01, 2.28386331e+01, 1.95685413e+01,\n", " 2.33535639e+01, 5.43324597e+01, 4.83919154e+01, 4.44866045e+01,\n", " 2.46294407e+01, 2.53035119e+01, 1.90294314e+01, 4.86445357e+01,\n", " 4.51782154e+01, 5.40772949e+01, 1.81583448e+01, 4.41443592e+01,\n", " 3.69757725e+01, 5.23320448e+01, 4.21467616e+01, 5.33122822e+01,\n", " 4.24121613e+01, 2.70014670e+01, 4.20051035e+01, 2.21602652e+01,\n", " 2.59550074e+01, 2.86226215e+01, 2.06325539e+01, 2.61912490e+01,\n", " 3.34594682e+01, 3.02981684e+01, 3.05140681e+01, 3.15200613e+01,\n", " 3.56035468e+01, 2.00897239e+01, 3.60414295e+01, 2.95487956e+01,\n", " 1.78073124e+01, 2.08486817e+01, 3.28774249e+01, 3.17002386e+01])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "indices = get_highest_values(sv_std_vector, 100)\n", "\n", "s_lab_image = metrics.get_SVD_s(lab_image)\n", "\n", "s_lab_image[indices]" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 3, 0, 26, 28, 29, 23, 30, 27, 31, 22, 21, 20,\n", " 24, 25, 11, 14, 18, 6, 15, 34, 17, 32, 37, 35, 2,\n", " 12, 33, 19, 36, 38, 39, 41, 40, 7, 47, 44, 42, 43,\n", " 45, 49, 46, 13, 48, 9, 50, 16, 10, 8, 53, 54, 55,\n", " 51, 56, 52, 61, 57, 58, 59, 60, 63, 62, 66, 65, 64,\n", " 67, 69, 197, 70, 104, 103, 105, 68, 72, 73, 71, 106, 4,\n", " 107, 108, 117, 109, 74, 111, 78, 145, 114, 123, 115, 102, 97,\n", " 152, 144, 101, 130, 131, 129, 127, 120, 116])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_lowest_values(sv_std_vector, 100)" ] } ], "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 }