{ "metadata": { "name": "", "signature": "sha256:3d97c0849c16ffe89e00a52f0322409be7d815e4f0b3a5eaa596ecc0aba4a0ba" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Hands-on: Numerical Python -- \"Camera\" Excercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Disclaimer/Appraisal**: This excercise is a part of [Practical NeuroImaging](http://github.com/practical-neuroimaging/pna2015) course tought at Berkeley by Matthew Brett (nibabel). His copyright and distributed under Creative Common Attribution (CC-BY 2.0) license." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Compatibility with Python 3\n", "from __future__ import print_function # print('me') instead of print 'me'\n", "from __future__ import division # 1/2 == 0.5, not 0" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Show figures inside the notebook\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np # the Python array package\n", "import matplotlib.pyplot as plt # the Python plotting package" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the `files` sub-directory as this notebook, you will find a file called `camera.txt`. It contains the pixel values for a picture, and it's your job to find what picture that is. The file contains a single floating point value per line. Here are the first four lines of the file:\n", "\n", "```\n", "0.6118\n", "0.6118\n", "0.6196\n", "0.6275\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will first need to read in the file as a list of floating point values. Use any functionality you know by now to read-in those floating point numbers (e.g., presented in [7th class notes (IO)](http://nbviewer.ipython.org/github/dartmouth-pbs/psyc161/blob/master/classes/07c-Numerical-Python-IO_NumPy.ipynb)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Read lines from file and convert to list of floats" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How many pixel values does this image contain? (Call this number `P`).\n", "\n", "The array that forms the image is two-dimensional (it has a number of rows `M` and a number of columns `N`). If we got the right number of pixel values then `M * N == P`. That is, `M` and `N` have to be _factors_ of `P` (the number of pixel values).\n", "\n", "We don't know what `M` and `N` are, we will have to guess. Given the number of pixel values, what would your guess be for `M` and `N`?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Guess M, N" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next we want to convert the list of pixel values to an array (that forms the image).\n", "\n", "Convert the pixel_values list to a pixel values array, and reshape it to your guess for the shape `(M, N)`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Convert list to array and reshape" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Show this as an image using matplotlib's `plt` module:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Show image using plt module" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is this the right shape already?\n", "\n", "What do we need to do to make it look right on the plot?\n", "\n", "Hint - the default `colormap` (`cmap`) in matplotlib is called 'jet'." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# A nicer version of the original plot" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's say we want to binarize this image at some threshold `T`, so the man's coat and hair and the camera tripod are black (pixel value of 0) and most everything else is white (pixel value of 1). How would we choose a good threshold? One step might be to plot a line out of the array (image) to get an idea of the values on that line." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# A plot of the pixel position in x and the pixel value in y, for an image line." ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now you need to apply your threshold to the image to make a binary image where values less than the threshold are == 0 and greater than the threshold are == 1. You might want to play with the threshold a little to get a good result." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Apply threshold to make new binary image, and show binary image" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For extra points - the camera guy has a pocket on side of his coat, that you can't see well at the moment, because the range ov values is too large to distiguish the different dark levels on his coat. Can you make a good picture to show the pocket? Hint: you might want to explore the ``np.clip`` function." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Extra points - a good image of the man's pocket." ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }