{ "metadata": { "name": "", "signature": "sha256:bb7a54e69463063da2797475236052e4b728f3e90ef047edc905e7129cb6bea1" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "# setup\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction: images are numpy arrays\n", "\n", "A grayscale image is just a 2D array:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "r = np.random.rand(500, 500)\n", "from matplotlib import pyplot as plt, cm\n", "plt.imshow(r, cmap=cm.gray, interpolation='nearest')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Trying that with a real picture:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from skimage import data\n", "coins = data.coins()\n", "print(type(coins))\n", "print(coins.dtype)\n", "print(coins.shape)\n", "plt.imshow(coins, cmap=cm.gray, interpolation='nearest')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A color image is a 3D array, where the last dimension has size 3 and represents the red, green, and blue channels:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "lena = data.lena()\n", "print(lena.shape)\n", "plt.imshow(lena, interpolation='nearest')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These are *just numpy arrays*. Making a red square is easy using just array slicing and manipulation:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "lena[100:200, 100:200, :] = [255, 0, 0] # [red, green, blue]\n", "plt.imshow(lena)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we will see, this opens up many lines of analysis for free.\n", "\n", "## Exercise: draw an H\n", "\n", "Define a function that takes as input an RGB image and a pair of coordinates (row, column), and returns the image (optionally a copy) with green letter H overlaid at those coordinates. The coordinates should point to the top-left corner of the H.\n", "\n", "The arms and strut of the H should have a width of 3 pixels, and the H itself should have a height of 24 pixels and width of 20 pixels." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def draw_h(image, coords, in_place=True):\n", " pass # code goes here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Test your function like so:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "lena_h = draw_h(lena, (50, -50), in_place=False)\n", "plt.imshow(lena_h)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bonus points: RGB intensity plot\n", "\n", "Plot the intensity of each channel of the image along some row." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def plot_intensity(image, row):\n", " pass # code goes here" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Test your function here:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "plot_intensity(coins, 50)\n", "plot_intensity(lena, 250)" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }