{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Image Processing SS 18 - Assignment - 01\n",
    "\n",
    "### Deadline is 25.4.2018 at 8:00\n",
    "\n",
    "Please solve the assignments together with a partner.\n",
    "I will run every notebook. Make sure the code runs through, when clicked on `Kernel` -> `Restart & Run All`.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction to Python / Numpy\n",
    "\n",
    "* [Learn Python in 15 minutes](https://learnxinyminutes.com/docs/python3/): We will use Python 3.\n",
    "* [Numpy for Matlab Users](https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html#general-purpose-equivalents)\n",
    "* [Numpy Quickstart](https://docs.scipy.org/doc/numpy-dev/user/quickstart.html)\n",
    "\n",
    "## Libraries\n",
    "\n",
    "We will use the following libraries:\n",
    "\n",
    "* matplotlib\n",
    "* numpy\n",
    "* scipy\n",
    "* skimage\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise 0 - Setup Development Enviroment - [1 Point]\n",
    "\n",
    "Find a partner, follow the steps in the [README](https://github.com/BildverarbeitungSS18/Hausaufgaben/blob/master/README.md) and paste a link to your repository, names and matriculation numbers into the KVV assignment box.\n",
    "You do not need to upload any files to the KVV. I will clone your repository. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# display the plots inside the notebook\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pylab\n",
    "pylab.rcParams['figure.figsize'] = (12, 12)   # This makes the plot bigger"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The [skimage](http://scikit-image.org/) library comes with multiple useful test images.  Let's start with an image of an astronaut. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from skimage.data import astronaut"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(512, 512, 3)\nuint8\n"
     ]
    }
   ],
   "source": [
    "img = astronaut()   # Get the image\n",
    "print(img.shape)    # the dimension of the image\n",
    "print(img.dtype)    # the image type"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have a `(512, 512, 3)` array of unsigned bytes. At `img[x, y]` there are three values for R,G and B."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will always work with floating point arrays between 0 and 1. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = img / 255."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lets display the image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 748300 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0xffca3b0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(img)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is [Eileen Collins](https://en.wikipedia.org/wiki/Eileen_Collins). She was the first astronaut \n",
    " to fly the Space Shuttle through a complete 360-degree pitch maneuver. What an inspiring woman."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 1 - Plot -       [1 Point]\n",
    "\n",
    "Plot the R, G and B channels separately."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 350796 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x6a53fd0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 351476 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0xfed9c70>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 372400 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x5aafcd0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "r_img = [[[r, 0, 0] for r, _, _ in x] for x in img]\n",
    "plt.imshow(r_img)\n",
    "plt.show()\n",
    "\n",
    "g_img = [[[0, g, 0] for _, g, _ in x] for x in img]\n",
    "plt.imshow(g_img)\n",
    "plt.show()\n",
    "\n",
    "b_img = [[[0, 0, b] for _, _, b in x] for x in img]\n",
    "plt.imshow(b_img)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 2 - RGB to HSV [6 Points]\n",
    "\n",
    "Implement the `rgb_to_hsv` and `hsv_to_rgb` functions. Don't use any color conversion functions from a library.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rgb_to_hsv(x):\n",
    "    \"\"\"\n",
    "    Converts the numpy array `x` from RGB to the HSV. \n",
    "    \"\"\"\n",
    "    def sub_func(r, g, b):\n",
    "        max_val = max(r, g, b)\n",
    "        min_val = min(r, g, b)\n",
    "        v = max_val\n",
    "        \n",
    "        s = 0\n",
    "        diff = max_val - min_val\n",
    "        if max_val != 0:\n",
    "            s = diff / max_val\n",
    "        \n",
    "        h = 0\n",
    "        if max_val == min_val:\n",
    "            h = 0\n",
    "        elif max_val == r:\n",
    "            val = 0\n",
    "            if g < b:\n",
    "                val = 6\n",
    "            h = (g - b) / diff + val\n",
    "        elif max_val == g:\n",
    "            h = (b - r) / diff + 2\n",
    "        else:\n",
    "            h = (r - g) / diff + 4\n",
    "        h = h / 6\n",
    "        \n",
    "        return [h, s, v]\n",
    "    \n",
    "    x = np.array([[sub_func(r, g, b) for r, g, b in y] for y in x])\n",
    "    return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hsv_to_rgb(x):\n",
    "    \"\"\"\n",
    "    Converts the numpy array `x` from HSV to the RGB. \n",
    "    \"\"\"\n",
    "    def sub_func(h, s, v):\n",
    "        r = g = b = 0\n",
    "        i = np.floor(h * 6)\n",
    "        f = h * 6 - i\n",
    "        p = v * (1 - s)\n",
    "        q = v * (1 - f * s)\n",
    "        t = v * (1 - (1 - f) * s)\n",
    "        \n",
    "        if i%6 == 0:\n",
    "            r = v\n",
    "            g = t\n",
    "            b = p\n",
    "        elif i%6 == 1:\n",
    "            r = q\n",
    "            g = v\n",
    "            b = p\n",
    "        elif i%6 == 2:\n",
    "            r = p\n",
    "            g = v\n",
    "            b = t\n",
    "        elif i%6 == 3:\n",
    "            r = p\n",
    "            g = q\n",
    "            b = v\n",
    "        elif i%6 == 4:\n",
    "            r = t\n",
    "            g = p\n",
    "            b = v\n",
    "        elif i%6 == 5:\n",
    "            r = v\n",
    "            g = p\n",
    "            b = q\n",
    "        \n",
    "        return [r, g, b]\n",
    "    \n",
    "    x = np.array([[sub_func(h, s, v) for h, s, v in y] for y in x])\n",
    "    return x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Plot the saturation of the astronaut image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 483268 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x712fab0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "img_as_hsv = rgb_to_hsv(img)\n",
    "# your code\n",
    "np.all(np.isclose(img, hsv_to_rgb(img_as_hsv)))\n",
    "\n",
    "s_img = [[[s, s, s] for _, s, _ in x] for x in img_as_hsv]\n",
    "plt.imshow(s_img)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Increase the saturation by a factor of 2, convert it back to RGB and plot the result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 732648 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0xfed9bd0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# your code\n",
    "n_img = hsv_to_rgb([[[h, min(s*2, 1), v] for h, s, v in x] for x in img_as_hsv])\n",
    "plt.imshow(n_img)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 3 - Callculation [2 Points]\n",
    "\n",
    "In the figure below you can see the [CIE-XYZ](https://de.wikipedia.org/wiki/CIE-Normvalenzsystem) color space.\n",
    "![](https://upload.wikimedia.org/wikipedia/commons/4/49/CIE-Normfarbtafel.png)\n",
    "\n",
    "What are the approximate x,y,z values for the following Adobe RGB colors:\n",
    "* `(0, 0.5, 0.5)`\n",
    "* `(0.33, 0.33, 0.33)`\n",
    "\n",
    "A sodium-vapor lamp shines with double the intensity of a mercury-vapor lamp\n",
    ". The light from the sodium lamp only contains \n",
    "the spectral line at `589,00nm` and the light from the mercury lamp only the\n",
    "spectral line at `435,83 nm`.\n",
    "\n",
    "What color does a human experience? What are the approximate x,y,z values? \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Transformation\n",
    "\n",
    "* RGB = `(0, 0.5, 0.5)`\n",
    "\t* XYZ = `(0.1140, 0.1678, 0.1751)`\n",
    "* RGB = `(0.33, 0.33, 0.33)`\n",
    "\t* XYZ = `(0.0876, 0.0908, 0.0750)`\n",
    "\n",
    "\n",
    "## Lamp\n",
    "\n",
    "589nm - rgb(255, 226, 0)\n",
    "\n",
    "435nm - rgb(35, 0, 255)\n",
    "\n",
    "rgb(255, 226, 0) + rgb(35, 0, 255) = rgb(255, 226, 255)\n",
    "\n",
    "rgb(255, 226, 255) = xyz(0.872007, 0.828326, 0.801956)\n",
    "\n",
    "For a person it looks like a pale pink."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
