Created
June 21, 2014 19:40
-
-
Save cpcloud/1cdaf217730786f5e6bd to your computer and use it in GitHub Desktop.
hack cell image segmentation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:debe7ece4be5302afd39b238bace9c32ef0ad8b91642bbb2d8a3695555ed9ef7" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%matplotlib" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Using matplotlib backend: Qt4Agg\n" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from functools import partial\n", | |
"from skimage.io import imread\n", | |
"from skimage.filter import canny, rank, threshold_otsu\n", | |
"from skimage import exposure as expos\n", | |
"from skimage.feature import blob_dog\n", | |
"from skimage.morphology import disk\n", | |
"from skimage.segmentation import felzenszwalb, slic, quickshift\n", | |
"from skimage.segmentation import mark_boundaries\n", | |
"\n", | |
"from matplotlib.widgets import RectangleSelector\n", | |
"import matplotlib.pyplot as plt\n", | |
"\n", | |
"def segment(img, stepsize=8, **kwargs):\n", | |
" sub = img[::stepsize, ::stepsize]\n", | |
" return felzenszwalb(sub, **kwargs), sub" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 33 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"img = imread('DATA_SET_2_0841.tif')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 48 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def onselect(ax, click, release):\n", | |
" column_slice = slice(click.xdata, release.xdata)\n", | |
" row_slice = slice(click.ydata, release.ydata)\n", | |
" indexer = row_slice, column_slice\n", | |
" \n", | |
" print('equalizing image subset ...')\n", | |
" sub = img[indexer]\n", | |
" im_eq = rank.autolevel(sub, disk(20))\n", | |
"# im_eq = expos.equalize_hist(sub)\n", | |
" \n", | |
" print('segmenting image subset ...')\n", | |
" res, sub = segment(im_eq, stepsize=2, scale=300, sigma=3.5)\n", | |
" \n", | |
" print('marking boundaries ...')\n", | |
" bounds = mark_boundaries(sub, res)\n", | |
" \n", | |
" print('drawing boundaries ...')\n", | |
" plt.imshow(bounds, cmap=plt.cm.jet)\n", | |
" \n", | |
" \n", | |
"def toggle_selector(event):\n", | |
" if event.key in ['Q', 'q'] and toggle_selector.RS.active:\n", | |
" toggle_selector.RS.set_active(False)\n", | |
" if event.key in ['A', 'a'] and not toggle_selector.RS.active:\n", | |
" toggle_selector.RS.set_active(True)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 53 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"fig, ax = plt.subplots(1, 1)\n", | |
"ax.imshow(img, cmap=plt.cm.gray)\n", | |
"ax.axis('off')\n", | |
"fig.tight_layout()\n", | |
"toggle_selector.RS = RectangleSelector(ax, partial(onselect, ax),\n", | |
" drawtype='box')\n", | |
"# plt.connect('key_press_event', toggle_selector)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"equalizing image subset ...\n", | |
"segmenting image subset ..." | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"marking boundaries ..." | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"drawing boundaries ..." | |
] | |
} | |
], | |
"prompt_number": 54 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sub = expos.equalize_hist(img[indexer])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 105 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": true, | |
"input": [ | |
"res, sub = segment(sub, stepsize=2, scale=1000, sigma=1.5)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 106 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"bounds = mark_boundaries(sub, res)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 107 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"plt.imshow(bounds, cmap=plt.cm.jet)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 108, | |
"text": [ | |
"<matplotlib.image.AxesImage at 0x7fe95ca39d90>" | |
] | |
} | |
], | |
"prompt_number": 108 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment