imsegm.superpixels module

Framework for superpixels
  • wrapper over skimage.SLIC

  • other related functions

SEE: * http://scikit-image.org/docs/dev/auto_examples/plot_segmentations.html

Copyright (C) 2014-2018 Jiri Borovec <jiri.borovec@fel.cvut.cz>

imsegm.superpixels.get_neighboring_segments(edges)[source]

get the indexes of neighboring superpixels for each superpixel the input is list edges of all neighboring segments

Parameters

edges (list(tuple(int,int))) –

Return list(list(int))

>>> get_neighboring_segments([[0, 1], [1, 2], [1, 3], [2, 3]])
[[1], [0, 2, 3], [1, 3], [1, 2]]
imsegm.superpixels.get_segment_diffs_2d_conn4(grid)[source]

wrapper for getting 4-connected in 2D image plane

Parameters

grid (ndarray) – segmentation

Return list(tuple(int,int))

imsegm.superpixels.get_segment_diffs_3d_conn6(grid)[source]

wrapper for getting 6-connected in 3D image plane

Parameters

grid (ndarray) – segmentation

Return list(tuple(int,int,int))

imsegm.superpixels.make_graph_segm_connect_grid2d_conn4(grid)[source]

construct graph of connected components

Parameters

grid (ndarray) – segmentation

Return list(int), list(tuple(int,int))

>>> grid = np.array([[0] * 5 + [1] * 5, [2] * 5 + [3] * 5])
>>> v, edges = make_graph_segm_connect_grid2d_conn4(grid)
>>> v
array([0, 1, 2, 3])
>>> edges
[[0, 1], [0, 2], [1, 3], [2, 3]]
imsegm.superpixels.make_graph_segm_connect_grid3d_conn6(grid)[source]

construct graph of connected components

Parameters

grid (ndarray) – segmentation

Return list(int), list(tuple(int,int))

>>> grid_2d = np.array([[0] * 5 + [1] * 5, [2] * 5 + [3] * 5])
>>> grid = np.array([grid_2d, grid_2d + 4])
>>> v, edges = make_graph_segm_connect_grid3d_conn6(grid)
>>> v
array([0, 1, 2, 3, 4, 5, 6, 7])
>>> edges  
[[0, 1], [0, 2], [1, 3], [2, 3], [0, 4], [1, 5], [4, 5], [2, 6], [4, 6],
[3, 7], [5, 7], [6, 7]]
imsegm.superpixels.make_graph_segment_connect_edges(vertices, all_edges)[source]

make graph of connencted components SEE: http://peekaboo-vision.blogspot.cz/2011/08/region-connectivity-graphs-in-python.html

Parameters
  • vertices (ndarray) –

  • all_edges (ndarray) –

Return tuple(ndarray,ndarray)

imsegm.superpixels.segment_slic_img2d(img, sp_size=50, relative_compact=0.1, slico=False)[source]

segmentation by SLIC superpixels using original SLIC implementation

Parameters
  • img (ndarray) – input color image

  • sp_size (int) – superpixel initial size

  • relative_compact (float) – relative regularisation in range (0, 1) where 0 is for free form and 1 for nearly rectangular superpixels

  • slico (bool) – whether use parameter free version ASLIC/SLICO

Return ndarray

segmentation

>>> np.random.seed(0)
>>> img = np.random.random((100, 150, 3))
>>> slic = segment_slic_img2d(img, 20, 0.2)
>>> slic.shape
(100, 150)
>>> img = np.random.random((150, 100))
>>> slic = segment_slic_img2d(img, 20, 0.2)
>>> slic.shape
(150, 100)
imsegm.superpixels.segment_slic_img3d_gray(im, sp_size=50, relative_compact=0.1, space=(1, 1, 1))[source]

segmentation by SLIC superpixels using originla SLIC implementation

Parameters
  • im (ndarray) – input 3D grascale image

  • sp_size (int) – superpixel initial size

  • relative_compact (float) – relative regularisation in range (0, 1) where 0 is for free form and 1 for nearly rectangular superpixels

  • space (tuple(int,int,int)) – spacing in 3d image may not be equal

Return ndarray

>>> np.random.seed(0)
>>> img = np.random.random((100, 100, 10))
>>> slic = segment_slic_img3d_gray(img, 20, 0.2, (1, 1, 5))
>>> slic.shape
(100, 100, 10)
imsegm.superpixels.superpixel_centers(segments)[source]

estimate centers of each superpixel

Parameters

segments (ndarray) – segmentation np.array<height, width>

Return list(tuple(float,float))

>>> segm = np.array([[0] * 6 + [1] * 5, [0] * 6 + [2] * 5])
>>> superpixel_centers(segm)
[(0.5, 2.5), (0.0, 8.0), (1.0, 8.0)]
>>> superpixel_centers(np.array([segm, segm, segm]))
[[1.0, 0.5, 2.5], [1.0, 0.0, 8.0], [1.0, 1.0, 8.0]]
imsegm.superpixels.IMAGE_SPACING = (1, 1, 1)[source]

spacing among neighboring pixels in axies X, Y, Z