Skip to content

Commit

Permalink
Improve _slic.pyx doc, bug fixes, debug print
Browse files Browse the repository at this point in the history
  • Loading branch information
jni committed Jul 2, 2013
1 parent 6e64515 commit 524255c
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions skimage/segmentation/_slic.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,32 @@ def _slic_cython(double[:, :, :, ::1] image_zyx,
double[:, :, ::1] distance,
double[:, ::1] means,
float ratio, int max_iter, int n_segments):
"""Helper function for SLIC segmentation."""
"""Helper function for SLIC segmentation.
Parameters
----------
image_zyx : 4D np.ndarray of double, shape (Z, Y, X, 6)
The image with embedded coordinates, that is, `image_zyx[i, j, k]` is
`array([i, j, k, r, g, b])` or `array([i, j, k, L, a, b])`, depending
on the colorspace.
nearest_mean : 3D np.ndarray of long, shape (Z, Y, X)
The (initially empty) label field.
distance : 3D np.ndarray of double, shape (Z, Y, X)
The (initially infinity) array of distances to the nearest centroid.
means : 2D np.ndarray of double, shape (n_segments, 6)
The centroids obtained by SLIC.
ratio : float
The ratio of xyz-space and colorspace in the clustering.
max_iter : int
The maximum number of k-means iterations.
n_segments : int
The approximate/desired number of segments.
Returns
-------
nearest_mean : 3D np.ndarray of long, shape (Z, Y, X)
The label field/superpixels found by SLIC.
"""

# initialize on grid:
cdef Py_ssize_t depth, height, width
Expand All @@ -39,7 +64,6 @@ def _slic_cython(double[:, :, :, ::1] image_zyx,
#cdef long[::1] nearest_mean_ravel
#cdef double[::1] image_zyx_ravel_j
for i in range(max_iter):
distance[:, :, :] = np.inf
changes = 0
# assign pixels to means
for k in range(n_means):
Expand Down Expand Up @@ -70,10 +94,11 @@ def _slic_cython(double[:, :, :, ::1] image_zyx,
nearest_mean_ravel = np.asarray(nearest_mean).ravel()
means_list = []
for j in range(6):
image_zyx_ravel = np.asarray(image_zyx[:, :, :, j]).ravel()
image_zyx_ravel = np.ascontiguousarray(image_zyx[:, :, :, j]).ravel()
means_list.append(np.bincount(nearest_mean_ravel,
image_zyx_ravel))
in_mean = np.bincount(nearest_mean_ravel)
in_mean[in_mean == 0] = 1
means = (np.vstack(means_list) / in_mean).T.copy("C")
print np.asarray(nearest_mean)
return nearest_mean

0 comments on commit 524255c

Please sign in to comment.