Last active
August 29, 2015 14:03
-
-
Save jni/7f6ac0c8284aba46121c to your computer and use it in GitHub Desktop.
IPython interact ~~fail~~ success!
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
%matplotlib inline | |
from skimage import img_as_float | |
import numpy as np | |
from matplotlib import pyplot as plt | |
def imshow_all(*images, **kwargs): | |
""" Plot a series of images side-by-side. | |
Convert all images to float so that images have a common intensity range. | |
Parameters | |
---------- | |
limits : str | |
Control the intensity limits. By default, 'image' is used set the | |
min/max intensities to the min/max of all images. Setting `limits` to | |
'dtype' can also be used if you want to preserve the image exposure. | |
titles : list of str | |
Titles for subplots. If the length of titles is less than the number | |
of images, empty strings are appended. | |
kwargs : dict | |
Additional keyword-arguments passed to `imshow`. | |
""" | |
images = [img_as_float(img) for img in images] | |
titles = kwargs.pop('titles', []) | |
if len(titles) != len(images): | |
titles = list(titles) + [''] * (len(images) - len(titles)) | |
limits = kwargs.pop('limits', 'image') | |
if limits == 'image': | |
kwargs.setdefault('vmin', min(img.min() for img in images)) | |
kwargs.setdefault('vmax', max(img.max() for img in images)) | |
elif limits == 'dtype': | |
vmin, vmax = dtype_limits(images[0]) | |
kwargs.setdefault('vmin', vmin) | |
kwargs.setdefault('vmax', vmax) | |
nrows, ncols = kwargs.get('shape', (1, len(images))) | |
size = nrows * kwargs.pop('size', 5) | |
width = size * len(images) | |
if nrows > 1: | |
width /= nrows * 1.33 | |
fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(width, size)) | |
for ax, img, label in zip(axes.ravel(), images, titles): | |
ax.imshow(img, **kwargs) | |
ax.set_title(label) | |
def mean_color(image, labels): | |
out = np.zeros_like(image) | |
for label in np.unique(labels): | |
indices = np.nonzero(labels == label) | |
out[indices] = np.mean(image[indices], axis=0) | |
return out | |
from skimage import data, segmentation as seg | |
image = data.lena() | |
def islic(*args, **kwargs): | |
labels = seg.slic(image, *args, **kwargs) | |
label_image = mean_color(image, labels) | |
imshow_all(image, label_image) # plot two items side by side | |
plt.show() | |
from IPython.html import widgets | |
widgets.interact(islic, n_segments=(20, 100, 5), enforce_connectivity=True, | |
compactness=(20., 100., 10.), sigma=(0., 5., 0.1), | |
slic_zero=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment