{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

\n", "NASA Goddard Space Flight Center
\n", " Python User Group
\n", "2014 Python Boot Camp\n", "

\n", "
" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Scientific Programming I: Breakout Solutions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "from scipy import misc\n", "from scipy import ndimage" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import matplotlib\n", "matplotlib.rcParams['savefig.dpi'] = 2 * matplotlib.rcParams['savefig.dpi'] # Increase default figure resolution" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1) Open the image file MV_HFV_012.png and display it. This Scanning Element Microscopy image shows a glass sample (light gray) with some bubbles (on black) and unmolten sand grains (dark gray)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "img = plt.imread(\"MV_HFV_012.png\")\n", "fig = plt.figure()\n", "ax = fig.add_subplot(111)\n", "ax.imshow(img, cmap=cm.gray)\n", "ax.axis('off')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2) Crop the image to remove the lower panel with measure information." ] }, { "cell_type": "code", "collapsed": false, "input": [ "trim = img[:-60,:]\n", "fig = plt.figure()\n", "ax = fig.add_subplot(111)\n", "ax.imshow(trim, cmap=cm.gray)\n", "ax.axis('off')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3) Slightly filter the image with a median filter in order to refine its histogram. Check how the histogram changes." ] }, { "cell_type": "code", "collapsed": false, "input": [ "filtdat = ndimage.median_filter(trim, size=(7,7))\n", "thist = plt.hist(np.ravel(trim), bins=np.linspace(0,1,256), histtype=\"step\", label=\"Unfiltered\")\n", "fhist = plt.hist(np.ravel(filtdat), bins=np.linspace(0,1,256), histtype=\"step\", label=\"Filtered\")\n", "plt.legend()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4) Using the histogram of the filtered image, determine thresholds that allow to define masks for sand pixels, glass pixels and bubble pixels." ] }, { "cell_type": "code", "collapsed": false, "input": [ "bubble = filtdat <= 0.2\n", "sand = np.logical_and(filtdat > 0.2, filtdat <= 0.45)\n", "glass = filtdat > 0.45" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5) Display an image in which the three phases are colored with three different colors." ] }, { "cell_type": "code", "collapsed": false, "input": [ "phases = glass.astype(np.int) + 2*sand.astype(np.int) + 3*bubble.astype(np.int)\n", "fig = plt.figure()\n", "ax = fig.add_subplot(121)\n", "ax.imshow(phases)\n", "ax.axis('off')\n", "ax2 = fig.add_subplot(122)\n", "ax2.imshow(filtdat, cmap=cm.gray)\n", "ax2.axis('off')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6) Attribute labels to all bubbles and sand grains, and remove from the sand mask grains that are smaller than 10 pixels. To do so, use ndimage.sum or np.bincount to compute the grain sizes." ] }, { "cell_type": "code", "collapsed": false, "input": [ "sand_labels, sand_nb = ndimage.label(sand)\n", "sand_areas = np.array(ndimage.sum(sand, sand_labels, np.arange(sand_labels.max()+1)))\n", "mask = sand_areas > 100\n", "remove_small_sand = mask[sand_labels.ravel()].reshape(sand_labels.shape)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "7) Compute the mean and median size of bubbles." ] }, { "cell_type": "code", "collapsed": false, "input": [ "bubbles_labels, bubbles_nb = ndimage.label(bubble)\n", "bubbles_areas = np.bincount(bubbles_labels.ravel())[1:]\n", "mean_bubble_size = bubbles_areas.mean()\n", "median_bubble_size = np.median(bubbles_areas)\n", "mean_bubble_size, median_bubble_size" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }