bundles / skimage 0.26.1rc0.dev0+git20260530.b607368ff / skimage / segmentation / slic_superpixels / slic
function
skimage.segmentation.slic_superpixels:slic
source: /dev/scikit-image/src/skimage/segmentation/slic_superpixels.py :105
Signature
def slic ( image , n_segments = 100 , compactness = 10.0 , max_num_iter = 10 , sigma = 0 , spacing = None , convert2lab = None , enforce_connectivity = True , min_size_factor = 0.5 , max_size_factor = 3 , slic_zero = False , start_label = 1 , mask = None , * , channel_axis = -1 ) Summary
Segments image using k-means clustering in Color-(x,y,z) space.
Parameters
image: (M, N[, P][, C]) ndarrayInput image. Can be 2D or 3D, and grayscale or multichannel (see
channel_axisparameter). Input image must either be NaN-free or the NaN's must be masked out.n_segments: int, optionalThe (approximate) number of labels in the segmented output image.
compactness: float, optionalBalances color proximity and space proximity. Higher values give more weight to space proximity, making superpixel shapes more square/cubic. In SLICO mode, this is the initial compactness. This parameter depends strongly on image contrast and on the shapes of objects in the image. We recommend exploring possible values on a log scale, e.g., 0.01, 0.1, 1, 10, 100, before refining around a chosen value.
max_num_iter: int, optionalMaximum number of iterations of k-means.
sigma: float or array-like of floats, optionalWidth of Gaussian smoothing kernel for pre-processing for each dimension of the image. The same sigma is applied to each dimension in case of a scalar value. Zero means no smoothing. Note that
sigmais automatically scaled if it is scalar and if a manual voxel spacing is provided (see Notes section). If sigma is array-like, its size must matchimage's number of spatial dimensions.spacing: array-like of floats, optionalThe voxel spacing along each spatial dimension. By default, slic assumes uniform spacing (same voxel resolution along each spatial dimension). This parameter controls the weights of the distances along the spatial dimensions during k-means clustering.
convert2lab: bool, optionalWhether the input should be converted to Lab colorspace prior to segmentation. The input image must be RGB. Highly recommended. This option defaults to
Truewhenchannel_axis` is not None *and* ``image.shape[-1] == 3.enforce_connectivity: bool, optionalWhether the generated segments are connected or not
min_size_factor: float, optionalProportion of the minimum segment size to be removed with respect to the supposed segment size
`depth*width*height/n_segments`max_size_factor: float, optionalProportion of the maximum connected segment size. A value of 3 works in most of the cases.
slic_zero: bool, optionalRun SLIC-zero, the zero-parameter mode of SLIC. [2]
start_label: int, optionalThe labels' index start. Should be 0 or 1.
mask: ndarray, optionalIf provided, superpixels are computed only where mask is True, and seed points are homogeneously distributed over the mask using a k-means clustering strategy. Mask number of dimensions must be equal to image number of spatial dimensions.
channel_axis: int or None, optionalIf None, the image is assumed to be a grayscale (single channel) image. Otherwise, this parameter indicates which axis of the array corresponds to channels.
Returns
labels: 2D or 3D arrayInteger mask indicating segment labels.
Raises
: ValueErrorIf
convert2labis set toTruebut the last array dimension is not of length 3.: ValueErrorIf
start_labelis not 0 or 1.: ValueErrorIf
imagecontains unmasked NaN values.: ValueErrorIf
imagecontains unmasked infinite values.: ValueErrorIf
imageis 2D butchannel_axisis -1 (the default).
Notes
If
sigma > 0, the image is smoothed using a Gaussian kernel prior to segmentation.If
sigmais scalar andspacingis provided, the kernel width is divided along each dimension by the spacing. For example, ifsigma=1andspacing=[5, 1, 1], the effectivesigmais[0.2, 1, 1]. This ensures sensible smoothing for anisotropic images.The image is rescaled to be in [0, 1] prior to processing (masked values are ignored).
Images of shape (M, N, 3) are interpreted as 2D RGB images by default. To interpret them as 3D with the last dimension having length 3, use
channel_axis=None.start_labelis introduced to handle the issue [4]. Label indexing starts at 1 by default.
Examples
from skimage.segmentation import slic from skimage.data import astronaut img = astronaut() segments = slic(img, n_segments=100, compactness=10)✓
segments = slic(img, n_segments=100, compactness=20)
✓Aliases
-
skimage.segmentation.slic