FIX Tried to address @stefanv's comments on the PR.

This commit is contained in:
Andreas Mueller
2012-06-22 23:08:59 +02:00
parent 05cc863f3f
commit ccfb89b957
6 changed files with 31 additions and 40 deletions
+12 -13
View File
@@ -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()
+11 -13
View File
@@ -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()
+1 -1
View File
@@ -4,4 +4,4 @@ from .km_segmentation import km_segmentation
from .quickshift import quickshift
__all__ = [random_walker, quickshift, felzenszwalb_segmentation,
km_segmentation]
km_segmentation]
+2 -4
View File
@@ -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
+3 -7
View File
@@ -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])
+2 -2
View File
@@ -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()