kmeans

SLIC

author: Richard Bonye (github Boyne272) Last updated on Wed Aug 28 08:28:20 2019

class TSA.kmeans.SLIC.SLIC(img, bin_grid, dist_metric=None, dist_metric_args=None)

Implements Kmeans clustering on an image in 5d color position space using localised bins on a regular grid to enforce locality.

SLIC(img, bin_grid, dist_metric=None, dist_metric_args=[1.,])

Parameters:
  • img (3d numpy array) – Image to be segmented by kmeans, expected in shape [x_dim, y_dim, rgb] with all values in the interval [0, 1], not 255.
  • bin_grid (tuple length 2) – The number of initial partitions in x and y respectivly, therefore the number of segments is the product of these. Both must be a factor of the x, y dimensions for the whole image. This intial segmentation restrains the kmeans centers in space, forcing locality of segments and speeding up the algorithm.
  • dist_metric (function, optional) – The method of calculating distance between vectors and cluster centers. This must have form f(vecs, clusts, *args) where vecs and clusts are rank 2 tensors with samples on the first axis. This should return a rank 2 tensor of distances with shape (clusters, vectors) such that taking the argmin along dim=1 gives an array of closest cluster centroids for each vector.
  • dist_metric_args (tuple, optional) – arguments to passed into dist_metric function. By defualt this is a single parameter for the bia to color over distance.
get_segmentation()

Returns the current segmentation mask as a numpy array

iterate(n_iter)

loop for n_iter iterations with progress bar

plot(option='default', ax=None)
Plot a one fo the following options on an axis if specified:
  • ‘default’ image and the segement outlines
  • ‘setup’ image, bin edges and cluster centers
  • ‘segments’ the sedment mask
  • ‘edges’ just the segment outlines in a transparent manner
  • ‘img’ just the orinal image
  • ‘centers’ each kmean centroid
  • ‘bin_edges’ the bin mesh used
  • ‘bins’ both ‘img’ and ‘bin_edges’
  • ‘time’ the iterations vs time

If path is given the image will be saved on that path.

If no axis is given one will be made.

class TSA.kmeans.SLIC.bin_base(bin_grid, dim_x, dim_y)

Class to hold methods for binning vectors into a regular grid.

bin_base(bin_grid, dim_x, dim_y)

Parameters:
  • bin_grid (tuple) – The number of grid cordinates in the x and y directions. Must be length 2.
  • dim_y (dim_x,) – The dimensions of the whole grid

MSLIC

author: Richard Bonye (github Boyne272) Last updated on Wed Aug 28 08:27:33 2019

class TSA.kmeans.MSLIC.MSLIC_wrapper(imgs, bin_grid, combo_metric='max', combo_metric_args=None, dist_metric=None, dist_metric_args=[1.0])

Wrapper around SLIC to run multiple instances at once, using a the combined of each instance to assign clusters in every iteration

MSLIC_wrapper(imgs, bin_grid, combo_metric=’max’, combo_metric_args=[],
dist_metric=None, dist_metric_args=[1.,])
Parameters:
  • imgs (list of 3d numpy arrays) – The imgs on which the SLIC algorithm is to be run in parallel. Each must have the same shape and have a
  • bin_grid (tuple length 2 (same as SLIC)) – The number of initial partitions in x and y respectivly, therefore the number of segments is the product of these. Both must be a factor of the x, y dimensions for the whole image. This intial segmentation restrains the kmeans centers in space, forcing locality of segments and speeding up the algorithm.
  • combo_metric (string or callable, optional) –

    The metric by which the distances between vectors and centroids in each image are to be combined. Allowed string options are: - ‘max’ maximum of the distances in each image - ‘min’ minimum of the distances in each image - ‘mean’ average of the distances in each image - ‘sum’ total of the distances in each image

    Alternatively a callable function can be used, this function must have form f(t, *args) where t is a rank 3 tensor of distances with shape (img, vectors, clusters) and return a rank 2 tensor of distances with shape (vectors, clusters).

  • combo_metric_args (tuple, optional) – arguments to passed into combo_metric function if given
  • dist_metric (function, optional) – Passed into SLIC objects. The method of calculating distance between vectors and cluster centers. This must have form f(vecs, clusts, *args) where vecs and clusts are rank 2 tensors with samples on the first axis. This should return a rank 2 tensor of distances with shape (clusters, vectors) such that taking the argmin along dim=1 gives an array of closest cluster centroids for each vector
  • dist_metric_args (tuple, optional) – Passed into SLIC objects. arguments to passed into dist_metric function if given
get_segmentation()

Returns the current segmentation mask as a numpy array

iterate(n_iter)

loop for n_iter iterations with progress bar

plot(objs=[], option='default', axs=[None], path='')

Calls obj.plot() on each of the SLIC objects specified with option given

Parameters:
  • obj_indexs (list of ints, optional) – which of the SLIC objects to call plot on. If left empty this will plot all SLIC objects.
  • option (string, optional) – the plot option to be passed to obj.plot, refer to the docstrings there for available options
  • axs (tuple of matplotlib axis, optional) – the axis to be plotted on, if not given a subplot for each of the SLIC objs is used.
  • path (string, optional) – the path to save the figure on if wanted