FIX width/height trouble, add non-regression test

This commit is contained in:
Andreas Mueller
2012-08-05 21:06:37 +01:00
parent e0034e384a
commit 73dd46019b
6 changed files with 31 additions and 29 deletions
+3 -3
View File
@@ -49,9 +49,9 @@ def _felzenszwalb_segmentation_grey(image, scale=1, sigma=0.8, min_size=20):
down_cost.ravel(), dright_cost.ravel(),
uright_cost.ravel()]).astype(np.float)
# compute edges between pixels:
width, height = image.shape[:2]
height, width = image.shape[:2]
cdef np.ndarray[np.int_t, ndim=2] segments \
= np.arange(width * height).reshape(width, height)
= np.arange(width * height).reshape(height, width)
right_edges = np.c_[segments[1:, :].ravel(), segments[:-1, :].ravel()]
down_edges = np.c_[segments[:, 1:].ravel(), segments[:, :-1].ravel()]
dright_edges = np.c_[segments[1:, 1:].ravel(), segments[:-1, :-1].ravel()]
@@ -107,4 +107,4 @@ def _felzenszwalb_segmentation_grey(image, scale=1, sigma=0.8, min_size=20):
old = flat
flat = flat[flat]
flat = np.unique(flat, return_inverse=True)[1]
return flat.reshape((width, height))
return flat.reshape((height, width))
+13 -13
View File
@@ -91,8 +91,8 @@ def quickshift(image, ratio=1., kernel_size=5, max_dist=10, return_tree=False,
raise ValueError("Sigma should be >= 1")
cdef int w = int(3 * kernel_size)
cdef int width = image_c.shape[0]
cdef int height = image_c.shape[1]
cdef int height = image_c.shape[0]
cdef int width = image_c.shape[1]
cdef int channels = image_c.shape[2]
cdef float closest, dist
cdef int x, y, x_, y_
@@ -101,11 +101,11 @@ def quickshift(image, ratio=1., kernel_size=5, max_dist=10, return_tree=False,
cdef np.float_t* current_pixel_p = image_p
cdef np.ndarray[dtype=np.float_t, ndim=2] densities \
= np.zeros((width, height))
= np.zeros((height, width))
# compute densities
for x, y in product(xrange(width), xrange(height)):
x_min, x_max = max(x - w, 0), min(x + w + 1, width)
y_min, y_max = max(y - w, 0), min(y + w + 1, height)
for x, y in product(xrange(height), xrange(width)):
x_min, x_max = max(x - w, 0), min(x + w + 1, height)
y_min, y_max = max(y - w, 0), min(y + w + 1, width)
for x_, y_ in product(xrange(x_min, x_max), xrange(y_min, y_max)):
dist = 0
for c in xrange(channels):
@@ -115,20 +115,20 @@ def quickshift(image, ratio=1., kernel_size=5, max_dist=10, return_tree=False,
current_pixel_p += channels
# this will break ties that otherwise would give us headache
densities += random_state.normal(scale=0.00001, size=(width, height))
densities += random_state.normal(scale=0.00001, size=(height, width))
# default parent to self:
cdef np.ndarray[dtype=np.int_t, ndim=2] parent \
= np.arange(width * height).reshape(width, height)
= np.arange(width * height).reshape(height, width)
cdef np.ndarray[dtype=np.float_t, ndim=2] dist_parent \
= np.zeros((width, height))
= np.zeros((height, width))
# find nearest node with higher density
current_pixel_p = image_p
for x, y in product(xrange(width), xrange(height)):
for x, y in product(xrange(height), xrange(width)):
current_density = densities[x, y]
closest = np.inf
x_min, x_max = max(x - w, 0), min(x + w + 1, width)
y_min, y_max = max(y - w, 0), min(y + w + 1, height)
x_min, x_max = max(x - w, 0), min(x + w + 1, height)
y_min, y_max = max(y - w, 0), min(y + w + 1, width)
for x_, y_ in product(xrange(x_min, x_max), xrange(y_min, y_max)):
if densities[x_, y_] > current_density:
dist = 0
@@ -152,7 +152,7 @@ def quickshift(image, ratio=1., kernel_size=5, max_dist=10, return_tree=False,
old = flat
flat = flat[flat]
flat = np.unique(flat, return_inverse=True)[1]
flat = flat.reshape(width, height)
flat = flat.reshape(height, width)
if return_tree:
return flat, parent, dist_parent
return flat
+4 -4
View File
@@ -53,13 +53,13 @@ def slic(image, n_segments=100, ratio=10., max_iter=10, sigma=1,
# initialize on grid:
height, width = image.shape[:2]
# approximate grid size for desired n_segments
step = np.sqrt(height * width / n_segments)
step = np.ceil(np.sqrt(height * width / n_segments))
grid_y, grid_x = np.mgrid[:height, :width]
means_y = grid_y[::step, ::step]
means_x = grid_x[::step, ::step]
print(means_y, means_x)
n_seeds = len(means_y)
means_color = np.zeros((n_seeds, n_seeds, 3))
means_color = np.zeros((means_y.shape[0], means_y.shape[1], 3))
cdef np.ndarray[dtype=np.float_t, ndim=2] means \
= np.dstack([means_y, means_x, means_color]).reshape(-1, 5)
cdef np.float_t* current_mean
@@ -92,7 +92,7 @@ def slic(image, n_segments=100, ratio=10., max_iter=10, sigma=1,
y_min = int(max(current_mean[0] - 2 * step, 0))
y_max = int(min(current_mean[0] + 2 * step, height))
x_min = int(max(current_mean[1] - 2 * step, 0))
x_max = int(min(current_mean[1] + 2 * step, height))
x_max = int(min(current_mean[1] + 2 * step, width))
for y in xrange(y_min, y_max):
current_pixel = &image_p[5 * (y * width + x_min)]
current_distance = &distance_p[y * width + x_min]
@@ -6,7 +6,7 @@ from skimage.segmentation import felzenszwalb_segmentation
def test_grey():
# very weak tests. This algorithm is pretty unstable.
img = np.zeros((20, 20))
img = np.zeros((20, 21))
img[:10, 10:] = 0.2
img[10:, :10] = 0.4
img[10:, 10:] = 0.6
@@ -21,7 +21,7 @@ def test_grey():
def test_color():
# very weak tests. This algorithm is pretty unstable.
img = np.zeros((20, 20, 3))
img = np.zeros((20, 21, 3))
img[:10, :10, 0] = 1
img[10:, :10, 1] = 1
img[10:, 10:, 2] = 1
@@ -6,12 +6,13 @@ from skimage.segmentation import quickshift
def test_grey():
rnd = np.random.RandomState(0)
img = np.zeros((20, 20))
img = np.zeros((20, 21))
img[:10, 10:] = 0.2
img[10:, :10] = 0.4
img[10:, 10:] = 0.6
img += 0.1 * rnd.normal(size=img.shape)
seg = quickshift(img, kernel_size=2, max_dist=3, random_seed=0, convert2lab=False, sigma=0)
seg = quickshift(img, kernel_size=2, max_dist=3, random_seed=0,
convert2lab=False, sigma=0)
# we expect 4 segments:
assert_equal(len(np.unique(seg)), 4)
# that mostly respect the 4 regions:
@@ -22,7 +23,7 @@ def test_grey():
def test_color():
rnd = np.random.RandomState(0)
img = np.zeros((20, 20, 3))
img = np.zeros((20, 21, 3))
img[:10, :10, 0] = 1
img[10:, :10, 1] = 1
img[10:, 10:, 2] = 1
@@ -33,14 +34,14 @@ def test_color():
# we expect 4 segments:
assert_equal(len(np.unique(seg)), 4)
assert_array_equal(seg[:10, :10], 0)
assert_array_equal(seg[10:, :10], 2)
assert_array_equal(seg[10:, :10], 3)
assert_array_equal(seg[:10, 10:], 1)
assert_array_equal(seg[10:, 10:], 3)
assert_array_equal(seg[10:, 10:], 2)
seg2 = quickshift(img, kernel_size=1, max_dist=2, random_seed=0,
convert2lab=False, sigma=0)
# very oversegmented:
assert_equal(len(np.unique(seg2)), 11)
assert_equal(len(np.unique(seg2)), 7)
# still don't cross lines
assert_true((seg2[9, :] != seg2[10, :]).all())
assert_true((seg2[:, 9] != seg2[:, 10]).all())
+2 -1
View File
@@ -5,7 +5,7 @@ from skimage.segmentation import slic
def test_color():
rnd = np.random.RandomState(0)
img = np.zeros((20, 20, 3))
img = np.zeros((20, 21, 3))
img[:10, :10, 0] = 1
img[10:, :10, 1] = 1
img[10:, 10:, 2] = 1
@@ -14,6 +14,7 @@ def test_color():
img[img < 0] = 0
seg = slic(img, sigma=0, n_segments=4)
# we expect 4 segments:
print(seg)
assert_equal(len(np.unique(seg)), 4)
assert_array_equal(seg[:10, :10], 0)
assert_array_equal(seg[10:, :10], 2)