mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-05 08:48:24 +08:00
Added max_size as a parameter
This commit is contained in:
@@ -11,7 +11,8 @@ from skimage.util import regular_grid
|
||||
|
||||
def _enforce_label_connectivity_cython(Py_ssize_t[:, :, ::1] nearest_segments,
|
||||
Py_ssize_t n_segments,
|
||||
int min_size):
|
||||
int min_size,
|
||||
int max_size):
|
||||
""" Helper function to remove small disconnected regions from the labels
|
||||
|
||||
Parameters
|
||||
@@ -22,7 +23,9 @@ def _enforce_label_connectivity_cython(Py_ssize_t[:, :, ::1] nearest_segments,
|
||||
number of specified segments
|
||||
min_size: int
|
||||
minimum size of the segment
|
||||
|
||||
max_size: int
|
||||
maximum size of the segment. This is done for performance reasons,
|
||||
to pre-allocate a sufficiently large array for the breadth first search
|
||||
Returns
|
||||
-------
|
||||
connected_nearest_segments : 3D array of int, shape (Z, Y, X)
|
||||
@@ -40,9 +43,6 @@ def _enforce_label_connectivity_cython(Py_ssize_t[:, :, ::1] nearest_segments,
|
||||
cdef Py_ssize_t[:] ddy = np.array((0,0,1,-1,0,0))
|
||||
cdef Py_ssize_t[:] ddz = np.array((0,0,0,0,1,-1))
|
||||
|
||||
cdef double size = height*width*depth / n_segments
|
||||
cdef double max_size = 3*size
|
||||
|
||||
#new object with connected segments
|
||||
cdef Py_ssize_t[:, :, ::1] new_nearest_segments \
|
||||
= np.zeros((depth, height, width), dtype=np.intp)
|
||||
|
||||
@@ -12,7 +12,7 @@ from skimage.color import rgb2lab
|
||||
|
||||
def slic(image, n_segments=100, compactness=10., max_iter=10, sigma=None,
|
||||
spacing=None, multichannel=True, convert2lab=True, ratio=None,
|
||||
enforce_connectivity=True, min_size_factor=0.5):
|
||||
enforce_connectivity=True, min_size_factor=0.5, max_size_factor=3):
|
||||
"""Segments image using k-means clustering in Color-(x,y,z) space.
|
||||
|
||||
Parameters
|
||||
@@ -53,7 +53,9 @@ def slic(image, n_segments=100, compactness=10., max_iter=10, sigma=None,
|
||||
min_size_factor: float
|
||||
proportion of the minimum segment size to be removed with respect
|
||||
to the supposed segment size (depth*width*height/n_segments)
|
||||
|
||||
max_size_factor: float
|
||||
proportion of the maximum connected segment size. A value of 3 works
|
||||
in most of the cases.
|
||||
Returns
|
||||
-------
|
||||
labels : 2D or 3D array
|
||||
@@ -170,7 +172,11 @@ def slic(image, n_segments=100, compactness=10., max_iter=10, sigma=None,
|
||||
labels = _slic_cython(image, segments, max_iter, spacing, enforce_connectivity)
|
||||
|
||||
if (enforce_connectivity):
|
||||
labels = _enforce_label_connectivity_cython(labels, n_segments, min_size_factor*depth*height*width/n_segments)
|
||||
segment_size = depth*height*width/n_segments
|
||||
labels = _enforce_label_connectivity_cython(labels,
|
||||
n_segments,
|
||||
min_size_factor*segment_size,
|
||||
max_size_factor*segment_size)
|
||||
|
||||
if is_2d:
|
||||
labels = labels[0]
|
||||
|
||||
Reference in New Issue
Block a user