skimage.segmentation.clear_border(image[, ...]) | Clear objects connected to image border. |
skimage.segmentation.felzenszwalb(image[, ...]) | Computes Felsenszwalb’s efficient graph based image segmentation. |
skimage.segmentation.find_boundaries(label_img) | |
skimage.segmentation.quickshift | Segments image using quickshift clustering in Color-(x,y) space. |
skimage.segmentation.random_walker(data, labels) | Random walker algorithm for segmentation from markers, for gray-level or multichannel images. |
skimage.segmentation.slic | Segments image using k-means clustering in Color-(x,y) space. |
skimage.segmentation.visualize_boundaries(...) |
Clear objects connected to image border.
The changes will be applied to the input image.
Parameters : | image : (N, M) array
buffer_size : int, optional
bgval : float or int, optional
|
---|---|
Returns : | image : (N, M) array
|
Examples
>>> import numpy as np
>>> from skimage.segmentation import clear_border
>>> image = np.array([[0, 0, 0, 0, 0, 0, 0, 1, 0],
... [0, 0, 0, 0, 1, 0, 0, 0, 0],
... [1, 0, 0, 1, 0, 1, 0, 0, 0],
... [0, 0, 1, 1, 1, 1, 1, 0, 0],
... [0, 1, 1, 1, 1, 1, 1, 1, 0],
... [0, 0, 0, 0, 0, 0, 0, 0, 0]])
>>> clear_border(image)
array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]])
Computes Felsenszwalb’s efficient graph based image segmentation.
Produces an oversegmentation of a multichannel (i.e. RGB) image using a fast, minimum spanning tree based clustering on the image grid. The parameter scale sets an observation level. Higher scale means less and larger segments. sigma is the diameter of a Gaussian kernel, used for smoothing the image prior to segmentation.
The number of produced segments as well as their size can only be controlled indirectly through scale. Segment size within an image can vary greatly depending on local contrast.
For RGB images, the algorithm computes a separate segmentation for each channel and then combines these. The combined segmentation is the intersection of the separate segmentations on the color channels.
Parameters : | image : (width, height, 3) or (width, height) ndarray
scale : float
sigma : float
min_size : int
|
---|---|
Returns : | segment_mask : (width, height) ndarray
|
References
[R82] | Efficient graph-based image segmentation, Felzenszwalb, P.F. and Huttenlocher, D.P. International Journal of Computer Vision, 2004 |
Segments image using quickshift clustering in Color-(x,y) space.
Produces an oversegmentation of the image using the quickshift mode-seeking algorithm.
Parameters : | image : (width, height, channels) ndarray
ratio : float, between 0 and 1.
kernel_size : float
max_dist : float
return_tree : bool
sigma : float
convert2lab : bool
random_seed : None or int
|
---|---|
Returns : | segment_mask : (width, height) ndarray
|
Notes
The authors advocate to convert the image to Lab color space prior to segmentation, though this is not strictly necessary. For this to work, the image must be given in RGB format.
References
[R83] | Quick shift and kernel methods for mode seeking, Vedaldi, A. and Soatto, S. European Conference on Computer Vision, 2008 |
Random walker algorithm for segmentation from markers, for gray-level or multichannel images.
Parameters : | data : array_like
labels : array of ints, of same shape as data without channels dimension
beta : float
mode : {‘bf’, ‘cg_mg’, ‘cg’} (default: ‘bf’)
tol : float
copy : bool
multichannel : bool, default False
return_full_prob : bool, default False
depth : float, default 1.
|
---|---|
Returns : | output : ndarray
|
See also
Notes
Multichannel inputs are scaled with all channel data combined. Ensure all channels are separately normalized prior to running this algorithm.
The depth argument is specifically for certain types of 3-dimensional volumes which, due to how they were acquired, have different spacing along in-plane and out-of-plane dimensions. This is commonly encountered in medical imaging. The depth argument corrects gradients calculated along the third spatial dimension for the otherwise inherent assumption that all points are equally spaced.
The algorithm was first proposed in Random walks for image segmentation, Leo Grady, IEEE Trans Pattern Anal Mach Intell. 2006 Nov;28(11):1768-83.
The algorithm solves the diffusion equation at infinite times for sources placed on markers of each phase in turn. A pixel is labeled with the phase that has the greatest probability to diffuse first to the pixel.
The diffusion equation is solved by minimizing x.T L x for each phase, where L is the Laplacian of the weighted graph of the image, and x is the probability that a marker of the given phase arrives first at a pixel by diffusion (x=1 on markers of the phase, x=0 on the other markers, and the other coefficients are looked for). Each pixel is attributed the label for which it has a maximal value of x. The Laplacian L of the image is defined as:
- L_ii = d_i, the number of neighbors of pixel i (the degree of i)
- L_ij = -w_ij if i and j are adjacent pixels
The weight w_ij is a decreasing function of the norm of the local gradient. This ensures that diffusion is easier between pixels of similar values.
When the Laplacian is decomposed into blocks of marked and unmarked pixels:
L = M B.T
B A
with first indices corresponding to marked pixels, and then to unmarked pixels, minimizing x.T L x for one phase amount to solving:
A x = - B x_m
where x_m = 1 on markers of the given phase, and 0 on other markers. This linear system is solved in the algorithm using a direct method for small images, and an iterative method for larger images.
Examples
>>> a = np.zeros((10, 10)) + 0.2*np.random.random((10, 10))
>>> a[5:8, 5:8] += 1
>>> b = np.zeros_like(a)
>>> b[3,3] = 1 #Marker for first phase
>>> b[6,6] = 2 #Marker for second phase
>>> random_walker(a, b)
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 2, 2, 2, 1, 1],
[1, 1, 1, 1, 1, 2, 2, 2, 1, 1],
[1, 1, 1, 1, 1, 2, 2, 2, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int32)
Segments image using k-means clustering in Color-(x,y) space.
Parameters : | image : (width, height, 3) ndarray
n_segments : int
ratio: float :
max_iter : int
sigma : float
convert2lab : bool
|
---|---|
Returns : | segment_mask : (width, height) ndarray
|
Notes
The image is smoothed using a Gaussian kernel prior to segmentation.
References
[R84] | Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi, Pascal Fua, and Sabine Süsstrunk, SLIC Superpixels Compared to State-of-the-art Superpixel Methods, TPAMI, May 2012. |
Examples
>>> from skimage.segmentation import slic
>>> from skimage.data import lena
>>> img = lena()
>>> segments = slic(img, n_segments=100, ratio=10)
>>> # Increasing the ratio parameter yields more square regions
>>> segments = slic(img, n_segments=100, ratio=20)