{ "metadata": { "name": "", "signature": "sha256:0b496ba603b98c50efebc3a6b900049d4be52796205c8854145035d8e64892f5" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "##EE16A: Homework 1\n", "This notebook contains the code for Homework problem 1.1.\n", "\n", "###Problem 1.1\n", "\n", "We will give you this following function, which may come in useful:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import Image\n", "import numpy as np\n", "from matplotlib import pyplot as plt\n", "\n", "w,h = 6,6\n", "\n", "def show_black_pixel(row_start, row_end, col_start, col_end):\n", " \"\"\" Generates an image with black pixels within given coordinates \n", " row_start: first row of black pixel region\n", " row_end: last row after black pixel region\n", " col_start: first column of black pixel region\n", " col_end: last column after black pixel region\n", " \n", " Make sure row_end - row-start >= 1 (same with col_end and col_start)\n", "\n", " \"\"\"\n", " data = np.zeros( (w,h), dtype=np.uint8)\n", " data.fill(1)\n", " try:\n", " data[row_start:row_end, col_start:col_end] = 0\n", " except Exception:\n", " print('Make sure your coordinates are in the right format and order!')\n", " plt.imshow(data, interpolation='nearest', cmap='gray')\n", " plt.show()\n", " return data" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Part (a)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Note that to generate a pixel at (0,0), the x-coordinates are (0,1) and the y coordinates are also (0,1)\n", "#grid = show_black_pixel(0, 1, 0, 1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Part (b)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#grid = show_black_pixel()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Part (c)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Generate an image that has all black pixels in the top 2 rows\n", "#grid = show_black_pixel()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Part(d)\n", "The command [np.shape()](http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html) will help you find the dimensions of the data grid. The command [np.reshape()](http://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html) will be useful to convert the grid to a vector. " ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 1.4\n", "\n", "The following function converts a vector to an image and displays it." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def display_image(vector):\n", " data_grid=np.reshape(vector,(50,50))\n", " plt.imshow(data_grid, cmap='gray')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Part(a)\n", "Run the cell below to load in the image vectors." ] }, { "cell_type": "code", "collapsed": false, "input": [ "cal_vector = np.load('cal.npy')\n", "campanile_vector= np.load('campanile.npy')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Display the above images using the ``display_image`` function." ] }, { "cell_type": "code", "collapsed": false, "input": [ "figure()\n", "#TODO: call display_image to show the cal image\n", "\n", "figure()\n", "#TODO: call display_image to show the campanile image\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a scaling matrix that scales each element in a vector by 0.5. The commands [np.eye()](http://docs.scipy.org/doc/numpy/reference/generated/numpy.eye.html) and [np.dot()](http://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html) might be useful." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#scaling_matrix = \n", "#result_vector = " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 41 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Part(b)\n", "Create the Campanile and Cal image scaling matrices separately" ] }, { "cell_type": "code", "collapsed": false, "input": [ "scaling_matrix_cal = np.eye(50*50)\n", "#TODO:Change the scaling matrix here\n", "\n", "scaling_matrix_campanile = np.eye(50*50)\n", "#TODO:Change the scaling matrix here\n", "\n", "#result_vector =\n", "#display_image(result_vector)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 } ], "metadata": {} } ] }