mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-28 11:25:42 +08:00
Fix initialization in km_segmentation, prettier examples
This commit is contained in:
@@ -25,30 +25,19 @@ import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
from skimage.data import lena
|
||||
from skimage.segmentation import quickshift
|
||||
from skimage.segmentation import quickshift, visualize_boundaries
|
||||
from skimage.util import img_as_float
|
||||
from skimage.color import rgb2lab
|
||||
|
||||
img = img_as_float(lena())[::2, ::2, :].copy("C")
|
||||
segments = quickshift(img, kernel_size=5, max_dist=20)
|
||||
segments = quickshift(rgb2lab(img), kernel_size=5, max_dist=20)
|
||||
segments_rgb = quickshift(img, kernel_size=5, max_dist=20)
|
||||
|
||||
print("number of segments: %d" % len(np.unique(segments)))
|
||||
|
||||
fig, (ax_org, ax_sp, ax_mean) = plt.subplots(1, 3)
|
||||
ax_org.set_title("original")
|
||||
ax_org.imshow(img, interpolation='nearest')
|
||||
ax_org.axis("off")
|
||||
|
||||
ax_sp.set_title("superpixels")
|
||||
ax_sp.imshow(segments, interpolation='nearest', cmap=plt.cm.prism)
|
||||
ax_sp.axis("off")
|
||||
|
||||
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
|
||||
ax_mean.set_title("mean color")
|
||||
ax_mean.imshow(colors.T[segments], interpolation='nearest')
|
||||
ax_mean.axis("off")
|
||||
fig.subplots_adjust(wspace=0.02, hspace=0.02, top=0.9,
|
||||
bottom=0.02, left=0.02, right=0.98)
|
||||
boundaries = visualize_boundaries(img, segments)
|
||||
boundaries_rgb = visualize_boundaries(img, segments_rgb)
|
||||
plt.imshow(boundaries)
|
||||
plt.figure()
|
||||
plt.imshow(boundaries_rgb)
|
||||
plt.axis("off")
|
||||
plt.show()
|
||||
|
||||
Reference in New Issue
Block a user