{ "metadata": { "name": "", "signature": "sha256:31b9b150b5137b4658c380c04ed6ec21910e2ef579c19a404f49a6af928d8218" }, "name": "what_extra_transform", "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "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 common modules\n", "import numpy as np # the Python array package\n", "import matplotlib.pyplot as plt # the Python plotting package" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# - set gray colormap and nearest neighbor interpolation by default\n", "plt.rcParams['image.cmap'] = 'gray'\n", "plt.rcParams['image.interpolation'] = 'nearest'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# - import numpy.linalg with a shorter name\n", "import numpy.linalg as npl" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# - make numpy print out values in less verbose form\n", "np.set_printoptions(suppress=True, precision=4)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What transforms did I add?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I took this image\n", "\n", "* `ds114_sub009_highres.nii`\n", "\n", "and I made a new copy, in `img, hdr` format:\n", "\n", "* `ds114_sub009_highres_moved.img`\n", "\n", "I then modified the affine in `ds114_sub009_highres_moved.hdr` to add an extra rotation and translation.\n", "\n", "What extra translation and rotation did I add?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hint: remember the rotation formulae:\n", "\n", "$$\n", "\\begin{alignat}{1}\n", "R_x(\\theta) &= \\begin{bmatrix}\n", "1 & 0 & 0 \\\\\n", "0 & \\cos \\theta & -\\sin \\theta \\\\[3pt]\n", "0 & \\sin \\theta & \\cos \\theta \\\\[3pt]\n", "\\end{bmatrix} \\\\[6pt]\n", "R_y(\\theta) &= \\begin{bmatrix}\n", "\\cos \\theta & 0 & \\sin \\theta \\\\[3pt]\n", "0 & 1 & 0 \\\\[3pt]\n", "-\\sin \\theta & 0 & \\cos \\theta \\\\\n", "\\end{bmatrix} \\\\[6pt]\n", "R_z(\\theta) &= \\begin{bmatrix}\n", "\\cos \\theta & -\\sin \\theta & 0 \\\\[3pt]\n", "\\sin \\theta & \\cos \\theta & 0\\\\[3pt]\n", "0 & 0 & 1\\\\\n", "\\end{bmatrix}\n", "\\end{alignat}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You might also want to compare against results from our `rotations` code:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# - get our functions to make rotation matrices\n", "# If this gives an error - maybe you didn't set your PYTHONPATH? Let us know!\n", "from rotations import x_rotmat, y_rotmat, z_rotmat" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Final hint:\n", "\n", "* inverse sine, cosine are ``np.arcsin, np.arccos``." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Get the original image affine" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Get the new image affine" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Work out what transform has been added in the new affine" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# What rotation and translation has been applied?" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }