Color example :)

This commit is contained in:
Andreas Mueller
2012-08-03 11:37:10 +01:00
parent 48fa3252be
commit b977d59c1b
2 changed files with 33 additions and 37 deletions
+18 -34
View File
@@ -1,47 +1,31 @@
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
#from skimage.data import lena
#from skimage.util import img_as_float
from skimage.data import lena
from skimage.segmentation import quickshift
from skimage.util import img_as_float
from IPython.core.debugger import Tracer
tracer = Tracer()
def microstructure(l=256):
"""
Synthetic binary data: binary microstructure with blobs.
Parameters
----------
l: int, optional
linear size of the returned image
"""
n = 5
x, y = np.ogrid[0:l, 0:l]
mask = np.zeros((l, l))
generator = np.random.RandomState(1)
points = l * generator.rand(2, n ** 2)
mask[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1
mask = ndimage.gaussian_filter(mask, sigma=l / (4. * n))
return (mask > mask.mean()).astype(np.float)
#img = img_as_float(lena()[250:300, 250:300])
img = microstructure(l=50)
segments = quickshift(img.reshape(50, 50, 1))
segments = np.unique(segments, return_inverse=True)[1].reshape(50, 50)
intensities = np.bincount(segments.ravel(), img.ravel())
counts = np.bincount(segments.ravel())
intensities /= counts
img = img_as_float(lena())[::3, ::3, :].copy("C")
segments = quickshift(img, sigma=2)
segments = np.unique(segments, return_inverse=True)[1].reshape(img.shape[:2])
plt.subplot(131, title="original")
plt.imshow(img, interpolation='nearest')
plt.figure()
plt.imshow(segments, interpolation='nearest')
plt.figure()
plt.imshow(intensities[segments], interpolation='nearest')
plt.subplot(132, title="superpixels")
# shuffle the labels for better visualization
permuted_labels = np.random.permutation(segments.max() + 1)
plt.imshow(permuted_labels[segments], interpolation='nearest')
plt.subplot(133, title="mean color")
colors = [np.bincount(segments.ravel(), img[:, :, c].ravel()) for c in
xrange(img.shape[2])]
counts = np.bincount(segments.ravel())
colors = np.vstack(colors) / counts
plt.imshow(colors.T[segments], interpolation='nearest')
plt.show()
print("num segments: %d" % len(np.unique(segments)))