From ccfb89b9570598f41cff7ad4d34f3236c1d911f3 Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Fri, 22 Jun 2012 23:08:59 +0200 Subject: [PATCH] FIX Tried to address @stefanv's comments on the PR. --- .../plot_felzenszwalb_segmentation.py | 25 +++++++++---------- doc/examples/plot_quickshift.py | 24 ++++++++---------- skimage/segmentation/__init__.py | 2 +- skimage/segmentation/_felzenszwalb.pyx | 6 ++--- skimage/segmentation/felzenszwalb.py | 10 +++----- skimage/segmentation/quickshift.pyx | 4 +-- 6 files changed, 31 insertions(+), 40 deletions(-) diff --git a/doc/examples/plot_felzenszwalb_segmentation.py b/doc/examples/plot_felzenszwalb_segmentation.py index 3d06dd2a..18ac0f80 100644 --- a/doc/examples/plot_felzenszwalb_segmentation.py +++ b/doc/examples/plot_felzenszwalb_segmentation.py @@ -29,24 +29,23 @@ segments = np.unique(segments, return_inverse=True)[1].reshape(img.shape[:2]) print("number of segments: %d" % len(np.unique(segments))) -plt.subplot(131, title="original") -plt.imshow(img, interpolation='nearest') -plt.axis("off") -plt.subplot(132, title="segmentation") -# shuffle the labels for better visualization -permuted_labels = np.random.permutation(segments.max() + 1) -plt.imshow(permuted_labels[segments], interpolation='nearest') -plt.axis("off") +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") -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.axis("off") - -plt.subplots_adjust(wspace=0.02, hspace=0.02, top=0.9, +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 index 1285ad28..5adf1969 100644 --- a/doc/examples/plot_quickshift.py +++ b/doc/examples/plot_quickshift.py @@ -33,24 +33,22 @@ segments = quickshift(img, kernel_size=5, max_dist=20) print("number of segments: %d" % len(np.unique(segments))) -plt.subplot(131, title="original") -plt.imshow(img, interpolation='nearest') -plt.axis("off") +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") -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.axis("off") +ax_sp.set_title("superpixels") +ax_sp.imshow(segments, interpolation='nearest', cmap=plt.cm.prism) +ax_sp.axis("off") -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.axis("off") - -plt.subplots_adjust(wspace=0.02, hspace=0.02, top=0.9, +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/skimage/segmentation/__init__.py b/skimage/segmentation/__init__.py index 2de27ad1..b2c97448 100644 --- a/skimage/segmentation/__init__.py +++ b/skimage/segmentation/__init__.py @@ -4,4 +4,4 @@ from .km_segmentation import km_segmentation from .quickshift import quickshift __all__ = [random_walker, quickshift, felzenszwalb_segmentation, - km_segmentation] + km_segmentation] diff --git a/skimage/segmentation/_felzenszwalb.pyx b/skimage/segmentation/_felzenszwalb.pyx index d058975d..a677020a 100644 --- a/skimage/segmentation/_felzenszwalb.pyx +++ b/skimage/segmentation/_felzenszwalb.pyx @@ -22,12 +22,10 @@ def _felzenszwalb_segmentation_grey(image, scale=1, sigma=0.8): Parameters ---------- - image: ndarray, [width, height] + image: (width, height) ndarray Input image - scale: float Free parameter. Higher means larger clusters. - sigma: float Width of Gaussian kernel used in preprocessing. @@ -74,7 +72,7 @@ def _felzenszwalb_segmentation_grey(image, scale=1, sigma=0.8): # set costs_p back one. we increase it before we use it # since we might continue before that. costs_p -= 1 - for e in xrange(costs.size): + for e in range(costs.size): seg0 = find_root(segments_p, edges_p[0]) seg1 = find_root(segments_p, edges_p[1]) edges_p += 2 diff --git a/skimage/segmentation/felzenszwalb.py b/skimage/segmentation/felzenszwalb.py index 54846102..cfcf4f23 100644 --- a/skimage/segmentation/felzenszwalb.py +++ b/skimage/segmentation/felzenszwalb.py @@ -23,13 +23,10 @@ def felzenszwalb_segmentation(image, scale=1, sigma=0.8): Parameters ---------- - image: ndarray, [width, height] + image: (width, height) ndarray Input image - scale: float Free parameter. Higher means larger clusters. - For 0-255 data, hundereds are good. - sigma: float Width of Gaussian kernel used in preprocessing. @@ -69,9 +66,8 @@ def felzenszwalb_segmentation(image, scale=1, sigma=0.8): # we do this by combining the channels to one number n0 = segmentations[0].max() + 1 n1 = segmentations[1].max() + 1 - hasher = np.array([n1 * n0, n0, 1]) - segmentations = np.dstack(segmentations).reshape(-1, n_channels) - segmentation = np.dot(segmentations, hasher) + segmentation = (segmentations[0] + segmentations[1] * n0 + + segmentations[2] * n0 * n1) # make segment labels consecutive numbers starting at 0 labels = np.unique(segmentation, return_inverse=True)[1] return labels.reshape(image.shape[:2]) diff --git a/skimage/segmentation/quickshift.pyx b/skimage/segmentation/quickshift.pyx index 1822055b..b0a9b2d6 100644 --- a/skimage/segmentation/quickshift.pyx +++ b/skimage/segmentation/quickshift.pyx @@ -21,7 +21,7 @@ def quickshift(image, ratio=1., kernel_size=5, max_dist=10, return_tree=False, r Parameters ---------- - image: ndarray, [width, height, channels] + image: (width, height, channels) ndarray Input image ratio: float, between 0 and 1. Balances color-space proximity and image-space proximity. @@ -54,7 +54,7 @@ def quickshift(image, ratio=1., kernel_size=5, max_dist=10, return_tree=False, r """ image = np.atleast_3d(image) - cdef np.ndarray[dtype=np.float_t, ndim=3, mode="c"] image_c = img_as_float(np.ascontiguousarray(image)) * ratio + cdef np.ndarray[dtype=np.float_t, ndim=3, mode="c"] image_c = np.ascontiguousarray(img_as_float(image)) * ratio if random_seed is None: random_state = np.random.RandomState()