diff --git a/assignments/01_assignment_sample_solution.ipynb b/assignments/01_assignment_sample_solution.ipynb new file mode 100644 index 0000000..f0667af --- /dev/null +++ b/assignments/01_assignment_sample_solution.ipynb @@ -0,0 +1,422 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Image Processing SS 16 - Assignment - 01\n", + "\n", + "### Deadline is 27.4.2016 at 16: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/python/)\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/) 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", + "import copy\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": [], + "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": [], + "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": [], + "source": [ + "img_red = copy.deepcopy(img)\n", + "img_red[:,:,1] = 0\n", + "img_red[:,:,2] = 0\n", + "plt.imshow(img_red)\n", + "plt.show()\n", + "\n", + "img_green = copy.deepcopy(img);\n", + "img_green[:,:,0] = 0\n", + "img_green[:,:,2] = 0\n", + "plt.imshow(img_green)\n", + "plt.show()\n", + "\n", + "img_blue = copy.deepcopy(img);\n", + "img_blue[:,:,0] = 0\n", + "img_blue[:,:,1] = 0\n", + "plt.imshow(img_blue)\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": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def rgb_to_hsv(x):\n", + " \"\"\"\n", + " Converts the numpy array `x` from RGB to the HSV. \n", + " \"\"\"\n", + " hsv=[]\n", + " for line in x:\n", + " newLine=[]\n", + " for pixel in line:\n", + " r= pixel[0]/255\n", + " g= pixel[1]/255\n", + " b= pixel[2]/255\n", + " cMax = np.amax([r,g,b])\n", + " cMin = min([r,g,b])\n", + " delta = cMax-cMin\n", + " v = cMax\n", + " h=0\n", + " s=0\n", + " if v > 0:\n", + " s = delta/cMax\n", + " if s>0 :\n", + " if r == cMax:\n", + " h= ((g-b)/delta)%6\n", + " elif g==cMax:\n", + " h= ((b-r)/delta)+2\n", + " elif b==cMax:\n", + " h=((r-g)/delta)+4\n", + " h=h*60\n", + " if h<0:\n", + " h=h+360\n", + " newLine.append([h,s,v])\n", + " hsv.append(newLine)\n", + " return hsv\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def hsv_to_rgb(x):\n", + " \"\"\"\n", + " Converts the numpy array `x` from HSV to the RGB. \n", + " \"\"\"\n", + " rgb = []\n", + " for line in x:\n", + " newLine=[]\n", + " for pixel in line:\n", + " h= pixel[0]\n", + " s= pixel[1]\n", + " v= pixel[2]\n", + " c = v*s\n", + " x=c*(1-abs(((h/60)%2)-1))\n", + " m= v-c\n", + " tmpPixel= [0,0,0]\n", + " if h >= 0 and h <60:\n", + " tmpPixel=[c,x,0]\n", + " elif h >= 60 and h <120:\n", + " tmpPixel=[x,c,0]\n", + " elif h >= 120 and h <180:\n", + " tmpPixel=[0,c,x]\n", + " elif h >= 180 and h <240:\n", + " tmpPixel=[0,x,c]\n", + " elif h >= 240 and h <300:\n", + " tmpPixel=[x,0,c]\n", + " elif h >= 300 and h <360:\n", + " tmpPixel=[c,0,x]\n", + " newLine.append([(tmpPixel[0]+m)*255,(tmpPixel[1]+m)*255,(tmpPixel[2]+m)*255])\n", + " rgb.append(newLine)\n", + " return rgb" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot the saturation of the astronaut image" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "img_as_hsv = rgb_to_hsv(img)\n", + "\n", + " # your code\\n\",\n", + "\n", + "img_saturation=copy.deepcopy(np.array(img_as_hsv))\n", + "img_saturation[:,:,0]=0\n", + "img_saturation[:,:,2]=0\n", + "plt.imshow(img_saturation[:, :, 1], cmap='gray')\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": 11, + "metadata": {}, + "outputs": [], + "source": [ + "img_as_hsv = rgb_to_hsv(img)\n", + "# your code\\n\",\n", + "img_saturation = []\n", + "for line in img_as_hsv:\n", + " newLine=[]\n", + " for pixel in line:\n", + " newPixel=[]\n", + " newPixel.append(pixel[0]) \n", + " newSaturation = pixel[1]*2\n", + " if newSaturation >1:\n", + " newSaturation=1\n", + "\n", + " newPixel.append(newSaturation)\n", + " newPixel.append(pixel[2])\n", + " newLine.append(newPixel)\n", + " img_saturation.append(newLine)\n", + "\n", + "img_as_rgb= hsv_to_rgb(img_saturation)\n", + "plt.imshow(img_as_rgb)\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": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "T=np.matrix([[2.04159, -0.56501, -0.34473],[-0.96924, 1.87597, 0.04156],[0.01344, -0.11836, 1.01517]])" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "ARGB1 = np.array([0, 0.5, 0.5])\n", + "ARGB2 = np.array([0.33, 0.33, 0.33])\n", + "\n", + "XYZ1 = (T.I).dot(ARGB1)\n", + "print(XYZ1)\n", + "\n", + "XYZ2 = (T.I).dot(ARGB2)\n", + "print(XYZ2)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# For a sodium-vapor lamp and a mercury-vapor lamp a human does experience a bright red.\n", + "# The approximate x,y,z values are 0.5, 0.32, 0.18\n", + "# You can read the values directly from the XYZ-diagram. Connect the two spectral lines with a line.\n", + "# The resulting color is on this line. \n", + "# As the two colors mix and the sodium lamp has double intensity, the color that a human experience is nearer to \n", + "# the sodium lamp.\n", + "# It is exactly 1/3 of the total line length from the sodium spectral line away. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "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.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/assignments/02_assignment_sample_solution.ipynb b/assignments/02_assignment_sample_solution.ipynb new file mode 100644 index 0000000..ba14c5f --- /dev/null +++ b/assignments/02_assignment_sample_solution.ipynb @@ -0,0 +1,410 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Image Processing SS 16 - Assignment - 02\n", + "\n", + "### Deadline is 4.5.2016 at 16:00 o'clock\n", + "\n", + "Please solve the assignments together with a partner.\n", + "I will run every notebook. Make sure the code runs through. Select `Kernel` -> `Restart & Run All` to test it.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Exercise 1 - 10 Points\n", + "\n", + "Implement affine transformation with [bicubic interpolation](https://en.wikipedia.org/wiki/Bicubic_interpolation)." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# display the plots inside the notebook\n", + "%matplotlib inline" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "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": { + "collapsed": false + }, + "outputs": [], + "source": [ + "from skimage.data import astronaut\n", + "from skimage.color import rgb2gray" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# We use a gray image. All the algorithms should work with color images too.\n", + "img = rgb2gray(astronaut() / 255.)\n", + "plt.imshow(img, cmap='gray')\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def derive_y(image):\n", + " \"\"\"Computes the derivative of the image w.r.t the y coordinate\"\"\"\n", + " derived_image = np.zeros_like(image)\n", + " for x in range(image.shape[0]):\n", + " for y in range(image.shape[1]):\n", + " if y + 1 < image.shape[1] and y - 1 > 0:\n", + " derived_image[x,y] = image[x, y - 1] - image[x, y + 1]\n", + " return derived_image\n", + "\n", + "def derive_x(image):\n", + " \"\"\"Computes the derivative of the image w.r.t the x coordinate\"\"\"\n", + " derived_image = np.zeros_like(image)\n", + " for x in range(image.shape[0]):\n", + " for y in range(image.shape[1]):\n", + " if x + 1 < image.shape[1] and x - 1 > 0:\n", + " derived_image[x,y] = image[x - 1, y] - image[x + 1, y]\n", + " return derived_image" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "dx_img = derive_x(img)\n", + "dy_img = derive_y(img)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "plt.figure(figsize=(18, 12))\n", + "plt.subplot(131)\n", + "plt.imshow(img, cmap='gray')\n", + "plt.subplot(132)\n", + "plt.imshow(dx_img, cmap='gray')\n", + "plt.subplot(133)\n", + "plt.imshow(dy_img, cmap='gray')\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# The derivatives are no longer in the range [0,1].\n", + "print(\"min: {}, max: {}\".format(dx_img.min(), dx_img.max()))" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "indicies = np.indices(img.shape).reshape(2, -1)\n", + "indicies.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# pick some random index" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "indicies[:, 123456]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "indicies_hg = np.concatenate([\n", + " indicies, np.ones((1, indicies.shape[1]))], axis=0)\n", + "indicies_hg.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "indicies_hg[:, 123456]" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "T_scale = np.array([\n", + " [0.75, 0, 0],\n", + " [0, 0.75, 0],\n", + " [0, 0, 1],\n", + "])\n", + "# np.dot(T_affine, indicies_hg).shape, for python < 3.5\n", + "(T_scale @ indicies_hg).shape" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "T_affine = np.array([\n", + " [0.75, 0.2, 100],\n", + " [-0.2, 0.75, 100],\n", + " [0, 0.001, 1],\n", + "])" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# you can use this function to invert the matricies\n", + "np.linalg.inv(T_scale)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def affine_transformation(img, matrix):\n", + " indicies = np.indices(img.shape).reshape(2, -1)\n", + " indicies_hg = matrix @ np.concatenate([indicies, np.ones((1, indicies.shape[1]))], axis=0)\n", + " \n", + " res_img = np.zeros((int(np.ceil(np.max(indicies_hg[0,:]))), int(np.ceil(np.max(indicies_hg[1,:])))))\n", + " indicies_res = np.indices(res_img.shape).reshape(2, -1)\n", + " indicies_res_hg = np.concatenate([indicies_res, np.ones((1, indicies_res.shape[1]))], axis=0)\n", + " indicies_interpolation = np.linalg.inv(matrix) @ indicies_res_hg\n", + " \n", + " return bicubic_interpolation(img, indicies_interpolation, matrix, res_img)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def bicubic_interpolation(img, indicies, matrix, result):\n", + " dx_img = derive_x(img)\n", + " dy_img = derive_y(img)\n", + " dxy_img = derive_x(dy_img)\n", + " inv_matrix = np.array([\n", + " [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", + " [ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", + " [-3, 3, 0, 0, -2, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", + " [ 2, -2, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", + " [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],\n", + " [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],\n", + " [ 0, 0, 0, 0, 0, 0, 0, 0, -3, 3, 0, 0, -2, -1, 0, 0],\n", + " [ 0, 0, 0, 0, 0, 0, 0, 0, 2, -2, 0, 0, 1, 1, 0, 0],\n", + " [-3, 0, 3, 0, 0, 0, 0, 0, -2, 0, -1, 0, 0, 0, 0, 0],\n", + " [ 0, 0, 0, 0, -3, 0, 3, 0, 0, 0, 0, 0, -2, 0, -1, 0],\n", + " [ 9, -9, -9, 9, 6, 3, -6, -3, 6, -6, 3, -3, 4, 2, 2, 1],\n", + " [-6, 6, 6, -6, -3, -3, 3, 3, -4, 4, -2, 2, -2, -2, -1, -1],\n", + " [ 2, 0, -2, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0],\n", + " [ 0, 0, 0, 0, 2, 0, -2, 0, 0, 0, 0, 0, 1, 0, 1, 0],\n", + " [-6, 6, 6, -6, -4, -2, 4, 2, -3, 3, -3, 3, -2, -1, -2, -1],\n", + " [ 4, -4, -4, 4, 2, 2, -2, -2, 2, -2, 2, -2, 1, 1, 1, 1]\n", + " ])\n", + "\n", + " x_size = img.shape[0]\n", + " y_size = img.shape[1]\n", + " \n", + " for i in range(indicies.shape[-1]):\n", + " point = indicies[:, i]\n", + " x_val_floor = int(np.floor(point[0]))\n", + " x_val_ceil = int(np.ceil(point[0]))\n", + " y_val_floor = int(np.floor(point[1]))\n", + " y_val_ceil = int(np.ceil(point[1]))\n", + "\n", + " if 0 < x_val_floor < x_size and 0 < x_val_ceil < x_size and 0 < y_val_floor < y_size and 0 < y_val_ceil < y_size:\n", + " func_values = np.array([\n", + " img[x_val_floor][y_val_floor], img[x_val_ceil][y_val_floor], img[x_val_floor][y_val_ceil], img[x_val_ceil][y_val_ceil],\n", + " dx_img[x_val_floor][y_val_floor], dx_img[x_val_ceil][y_val_floor], dx_img[x_val_floor][y_val_ceil], dx_img[x_val_ceil][y_val_ceil],\n", + " dy_img[x_val_floor][y_val_floor], dy_img[x_val_ceil][y_val_floor], dy_img[x_val_floor][y_val_ceil], dy_img[x_val_ceil][y_val_ceil],\n", + " dxy_img[x_val_floor][y_val_floor], dxy_img[x_val_ceil][y_val_floor], dxy_img[x_val_floor][y_val_ceil], dxy_img[x_val_ceil][y_val_ceil]\n", + " ])\n", + " alpha = inv_matrix @ func_values\n", + " res_point = matrix @ point\n", + " result[int(np.rint(res_point[0]))][int(np.rint(res_point[1]))] = calc_value(point[0] - x_val_floor, point[1] - y_val_floor, alpha)\n", + "\n", + " return result\n", + "\n", + "def calc_value(x, y, alpha):\n", + " x_pow2 = x ** 2\n", + " x_pow3 = x ** 3\n", + " y_pow2 = y ** 2\n", + " y_pow3 = y ** 3\n", + " \n", + " return alpha[0] + alpha[4] * y + alpha[8] * y_pow2 + alpha[12] * y_pow3\\\n", + " + (alpha[1] + alpha[5] * y + alpha[9] * y_pow2 + alpha[13] * y_pow3) * x\\\n", + " + (alpha[2] + alpha[6] * y + alpha[10] * y_pow2 + alpha[14] * y_pow3) * x_pow2\\\n", + " + (alpha[3] + alpha[7] * y + alpha[11] * y_pow2 + alpha[15] * y_pow3) * x_pow3\\" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "img_scale = affine_transformation(img, T_scale)\n", + "img_affine = affine_transformation(img, T_affine)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "plt.imshow(img_scale, cmap='gray')\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "plt.imshow(img_affine, cmap='gray')\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "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.5.1+" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/assignments/04_assignment.ipynb b/assignments/04_assignment.ipynb new file mode 100644 index 0000000..60689e6 --- /dev/null +++ b/assignments/04_assignment.ipynb @@ -0,0 +1,217 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Image Processing SS 18 - Assignment - 04\n", + "\n", + "### Deadline is 16.5.2016 at 8:00 o'clock\n", + "\n", + "Please solve the assignments together with a partner.\n", + "I will run every notebook. Make sure the code runs through. Select `Kernel` -> `Restart & Run All` to test it.\n", + "Please strip the output from the cells, either select `Cell` -> `All Output` -> `Clear` or use the `nb_strip_output.py` script / git hook." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# display the plots inside the notebook\n", + "%matplotlib inline" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "import pylab\n", + "try:\n", + " import urllib.request as urllib2\n", + "except ImportError:\n", + " import urllib2\n", + "\n", + "import random\n", + "try:\n", + " from StringIO import StringIO as BytesIO\n", + "except ImportError:\n", + " from io import BytesIO\n", + " \n", + "from PIL import Image\n", + "\n", + "pylab.rcParams['figure.figsize'] = (12, 12) # This makes the plot bigger" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Exercise 1 - Qualify sharpness and noise - 5 Points\n", + "\n", + "Qualify the noise and sharpness in the images. Make a plot images, noise\n", + "\n", + "Please download sample picture from [here](http://sipi.usc.edu/database/misc.zip)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Load the pictures here\n", + "sample_images = []\n", + "direc = 'misc/' # directory of the sample pictures realtivly to your notebook\n", + "for number in [1,3,5,6]:\n", + " sample_images.append(\n", + " np.array(Image.open(direc+'4.2.0'+str(number)+'.tiff'))\n", + " )\n", + "for name in ['house']:\n", + " sample_images.append(\n", + " np.array(Image.open(direc+name+'.tiff'))\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def qualify_noise(img):\n", + " \"\"\"Qualify the noise based on the std of a gaussian model.\n", + " You may find a window that is contant in the images.\n", + " \"\"\"\n", + " # your code here\n", + " return random.randint(0, 10)\n", + "\n", + "plt.bar(range(len(sample_images)), [qualify_noise(i) for i in sample_images])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def qualify_sharpness(img):\n", + " \"\"\"Qualify the sharpness based on the average pixel differences.\"\"\"\n", + " # your code here\n", + " return random.randint(0, 10)\n", + "plt.bar(range(len(sample_images)), [qualify_sharpness(i) for i in sample_images])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Does the result match the expectations? If not what processing step can be done?/" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Exercise 2 - SSIM JPEG Compression - 5 Points" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def jpeg_enocde(img, quality):\n", + " pil_img = Image.fromarray(img)\n", + " buffer = BytesIO()\n", + " pil_img.save(buffer, \"JPEG\", quality=quality)\n", + " return buffer\n", + "\n", + "def jpeg_decode(buffer):\n", + " img = Image.open(buffer)\n", + " return np.array(img)\n", + "\n", + "def jpeg_quality_filter(img, quality):\n", + " as_jpeg = jpeg_enocde(img, quality)\n", + " return jpeg_decode(as_jpeg)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "images_for_jpeg = sample_images[2::]\n", + "len(images_for_jpeg)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "images10 = [jpeg_quality_filter(img, 10) for img in images_for_jpeg]\n", + "images50 = [jpeg_quality_filter(img, 10) for img in images_for_jpeg]\n", + "images80 = [jpeg_quality_filter(img, 10) for img in images_for_jpeg]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def ssim(img, filtered_img):\n", + " \"\"\"The SSIM similarity measure. Use the parameters from the paper \n", + " as on the second to last slide from the lecture\"\"\"\n", + " # your code\n", + " return random.randint(0, 10)\n", + "\n", + "for i, img in enumerate(images_for_jpeg):\n", + " print(i)\n", + " compressed_images = [images10[i], images50[i], images80[i]]\n", + " plt.bar(range(len(compressed_images)),\n", + " [ssim(img, comp) for comp in compressed_images])\n", + " plt.show()" + ] + }, + { + "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.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +}