{ "metadata": { "name": "", "signature": "sha256:7e14ecd369d48df51fb2f843f17504a9bd2927122c015435620054ee0907bd82" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "##EE16A: Homework 3\n", "This notebook contains the code for Homework problem 3.1 and 3.4" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "from scipy import ndimage as nd\n", "from scipy import misc\n", "from scipy import io" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 3.1 (b)\n", "Download the campanile image. Again, make sure to check the dimensions of the image using the shape command" ] }, { "cell_type": "code", "collapsed": false, "input": [ "campanile = np.load('campanile.npy')\n", "plt.imshow(campanile, cmap='gray', interpolation='nearest')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Design the distortion matrix and plot the resultant image." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#D = \n", "\n", "#blurred_image = \n", "\n", "\n", "figure(figsize=(15,10))\n", "subplot(1,2,1)\n", "plt.imshow(campanile, cmap='gray', interpolation='nearest')\n", "title('Original Image')\n", "subplot(1,2,2)\n", "plt.imshow(blurred_image, cmap='gray', interpolation='nearest')\n", "title('Blurred Image')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 3.1 (d)\n", "Download the distortion matrix and blurred image" ] }, { "cell_type": "code", "collapsed": false, "input": [ "blur_rand = np.load('distortion.npy') #this is the new distortion matrix!\n", "blurred = np.load('blurred.npy') #this is the blurred image\n", "plt.imshow(blurred, cmap='gray', interpolation='nearest')\n", "title('Blurred Image')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recover the original image" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#original = \n", "plt.imshow(original, cmap='gray', interpolation='nearest')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 3.4 (b)\n", "Load Pattern Image. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "pattern = np.load('pattern.npy')\n", "plt.imshow(pattern, cmap='gray', interpolation='nearest')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 44 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the command [shape](http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html) to find the dimensions of the image. How many eigenvalues do you expect? \n", "\n", "Run the code below to find the eigenvector and eigenvalues of ``pattern`` and sort them in descending order (first eigenvalue/vector corresponds to the largest eigenvalue)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "eig_vals, eig_vectors = np.linalg.eig(pattern)\n", "idx = (eig_vals.argsort())\n", "idx = idx[::-1]\n", "eig_vals = eig_vals[idx]\n", "eig_vectors = eig_vectors[:,idx] " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 3.4 (c)\n", "Find the pattern approximation using 100 largest eigenvalues/eigenvectors.\n", "\n", "* Index into above variables to choose the first 100 eigenvalues and eigenvectors.\n", "* You might find the command [np.diag](http://docs.scipy.org/doc/numpy/reference/generated/numpy.diag.html) useful\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#pattern_approx_100 = \n", "plt.imshow(pattern_approx_100, cmap='gray', vmin=0, vmax=255)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 43 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 3.4 (d)\n", "Find the pattern approximation using 50 largest eigenvalues/eigenvectors" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#pattern_approx_50 = \n", "\n", "plt.imshow(pattern_approx_50, cmap='gray', vmin=0, vmax=255)" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }