From ec6a1769faa51a8f6977c657e137c6c8d028f804 Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Sun, 5 Aug 2012 13:51:55 +0100 Subject: [PATCH] merge segmentation examples --- .../plot_felzenszwalb_segmentation.py | 51 ----------- doc/examples/plot_quickshift.py | 43 ---------- doc/examples/plot_segmentations.py | 85 +++++++++++++++++++ doc/examples/plot_slic.py | 20 ----- 4 files changed, 85 insertions(+), 114 deletions(-) delete mode 100644 doc/examples/plot_felzenszwalb_segmentation.py delete mode 100644 doc/examples/plot_quickshift.py create mode 100644 doc/examples/plot_segmentations.py delete mode 100644 doc/examples/plot_slic.py diff --git a/doc/examples/plot_felzenszwalb_segmentation.py b/doc/examples/plot_felzenszwalb_segmentation.py deleted file mode 100644 index 18ac0f80..00000000 --- a/doc/examples/plot_felzenszwalb_segmentation.py +++ /dev/null @@ -1,51 +0,0 @@ -""" -================================================= -Felzenszwalb's efficient graph based segmentation -================================================= - -This fast 2d image segmentation algorithm, proposed in [1]_ is popular in the -computer vision community. It is often used to extract "superpixels", small -homogeneous image regions, which build the basis for further processing. - -The algorithm has a single ``scale`` parameter that influences the segment -size. The actual size and number of segments can vary greatly, depending on -local contrast. - -.. [1] Efficient graph-based image segmentation, Felzenszwalb, P.F. and - Huttenlocher, D.P. International Journal of Computer Vision, 2004 -""" -print __doc__ - -import matplotlib.pyplot as plt -import numpy as np - -from skimage.data import lena -from skimage.segmentation import felzenszwalb_segmentation -from skimage.util import img_as_float - -img = img_as_float(lena()) -segments = felzenszwalb_segmentation(img, scale=1) -segments = np.unique(segments, return_inverse=True)[1].reshape(img.shape[:2]) - -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) -plt.show() diff --git a/doc/examples/plot_quickshift.py b/doc/examples/plot_quickshift.py deleted file mode 100644 index 9b096459..00000000 --- a/doc/examples/plot_quickshift.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -============================= -Quickshift image segmentation -============================= - -Quickshift is a relatively recent 2d image segmentation algorithm, based on an -approximation of kernelized mean-shift. Therefore it belongs to the family -of local mode-seeking algorithms and is applied to the color+coordinate space, -see [1]_ It is often used to extract "superpixels", small homogeneous image -regions, which build the basis for further processing. - -One of the benefits of quickshift is that it actually computes a -hierarchical segmentation on multiple scales simultaneously. - -Quickshift has two parameters, one controlling the scale of the local -density approximation, the other selecting a level in the hierarchical -segmentation that is produced. - -.. [1] Quick shift and kernel methods for mode seeking, Vedaldi, A. and Soatto, S. - European Conference on Computer Vision, 2008 -""" -print __doc__ - -import matplotlib.pyplot as plt -import numpy as np - -from skimage.data import lena -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(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))) -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() diff --git a/doc/examples/plot_segmentations.py b/doc/examples/plot_segmentations.py new file mode 100644 index 00000000..b204290e --- /dev/null +++ b/doc/examples/plot_segmentations.py @@ -0,0 +1,85 @@ +""" +==================================================== +Comparison of segmentation and superpixel algorithms +==================================================== + +This example compares three popular low-level image segmentation methods. +As it is difficult do obtain good segmentations, and the definition of "good" +often depends on the application, these methods are usually used +for optaining an oversegmentation, also known as superpixels. These superpixels +then serve as the level of operation for more sophisticated algorithms such as CRFs. + + + +Felzenszwalb's efficient graph based segmentation +------------------------------------------------- +This fast 2d image segmentation algorithm, proposed in [1]_ is popular in the +computer vision community. +The algorithm has a single ``scale`` parameter that influences the segment +size. The actual size and number of segments can vary greatly, depending on +local contrast. + +.. [1] Efficient graph-based image segmentation, Felzenszwalb, P.F. and + Huttenlocher, D.P. International Journal of Computer Vision, 2004 + + +Quickshift image segmentation +----------------------------- + +Quickshift is a relatively recent 2d image segmentation algorithm, based on an +approximation of kernelized mean-shift. Therefore it belongs to the family +of local mode-seeking algorithms and is applied to the color+coordinate space, +see [2]_. + +One of the benefits of quickshift is that it actually computes a +hierarchical segmentation on multiple scales simultaneously. + +Quickshift has two parameters, one controlling the scale of the local +density approximation, the other selecting a level in the hierarchical +segmentation that is produced. + +.. [2] Quick shift and kernel methods for mode seeking, + Vedaldi, A. and Soatto, S. + European Conference on Computer Vision, 2008 + + +SLIC - K-Means based image segmentation +--------------------------------------- +This algorithm simply performs K-kmeans in the 5d color-coordinate space and is +therefore closely related to quickshift. As the clustering method is simpler, +it is very efficient. It is essential for this algorithm to work in Lab color +space to obtain good results. The algorithm quickly gained momentum and is now +widely used. See [3] for details. + +.. [3] Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi, + Pascal Fua, and Sabine Suesstrunk, SLIC Superpixels Compared to + State-of-the-art Superpixel Methods, TPAMI, May 2012. +""" +print __doc__ + +import matplotlib.pyplot as plt +import numpy as np + +from skimage.data import lena +from skimage.segmentation import felzenszwalb_segmentation, \ + visualize_boundaries, slic, quickshift +from skimage.util import img_as_float + +img = img_as_float(lena()[::2, ::2]) +segments_fz = felzenszwalb_segmentation(img, scale=100, sigma=0.5, min_size=50) +segments_slic = slic(img, ratio=10, n_segments=250, sigma=1) +segments_quick = quickshift(img, kernel_size=3, max_dist=6, ratio=0.5) + +print("Felzenszwalb's number of segments: %d" % len(np.unique(segments_fz))) +print("Slic number of segments: %d" % len(np.unique(segments_slic))) +print("Quickshift number of segments: %d" % len(np.unique(segments_quick))) + +fig, ax = plt.subplots(1, 3) + +ax[0].imshow(visualize_boundaries(img, segments_fz)) +ax[1].imshow(visualize_boundaries(img, segments_slic)) +ax[2].imshow(visualize_boundaries(img, segments_quick)) +for a in ax: + a.set_xticks(()) + a.set_yticks(()) +plt.show() diff --git a/doc/examples/plot_slic.py b/doc/examples/plot_slic.py deleted file mode 100644 index 8ff59d98..00000000 --- a/doc/examples/plot_slic.py +++ /dev/null @@ -1,20 +0,0 @@ -""" -""" -print __doc__ - -import matplotlib.pyplot as plt -import numpy as np - -from skimage.data import lena -from skimage.segmentation import slic, visualize_boundaries -from skimage.util import img_as_float - -img = img_as_float(lena()).copy("C") -segments = slic(img, ratio=10.0, n_segments=1000) - -print("number of segments: %d" % len(np.unique(segments))) - -boundaries_mine = visualize_boundaries(img, segments) -plt.imshow(boundaries_mine) -plt.axis("off") -plt.show()