imsegm.descriptors module

Framework for feature extraction
  • color and gray 3D images

  • color and texture features

  • Ray features

  • label histogram

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

imsegm.descriptors._check_color_image(image)[source]

verify proper image

Parameters

image (ndarray) –

Return bool

>>> _check_color_image(np.zeros((200, 250, 1)))  
Traceback (most recent call last):
...
imsegm.utilities.ImageDimensionError: image is not RGB with dims (200, 250, 1)
imsegm.descriptors._check_color_image_segm(image, segm)[source]

verify image - segmentation compatibility

Parameters
  • image (ndarray) – image

  • segm (ndarray) – segmentation

Return bool

>>> _check_color_image_segm(np.zeros((125, 150, 3)), np.zeros((150, 125)))  
Traceback (most recent call last):
...
imsegm.utilities.ImageDimensionError: ndarrays - image and segmentation do not match (125, 150, 3) vs (150, 125)
imsegm.descriptors._check_gray_image_segm(image, segm)[source]

verify image - segmentation compatibility

Parameters
  • image (ndarray) – image

  • segm (ndarray) – segmentation

Return bool

>>> _check_gray_image_segm(np.zeros((125, 150)), np.zeros((150, 125)))  
Traceback (most recent call last):
...
imsegm.utilities.ImageDimensionError: ndarrays - image and segmentation do not match (125, 150) vs (150, 125)
imsegm.descriptors._check_unrecognised_feature_group(feature_flags)[source]

search for not defined flags

Parameters

feature_flags (dict) – input

Return list(str)

unrecognised

>>> _check_unrecognised_feature_group({'color': [], 'texture': []})
['texture']
imsegm.descriptors._check_unrecognised_feature_names(feature_flags)[source]

search for not defined flags

Parameters

feature_flags (list(str)) – input

Return list(str)

unrecognised

>>> _check_unrecognised_feature_names(['mean', 'average'])
['average']
imsegm.descriptors.adjust_bounding_box_crop(image_size, bbox_size, position)[source]

adjust the bounding box according image sizes and position

Parameters
Return (), (), (), ()

im_begin, im_end, bb_begin, bb_end

>>> adjust_bounding_box_crop((50, 50), (7, 7), (20, 20))
((17, 17), (24, 24), (0, 0), (7, 7))
>>> adjust_bounding_box_crop((50, 50), (15, 15), (20, 45))
((13, 38), (28, 50), (0, 0), (15, 12))
>>> adjust_bounding_box_crop((50, 50), (15, 15), (5, 5))
((0, 0), (13, 13), (2, 2), (15, 15))
>>> adjust_bounding_box_crop((50, 50), (80, 80), (20, 20))
((0, 0), (50, 50), (20, 20), (70, 70))
imsegm.descriptors.compute_image2d_color_statistic(image, segm, feature_flags=('mean', 'std', 'energy', 'median', 'meanGrad'), color_name='color')[source]

compute complete descriptors / statistic on color (2D) images

Parameters
  • image (ndarray) –

  • segm (ndarray) – segmentation

  • feature_flags (list(str)) –

  • color_name (str) – channel name

Return tuple(ndarray,list(str))

np.ndarray<nb_samples, nb_features>

>>> image = np.zeros((2, 10, 3))
>>> image[:, 2:6, 0] = 1
>>> image[:, 3:7, 1] = 3
>>> image[:, 4:9, 2] = 2
>>> segm = np.array([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
...                  [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]])
>>> features, names = compute_image2d_color_statistic(image, segm)
>>> names  
['color-ch1_mean', 'color-ch2_mean', 'color-ch3_mean',
 'color-ch1_std', 'color-ch2_std', 'color-ch3_std',
 'color-ch1_energy', 'color-ch2_energy', 'color-ch3_energy',
 'color-ch1_median', 'color-ch2_median', 'color-ch3_median',
 'color-ch1_meanGrad', 'color-ch2_meanGrad', 'color-ch3_meanGrad']
>>> features.shape
(2, 15)
>>> np.round(features, 1).tolist()  
[[0.6, 1.2, 0.4, 0.5, 1.5, 0.8, 0.6, 3.6, 0.8, 1.0, 0.0, 0.0, 0.2, 0.6, 0.4],
 [0.2, 1.2, 1.6, 0.4, 1.5, 0.8, 0.2, 3.6, 3.2, 0.0, 0.0, 2.0, -0.2, -0.6, -0.6]]
imsegm.descriptors.compute_image3d_gray_statistic(image, segm, feature_flags=('mean', 'std', 'energy', 'median', 'meanGrad'), ch_name='gray')[source]

compute complete descriptors / statistic on gray (3D) images

Parameters
  • image (ndarray) –

  • segm (ndarray) – segmentation

  • feature_flags (list(str)) –

  • ch_name (str) – channel name

Return tuple(ndarray,list(str))

np.ndarray<nb_samples, nb_features>

>>> image = np.zeros((2, 3, 8))
>>> image[0, :, 2:6] = 1
>>> image[1, :, 3:7] = 3
>>> segm = np.array([[[0, 0, 0, 0, 1, 1, 1, 1]] * 3,
...                  [[2, 2, 2, 2, 5, 5, 5, 5]] * 3])
>>> segm.shape
(2, 3, 8)
>>> features, names = compute_image3d_gray_statistic(image, segm)
>>> features.shape
(6, 5)
>>> np.round(features, 3)  
array([[ 0.5  ,  0.5  ,  0.5  ,  0.5  ,  0.25 ],
       [ 0.5  ,  0.5  ,  0.5  ,  0.5  , -0.25 ],
       [ 0.75 ,  1.299,  2.25 ,  0.   ,  0.75 ],
       [ 0.   ,  0.   ,  0.   ,  0.   ,  0.   ],
       [ 0.   ,  0.   ,  0.   ,  0.   ,  0.   ],
       [ 2.25 ,  1.299,  6.75 ,  3.   , -1.125]])
>>> names  
['gray_mean',
 'gray_std',
 'gray_energy',
 'gray_median',
 'gray_meanGrad']
imsegm.descriptors.compute_img_filter_response2d(img, filter_battery)[source]

compute image filter response in 2D

Parameters
Return list(list(float))

imsegm.descriptors.compute_img_filter_response3d(img, filter_battery)[source]

compute image filter response in 3D

Parameters
  • img (ndarray) –

  • filter_battery (ndarray) –

Returns

imsegm.descriptors.compute_label_hist_proba(segm, position, struc_elem)[source]

compute histogram of labels for set of centric annulus expecting that each label has own layer

Parameters
  • segm (ndarray) – np.array<height, width>

  • position (tuple(float,float)) –

  • struc_elem (ndarray) – np.array<height, width>

Return list(float)

>>> seg = np.zeros((50, 50, 2), dtype=float)
>>> seg[15:35, 20:40, 1] = 1
>>> seg[:, :, 0] = 1 - seg[:, :, 1]
>>> compute_label_hist_proba(seg, (15, 20), np.ones((12, 13), dtype=int))
(array([ 114.,   42.]), 156)
imsegm.descriptors.compute_label_hist_segm(segm, position, struc_elem, nb_labels)[source]

compute histogram of labels for set of centric annulus

Parameters
  • segm (ndarray) – np.array<height, width>

  • position (tuple(float,float)) – position in the segmentation

  • struc_elem (ndarray) – np.array<height, width>

  • nb_labels (int) – total number of labels in the segmentation

Return list(float)

>>> segm = np.zeros((10, 10), dtype=int)
>>> segm[1:9, 2:8] = 1
>>> segm[3:7, 4:6] = 2
>>> segm
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 2, 2, 1, 1, 0, 0],
       [0, 0, 1, 1, 2, 2, 1, 1, 0, 0],
       [0, 0, 1, 1, 2, 2, 1, 1, 0, 0],
       [0, 0, 1, 1, 2, 2, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
>>> compute_label_hist_segm(segm, [6, 6], np.ones((3, 3)), 3)
(array([ 0.,  7.,  2.]), 9.0)
>>> compute_label_hist_segm(segm, [4, 4], np.ones((5, 5)), 3)
(array([  0.,  17.,   8.]), 25.0)
imsegm.descriptors.compute_label_histograms_positions(segm, positions, diameters=(10, 20, 30, 40, 50), nb_labels=None)[source]

compute the histogram features doe consecutive growing diameter of inter circle neighbouring around given points in the segmentation

Parameters
  • segm (ndarray) – np.array<height, width>

  • positions (list(tuple(int,int))) – list of positions

  • diameters (list(int)) – circular diameters

  • nb_labels (int) –

Return tuple(ndarray,list(str))

ndarray<nb_samples, nb_features>, names

>>> segm = np.zeros((10, 10), dtype=int)
>>> segm[1:9, 2:8] = 1
>>> segm[3:7, 4:6] = 2
>>> points = [[3, 3], [4, 4], [2, 7], [6, 6]]
>>> hists, names = compute_label_histograms_positions(segm, points, [1, 2, 4])
>>> names  
['hist-d_1-lb_0', 'hist-d_1-lb_1', 'hist-d_1-lb_2',      'hist-d_2-lb_0', 'hist-d_2-lb_1', 'hist-d_2-lb_2',      'hist-d_4-lb_0', 'hist-d_4-lb_1', 'hist-d_4-lb_2']
>>> hists.shape
(4, 9)
>>> np.round(hists, 2)
array([[ 0.  ,  0.8 ,  0.2 ,  0.12,  0.62,  0.25,  0.44,  0.41,  0.15],
       [ 0.  ,  0.2 ,  0.8 ,  0.  ,  0.62,  0.38,  0.22,  0.75,  0.03],
       [ 0.2 ,  0.8 ,  0.  ,  0.5 ,  0.5 ,  0.  ,  0.46,  0.33,  0.21],
       [ 0.  ,  0.8 ,  0.2 ,  0.12,  0.62,  0.25,  0.44,  0.41,  0.15]])
>>> segm = np.zeros((10, 10, 2), dtype=int)
>>> segm[3:7, 4:6, 1] = 1
>>> segm[:, :, 0] = 1 - segm[:, :, 0]
>>> points = [[3, 3], [4, 4], [2, 7], [6, 6]]
>>> hists, names = compute_label_histograms_positions(segm, points, [1, 2, 4])
>>> np.round(hists, 2)
array([[ 1.  ,  0.2 ,  1.  ,  0.25,  1.  ,  0.15],
       [ 1.  ,  0.8 ,  1.  ,  0.38,  1.  ,  0.03],
       [ 1.  ,  0.  ,  1.  ,  0.  ,  1.  ,  0.21],
       [ 1.  ,  0.2 ,  1.  ,  0.25,  1.  ,  0.15]])
imsegm.descriptors.compute_ray_features_positions(segm, list_positions, angle_step=5.0, border_labels=None, segm_open=None, smooth_ray=None, shifting=True, edge='up')[source]

compute ray features fo multiple points in the segmentation with given boundary labels and step angle

Parameters
  • segm (ndarray) – np.array<height, width>

  • list_positions (list(tuple(int,int))) –

  • angle_step (float) –

  • border_labels (list(int)) – all labels to be set as boundaries

  • segm_open (int) –

  • smooth_ray (float) –

  • shifting (bool) –

  • edge (str) – type of edge up/down

Returns

Note

for more examples, see unittests

>>> from imsegm.utilities.drawing import _draw_disk
>>> np.random.seed(0)
>>> seg = np.zeros((100, 100), dtype=int)
>>> x, y = _draw_disk(45, 55, 30, shape=seg.shape)
>>> seg[x, y] = 1
>>> x, y = _draw_disk(55, 45, 10, shape=seg.shape)
>>> seg[x, y] = 2
>>> points = [(50, 50), (60, 40), (44, 55)]
>>> ray_dist, shift, _ = compute_ray_features_positions(seg, points, 20)
>>> shift  
[314.3..., 314.7..., 90.0...]
>>> ray_dist.astype(int).tolist()  
[[37, 37, 35, 32, 30, 27, 25, 24, 23, 23, 24, 25, 26, 30, 31, 33, 35, 38],
 [50, 47, 41, 31, 23, 17, 13, 10, 9, 9, 9, 11, 14, 19, 27, 37, 45, 50],
 [31, 31, 31, 30, 30, 29, 30, 30, 29, 29, 30, 30, 29, 30, 30, 31, 31, 31]]
>>> noise_pos = np.random.randint(10, 80, (2, 300))
>>> seg[noise_pos[0], noise_pos[1]] = 0  # add random noise
>>> ray_dist, shift, names = compute_ray_features_positions(
...     seg, points, 45, segm_open=10)
>>> names  
['ray-lb_0-agl_0', 'ray-lb_0-agl_45', 'ray-lb_0-agl_90',
 'ray-lb_0-agl_135', 'ray-lb_0-agl_180', 'ray-lb_0-agl_225',
 'ray-lb_0-agl_270', 'ray-lb_0-agl_315']
>>> shift  
[315.0..., 315.0..., 90.0...]
>>> ray_dist.astype(int)
array([[38, 35, 29, 25, 24, 25, 29, 35],
       [52, 41, 21, 11,  9, 11, 21, 41],
       [31, 31, 30, 29, 29, 29, 30, 31]])
imsegm.descriptors.compute_ray_features_segm_2d(seg_binary, position, angle_step=5.0, smooth_coef=0, edge='up')[source]

compute ray features vector , shift them to be starting from larges and smooth_coef them by gauss filter (from given point the close distance to boundary)

Parameters
  • seg_binary (ndarray) – np.array<height, width>

  • position (tuple(int,int)) – integer position in the segmentation

  • angle_step (float) – angular step for ray features

  • edge (str) – pointing to the up of down edge of an boundary

  • smooth_coef (int) – smoothing the final ray features

Return list(float)

ray distances

Note

for more examples, see unittests

>>> seg_empty = np.zeros((100, 150), dtype=bool)
>>> compute_ray_features_segm_2d(seg_empty, (50, 75), 90)  
array([-1., -1., -1., -1.]...)
>>> from imsegm.utilities.drawing import _draw_disk
>>> seg = np.ones((100, 150), dtype=bool)
>>> x, y = _draw_disk(50, 75, 40, shape=seg.shape)
>>> seg[x, y] = False
>>> np.round(compute_ray_features_segm_2d(seg, (50, 75), 45))  
array([ 40.,  41.,  40.,  41.,  40.,  41.,  40.,  41.]...)
>>> np.round(compute_ray_features_segm_2d(seg, (60, 40), 30, smooth_coef=1)).tolist()
[66.0, 52.0, 32.0, 16.0, 8.0, 5.0, 5.0, 8.0, 16.0, 33.0, 53.0, 67.0]
>>> ray_fts = compute_ray_features_segm_2d(seg, (40, 60), 20)
>>> np.round(ray_fts).tolist()  
[54.0, 57.0, 59.0, 55.0, 51.0, 44.0, 38.0, 31.0, 27.0, 24.0, 22.0, 22.0,
 23.0, 26.0, 29.0, 35.0, 42.0, 49.0]
imsegm.descriptors.compute_ray_features_segm_2d_vectors(seg_binary, position, angle_step=5.0, smooth_coef=0, edge='up')[source]

USES WHOLE IMAGE ROTATION SO IT IS VERY SLOW compute ray features vector , shift them to be startig from larges and smooth_coef them by gauss filter (from fiven point the close distance to boundary)

Parameters
  • edge (str) – pointing to the up of down edge o

  • smooth_coef (int) –

  • seg_binary (ndarray) – np.array<height, width>

  • position (tuple(int,int)) –

  • angle_step (float) –

Return list(float)

Note

for more examples, see unittests

>>> from imsegm.utilities.drawing import _draw_disk
>>> seg = np.ones((100, 100), dtype=bool)
>>> x, y = _draw_disk(45, 55, 30, shape=seg.shape)
>>> seg[x, y] = False
>>> compute_ray_features_segm_2d_vectors(seg, (50, 50), 45)
array([35, 29, 25, 23, 24, 29, 34, 36])
>>> compute_ray_features_segm_2d_vectors(seg, (60, 40), 30, smooth_coef=1)
array([35, 27, 18, 12, 10,  9, 12, 18, 27, 37, 45, 49])
>>> compute_ray_features_segm_2d_vectors(seg, (40, 60), 20).tolist()
[25, 27, 29, 32, 34, 35, 37, 36, 36, 34, 32, 29, 27, 25, 24, 23, 24, 24]
imsegm.descriptors.compute_selected_features_color2d(img, segments, feature_flags={'color': ('mean', 'std', 'energy', 'median', 'meanGrad'), 'tLM': ('mean', 'std', 'energy', 'median', 'meanGrad')})[source]

compute selected features color image 2D

Parameters
  • img (ndarray) – image

  • segments (ndarray) – segmentation

  • feature_flags (dict(list(str))) – dictionary of feature flags

Return tuple(np.ndarray<nb_samples, nb_features>, list(str))

>>> image = np.zeros((2, 10, 3))
>>> image[:, 2:6, 0] = 1
>>> image[:, 3:7, 1] = 3
>>> image[:, 4:9, 2] = 2
>>> segm = np.array([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
...                  [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]])
>>> features, names = compute_selected_features_color2d(image, segm,
...                                   {'color': ('mean', 'std', 'median')})
>>> np.round(features, 3)
array([[ 0.6 ,  1.2 ,  0.4 ,  0.49,  1.47,  0.8 ,  1.  ,  0.  ,  0.  ],
       [ 0.2 ,  1.2 ,  1.6 ,  0.4 ,  1.47,  0.8 ,  0.  ,  0.  ,  2.  ]])
>>> features, names = compute_selected_features_color2d(image, segm,
...                                   {'color_hsv': ('mean', 'std')})
>>> np.round(features, 3)
array([[ 0.139,  0.533,  1.4  ,  0.176,  0.452,  1.356],
       [ 0.439,  0.733,  2.   ,  0.244,  0.389,  1.095]])
>>> _ = compute_selected_features_color2d(image, segm,
...                                   {'tLM': ('mean', 'std', 'energy')})
>>> features, names = compute_selected_features_color2d(image, segm,
...                                   {'tLM_short': ('mean', 'energy')})
>>> features.shape
(2, 90)
>>> features, names = compute_selected_features_color2d(image, segm)
>>> features.shape
(2, 315)
imsegm.descriptors.compute_selected_features_gray2d(img, segments, features_flags={'color': ('mean', 'std', 'energy', 'median', 'meanGrad'), 'tLM': ('mean', 'std', 'energy', 'median', 'meanGrad')})[source]

compute selected features for gray image 2D

Parameters
  • img (ndarray) – image

  • segments (ndarray) – segmentation

  • feature_flags (dict(list(str))) – dictionary of feature flags

Return tuple(np.ndarray<nb_samples, nb_features>, list(str))

>>> image = np.zeros((2, 10))
>>> image[0, 2:6] = 1
>>> image[1, 3:7] = 3
>>> segm = np.array([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
...                  [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]])
>>> features, names = compute_selected_features_gray2d(
...     image, segm, {'color': ('mean', 'std', 'median')})
>>> np.round(features, 3)
array([[ 0.9  ,  1.136,  0.5  ],
       [ 0.7  ,  1.187,  0.   ]])
>>> _ = compute_selected_features_gray2d(
...     image, segm, {'tLM': ('mean', 'std', 'median')})
>>> features, names = compute_selected_features_gray2d(
...     image, segm, {'tLM_short': ('mean', 'std', 'energy')})
>>> features.shape
(2, 45)
>>> features, names = compute_selected_features_gray2d(image, segm)
>>> features.shape
(2, 105)
imsegm.descriptors.compute_selected_features_gray3d(img, segments, feature_flags={'color': ('mean', 'std', 'energy')})[source]

compute selected features on gray 3D image

Parameters
  • img (ndarray) – image

  • segments (ndarray) – segmentation

  • feature_flags (dict(list(str))) – dictionary of feature flags

Return tuple(np.ndarray<nb_samples, nb_features>, list(str))

>>> np.random.seed(0)
>>> img = np.random.random((2, 10, 15))
>>> slic = np.zeros((2, 10, 15), dtype=int)
>>> slic[:, :, :7] += 1
>>> slic[1, :, :] += 2
>>> fts, names = compute_selected_features_gray3d(
...     img, slic, {'color': ('mean', 'std', 'median')})
>>> fts.shape
(4, 3)
>>> names  
['gray_mean', 'gray_std', 'gray_median']
>>> _ = compute_selected_features_gray3d(
...     img, slic, {'tLM': ('median', 'std', 'energy')})
>>> fts, names = compute_selected_features_gray3d(
...     img, slic, {'tLM_short': ('mean', 'std', 'energy')})
>>> fts.shape
(4, 45)
>>> names  
['tLM_sigma1.4-edge_mean', ..., 'tLM_sigma4.0-GaussLap2_energy']
imsegm.descriptors.compute_selected_features_img2d(image, segm, features_flags={'color': ('mean', 'std', 'energy')})[source]

compute features

Parameters
  • img (ndarray) – image

  • segments (ndarray) – segmentation

  • feature_flags (dict(list(str))) – dictionary of feature flags

Returns

imsegm.descriptors.compute_texture_desc_lm_img2d_clr(img, seg, feature_flags, bank_type='normal')[source]

compute texture descriptors via Lewen-Malik filter response

Parameters
  • img (ndarray) – image

  • seg (ndarray) – segmentation

  • feature_flags (list(str)) –

  • bank_type (str) – define used LM filter bank [‘short’, ‘normal’]

Return tuple(np.ndarray<nb_samples, nb_features>, list(str))

>>> h, w, step = 30, 20, 5
>>> np.random.seed(0)
>>> seg = np.zeros((h, w), dtype=int)
>>> for i in range(int(np.ceil(h / float(step)))):
...     for j in range(int(np.ceil(w / float(step)))):
...         val = i * (w / step) + j
...         i_step, j_step = int(i * step), int(j * step)
...         seg[i_step:int(i_step + step), j_step:int(j_step + step)] = val
>>> img = np.random.random((h, w, 3))
>>> features, names = compute_texture_desc_lm_img2d_clr(img, seg,
...                          ['mean', 'std', 'median'], bank_type='short')
>>> features.shape
(24, 135)
>>> names  
['tLM_sigma1.4-edge-ch1_mean', ..., 'tLM_sigma1.4-edge-ch3_mean',
 'tLM_sigma1.4-edge-ch1_std', ..., 'tLM_sigma1.4-edge-ch3_std',
 'tLM_sigma1.4-edge-ch1_median', ..., 'tLM_sigma1.4-edge-ch3_median',
 'tLM_sigma1.4-bar-ch1_mean', ..., 'tLM_sigma1.4-bar-ch3_median',
 'tLM_sigma1.4-Gauss-ch1_mean', ..., 'tLM_sigma1.4-Gauss-ch3_median',
 'tLM_sigma1.4-GaussLap-ch1_mean', ..., 'tLM_sigma1.4-GaussLap-ch3_median',
 'tLM_sigma1.4-GaussLap2-ch1_mean', ..., 'tLM_sigma1.4-GaussLap2-ch3_median',
 'tLM_sigma2.0-edge-ch1_mean', ..., 'tLM_sigma2.0-GaussLap2-ch3_median',
 'tLM_sigma4.0-edge-ch1_mean', ..., 'tLM_sigma4.0-GaussLap2-ch3_median']
imsegm.descriptors.compute_texture_desc_lm_img3d_val(img, seg, feature_flags, bank_type='normal')[source]

compute texture descriptors as mean / std / … on Lewen-Malik filter bank response

Parameters
  • img (list(list(list(float)))) – image

  • seg (list(list(list(int)))) – segmentation

  • feature_flags (list(str)) – list of feature flags

  • bank_type (str) – define used LM filter bank [‘short’, ‘normal’]

Return tuple(ndarray,list(str))

np.ndarray<nb_samples, nb_features>, names

imsegm.descriptors.create_filter_bank_lm_2d(radius=16, sigmas=(1.4142135623730951, 2, 2.8284271247461903, 4), nb_orient=8)[source]

create filter bank with rotation, Gaussian, Laplace-Gaussian, …

Parameters
  • radius

  • sigmas

  • nb_orient

Return np.ndarray<nb_samples, nb_features>, list(str)

>>> filters, names = create_filter_bank_lm_2d(6, SHORT_FILTERS_SIGMAS, 2)
>>> [f.shape for f in filters]  
[(2, 13, 13), (2, 13, 13), (1, 13, 13), (1, 13, 13), (1, 13, 13),
 (2, 13, 13), (2, 13, 13), (1, 13, 13), (1, 13, 13), (1, 13, 13),
 (2, 13, 13), (2, 13, 13), (1, 13, 13), (1, 13, 13), (1, 13, 13)]
>>> names  
['sigma1.4-edge', 'sigma1.4-bar',
 'sigma1.4-Gauss', 'sigma1.4-GaussLap', 'sigma1.4-GaussLap2',
 'sigma2.0-edge', 'sigma2.0-bar',
 'sigma2.0-Gauss', 'sigma2.0-GaussLap', 'sigma2.0-GaussLap2',
 'sigma4.0-edge', 'sigma4.0-bar',
 'sigma4.0-Gauss', 'sigma4.0-GaussLap', 'sigma4.0-GaussLap2']
imsegm.descriptors.cython_img2d_color_energy(img, seg)[source]

wrapper for fast implementation of colour features

Parameters
  • img (ndarray) – input RGB image

  • seg (ndarray) – segmentation og the image

Returns

np.array<nb_lbs, 3> matrix features per segment

>>> image = np.zeros((2, 10, 3))
>>> image[:, 2:6, 0] = 1
>>> image[:, 3:7, 1] = 3
>>> image[:, 4:9, 2] = 2
>>> segm = np.array([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
...                  [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]])
>>> cython_img2d_color_energy(image, segm)
array([[ 0.6,  3.6,  0.8],
       [ 0.2,  3.6,  3.2]])
imsegm.descriptors.cython_img2d_color_mean(img, seg)[source]

wrapper for fast implementation of colour features

Parameters
  • img (ndarray) – input RGB image

  • seg (ndarray) – segmentation og the image

Returns

np.array<nb_lbs, 3> matrix features per segment

>>> image = np.zeros((2, 10, 3))
>>> image[:, 2:6, 0] = 1
>>> image[:, 3:7, 1] = 3
>>> image[:, 4:9, 2] = 2
>>> segm = np.array([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
...                  [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]])
>>> cython_img2d_color_mean(image, segm)
array([[ 0.6,  1.2,  0.4],
       [ 0.2,  1.2,  1.6]])
imsegm.descriptors.cython_img2d_color_std(img, seg, means=None)[source]

wrapper for fast implementation of colour features

Parameters
  • img (ndarray) – input RGB image

  • seg (ndarray) – segmentation og the image

  • means (ndarray) – precomputed feature means

Returns

np.array<nb_lbs, 3> matrix features per segment

>>> image = np.zeros((2, 10, 3))
>>> image[:, 2:6, 0] = 1
>>> image[:, 3:7, 1] = 3
>>> image[:, 4:9, 2] = 2
>>> segm = np.array([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
...                  [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]])
>>> cython_img2d_color_std(image, segm)
array([[ 0.48989794,  1.46969383,  0.80000003],
       [ 0.40000001,  1.46969383,  0.80000001]])
imsegm.descriptors.cython_img3d_gray_energy(img, seg)[source]

wrapper for fast implementation of colour features

WARNING: the Z dimension is parallel and without sync, multiple equal labels across Z dim may lead to not mistakes in summing

Parameters
  • img (ndarray) – input RGB image

  • seg (ndarray) – segmentation og the image

Returns

np.array<nb_lbs, 1> vector of mean colour per segment

>>> image = np.zeros((2, 3, 8))
>>> image[0, :, 2:6] = 1
>>> image[1, :, 3:7] = 3
>>> segm = np.array([[[0, 0, 0, 0, 1, 1, 1, 1]] * 3,
...                  [[2, 2, 2, 2, 3, 3, 3, 3]] * 3])
>>> cython_img3d_gray_energy(image, segm)
array([ 0.5 ,  0.5 ,  2.25,  6.75])
imsegm.descriptors.cython_img3d_gray_mean(img, seg)[source]

wrapper for fast implementation of colour features

WARNING: the Z dimension is parallel and without sync, multiple equal labels across Z dim may lead to not mistakes in summing

Parameters
  • img (ndarray) – input RGB image

  • seg (ndarray) – segmentation og the image

Returns

np.array<nb_lbs, 1> vector of mean colour per segment

>>> image = np.zeros((2, 3, 8))
>>> image[0, :, 2:6] = 1
>>> image[1, :, 3:7] = 3
>>> segm = np.array([[[0, 0, 0, 0, 1, 1, 1, 1]] * 3,
...                  [[2, 2, 2, 2, 3, 3, 3, 3]] * 3])
>>> segm.shape
(2, 3, 8)
>>> cython_img3d_gray_mean(image, segm)
array([ 0.5 ,  0.5 ,  0.75,  2.25])
imsegm.descriptors.cython_img3d_gray_std(img, seg, mean=None)[source]

wrapper for fast implementation of colour features

WARNING: the Z dimension is parallel and without sync, multiple equal labels across Z dim may lead to not mistakes in summing

Parameters
  • img (ndarray) – input RGB image

  • seg (ndarray) – segmentation og the image

  • mean (ndarray) – precomputed feature means

Returns

np.array<nb_lbs, 1> vector of mean colour per segment

>>> image = np.zeros((2, 3, 8))
>>> image[0, :, 2:6] = 1
>>> image[1, :, 3:7] = 3
>>> segm = np.array([[[0, 0, 0, 0, 1, 1, 1, 1]] * 3,
...                  [[2, 2, 2, 2, 3, 3, 3, 3]] * 3])
>>> cython_img3d_gray_std(image, segm)
array([ 0.5       ,  0.5       ,  1.29903811,  1.29903811])
imsegm.descriptors.cython_label_hist_seg2d(segm_select, struc_elem, nb_labels)[source]

compute histogram of labels for set of centric annulus

Parameters
  • segm_select (ndarray) – np.array<height, width>

  • struc_elem (ndarray) – np.array<height, width>

  • nb_labels (int) – total number of labels in the segmentation

Return list(float)

Note

output of this function should be equal to

for lb in range(nb_labels):
    hist[lb] = np.sum(np.logical_and(segm_select == lb, struc_elem == 1))
>>> segm = np.zeros((10, 10), dtype=int)
>>> segm[1:9, 2:8] = 1
>>> segm[3:7, 4:6] = 2
>>> cython_label_hist_seg2d(segm[2:5, 4:7], np.ones((3, 3)), 3)
array([ 0.,  5.,  4.])
>>> cython_label_hist_seg2d(segm[1:6, 3:8], np.ones((5, 5)), 3)
array([  0.,  19.,   6.])
imsegm.descriptors.cython_ray_features_seg2d(seg_binary, position, angle_step=5.0, edge='up')[source]

computing the Ray features from a segmentation and given position

Parameters
  • seg_binary (ndarray) – np.array<height, width>

  • position (tuple(int,int)) – integer position in the segmentation

  • angle_step (float) – angular step for ray features

  • edge (str) – pointing to the up of down edge of an boundary

Return list(float)

ray distances

>>> seg_empty = np.zeros((100, 150), dtype=bool)
>>> cython_ray_features_seg2d(seg_empty, (50, 75), 90)  
array([-1., -1., -1., -1.]...)
>>> from imsegm.utilities.drawing import _draw_disk
>>> seg = np.ones((100, 150), dtype=bool)
>>> x, y = _draw_disk(50, 75, 40, shape=seg.shape)
>>> seg[x, y] = False
>>> cython_ray_features_seg2d(seg, (50, 75), 45).astype(int)  
array([40, 41, 40, 41, 40, 41, 40, 41]...)
>>> cython_ray_features_seg2d(seg, (60, 40), 30).astype(int).tolist()
[74, 55, 28, 10, 5, 4, 4, 5, 9, 30, 57, 75]
>>> cython_ray_features_seg2d(seg, (40, 60), 20).astype(int).tolist()
[54, 57, 58, 55, 50, 43, 38, 31, 26, 24, 22, 22, 23, 26, 29, 34, 41, 48]
imsegm.descriptors.image_subtract_gauss_smooth(img, sigma)[source]

smoothing by fist dimension assuming the in dim 0. image is independent

Parameters
  • img (ndarray) –

  • sigma

Returns

imsegm.descriptors.interpolate_ray_dist(ray_dists, order='spline')[source]

interpolate ray distances

Parameters
  • ray_dists (list(float)) –

  • order (str|int) – degree of interpolation

Return list(float)

>>> interpolate_ray_dist([-1] * 5)
array([-1, -1, -1, -1, -1])
>>> vals = np.sin(np.linspace(0, 2 * np.pi, 20)) * 10
>>> np.round(vals).astype(int).tolist()
[0, 3, 6, 8, 10, 10, 9, 7, 5, 2, -2, -5, -7, -9, -10, -10, -8, -6, -3, 0]
>>> vals[3:7] = -1
>>> vals[16:] = -1
>>> vals_interp = interpolate_ray_dist(vals, order=3)
>>> np.round(vals_interp).astype(int).tolist()  
[0, 3, 6, 9, 10, 10, 8, 7, 5, 2, -2, -5, -7, -9, -10, -10, -10, -8, -4, 1]
>>> vals_interp = interpolate_ray_dist(vals, order='spline')
>>> np.round(vals_interp).astype(int).tolist()
[0, 3, 6, 8, 9, 10, 9, 7, 5, 2, -2, -5, -7, -9, -10, -10, -9, -7, -5, -3]
>>> vals_interp = interpolate_ray_dist(vals, order='cos')
>>> np.round(vals_interp).astype(int).tolist()
[0, 3, 6, 8, 10, 10, 9, 7, 5, 2, -2, -5, -7, -9, -10, -10, -8, -6, -3, 0]
imsegm.descriptors.make_edge_filter2d(sig, phase, points, sup)[source]
imsegm.descriptors.make_gaussian_filter1d(vals, sigma, order=0)[source]
imsegm.descriptors.norm_features(features, scaler=None)[source]

normalise features to be in range(0;1)

Parameters
  • features (ndarray) – vector of features

  • scaler (obj) –

Return list(list(float))

imsegm.descriptors.numpy_img2d_color_energy(img, seg)[source]

compute color energy by numpy

Parameters
  • img (ndarray) – input RGB image

  • seg (ndarray) – segmentation og the image

Returns

np.array<nb_lbs, 3> matrix features per segment

>>> image = np.zeros((2, 10, 3))
>>> image[:, 2:6, 0] = 1
>>> image[:, 3:8, 1] = 3
>>> image[:, 4:9, 2] = 2
>>> segm = np.array([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
...                  [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]])
>>> numpy_img2d_color_energy(image, segm)
array([[ 0.6,  3.6,  0.8],
       [ 0.2,  5.4,  3.2]])
imsegm.descriptors.numpy_img2d_color_mean(img, seg)[source]

compute color means by numpy

Parameters
  • img (ndarray) – input RGB image

  • seg (ndarray) – segmentation og the image

Returns

np.array<nb_lbs, 3> matrix features per segment

>>> image = np.zeros((2, 10, 3))
>>> image[:, 2:6, 0] = 1
>>> image[:, 3:8, 1] = 3
>>> image[:, 4:9, 2] = 2
>>> segm = np.array([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
...                  [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]])
>>> numpy_img2d_color_mean(image, segm)
array([[ 0.6,  1.2,  0.4],
       [ 0.2,  1.8,  1.6]])
imsegm.descriptors.numpy_img2d_color_median(img, seg)[source]

compute color median by numpy

Parameters
  • img (ndarray) – input RGB image

  • seg (ndarray) – segmentation og the image

Returns

np.array<nb_lbs, 3> matrix features per segment

See also

imsegm.descriptors.cython_img2d_color_median()

>>> image = np.zeros((2, 10, 3))
>>> image[:, 2:6, 0] = 1
>>> image[:, 3:8, 1] = 3
>>> image[:, 4:9, 2] = 2
>>> segm = np.array([[0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
...                  [0, 0, 0, 0, 1, 1, 1, 1, 1, 1]])
>>> numpy_img2d_color_median(image, segm)
array([[ 0.5,  0. ,  0. ],
       [ 0. ,  3. ,  2. ]])
imsegm.descriptors.numpy_img2d_color_std(img, seg, means=None)[source]

compute color STD by numpy

Parameters
  • img (ndarray) – input RGB image

  • seg (ndarray) – segmentation og the image

  • means (ndarray) – precomputed feature means

Returns

np.array<nb_lbs, 3> matrix features per segment

>>> image = np.zeros((2, 10, 3))
>>> image[:, 2:6, 0] = 1
>>> image[:, 3:8, 1] = 3
>>> image[:, 4:9, 2] = 2
>>> segm = np.array([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
...                  [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]])
>>> numpy_img2d_color_std(image, segm)
array([[ 0.48989795,  1.46969385,  0.8       ],
       [ 0.4       ,  1.46969385,  0.8       ]])
imsegm.descriptors.numpy_img3d_gray_energy(img, seg)[source]

compute gray (3D) energy by numpy

Parameters
  • img – input RGB image

  • seg – segmentation og the image

Returns

np.array<nb_lbs, 3> matrix features per segment

>>> image = np.zeros((2, 3, 8))
>>> image[0, :, 2:6] = 1
>>> image[1, :, 3:7] = 3
>>> segm = np.array([[[0, 0, 0, 0, 1, 1, 1, 1]] * 3,
...                  [[2, 2, 2, 2, 3, 3, 3, 3]] * 3])
>>> numpy_img3d_gray_energy(image, segm)
array([ 0.5 ,  0.5 ,  2.25,  6.75])
imsegm.descriptors.numpy_img3d_gray_mean(img, seg)[source]

compute gray (3D) means by numpy

Parameters
  • img (ndarray) – input RGB image

  • seg (ndarray) – segmentation og the image

Returns

np.array<nb_lbs, 3> matrix features per segment

>>> image = np.zeros((2, 3, 8))
>>> image[0, :, 2:6] = 1
>>> image[1, :, 3:7] = 3
>>> segm = np.array([[[0, 0, 0, 0, 1, 1, 1, 1]] * 3,
...                  [[2, 2, 2, 2, 3, 3, 3, 3]] * 3])
>>> numpy_img3d_gray_mean(image, segm)
array([ 0.5 ,  0.5 ,  0.75,  2.25])
imsegm.descriptors.numpy_img3d_gray_median(img, seg)[source]

compute gray (3D) median by numpy

Parameters
  • img (ndarray) – input RGB image

  • seg (ndarray) – segmentation og the image

Returns

np.array<nb_lbs, 3> matrix features per segment

See also

imsegm.descriptors.cython_img3d_gray_median()

>>> image = np.zeros((2, 3, 8))
>>> image[0, :, 2:6] = 1
>>> image[1, :, 3:7] = 3
>>> segm = np.array([[[0, 0, 0, 0, 1, 1, 1, 1]] * 3,
...                  [[2, 2, 2, 2, 3, 3, 3, 3]] * 3])
>>> numpy_img3d_gray_median(image, segm)
array([ 0.5,  0.5,  0. ,  3. ])
imsegm.descriptors.numpy_img3d_gray_std(img, seg, means=None)[source]

compute gray (3D) STD by numpy

Parameters
  • img (ndarray) – input RGB image

  • seg (ndarray) – segmentation og the image

  • means (ndarray) – precomputed feature means

Returns

np.array<nb_lbs, 3> matrix features per segment

>>> image = np.zeros((2, 3, 8))
>>> image[0, :, 2:6] = 1
>>> image[1, :, 3:7] = 3
>>> segm = np.array([[[0, 0, 0, 0, 1, 1, 1, 1]] * 3,
...                  [[2, 2, 2, 2, 3, 3, 3, 3]] * 3])
>>> numpy_img3d_gray_std(image, segm)
array([ 0.5       ,  0.5       ,  1.29903811,  1.29903811])
imsegm.descriptors.numpy_ray_features_seg2d(seg_binary, position, angle_step=5.0, edge='up')[source]

computing the Ray features from a segmentation and given position

Parameters
  • seg_binary (ndarray) – np.array<height, width>

  • position (tuple(int,int)) – integer position in the segmentation

  • angle_step (float) – angular step for ray features

  • edge (str) – pointing to the up of down edge of an boundary

Return list(float)

ray distances

>>> seg_empty = np.zeros((100, 150), dtype=bool)
>>> numpy_ray_features_seg2d(seg_empty, (50, 75), 90)  
array([-1., -1., -1., -1.]...)
>>> from imsegm.utilities.drawing import _draw_disk
>>> seg = np.ones((100, 150), dtype=bool)
>>> x, y = _draw_disk(50, 75, 40, shape=seg.shape)
>>> seg[x, y] = False
>>> numpy_ray_features_seg2d(seg, (50, 75), 45).astype(int)  
array([40, 41, 40, 41, 40, 41, 40, 41]...)
>>> numpy_ray_features_seg2d(seg, (60, 40), 30).astype(int).tolist()
[74, 55, 28, 10, 5, 4, 4, 5, 9, 30, 57, 75]
>>> numpy_ray_features_seg2d(seg, (40, 60), 20).astype(int).tolist()
[54, 57, 58, 55, 50, 43, 38, 31, 26, 24, 22, 22, 23, 26, 29, 34, 41, 48]
imsegm.descriptors.reconstruct_ray_features_2d(position, ray_features, shift=0)[source]

reconstruct ray features for 2D image

Parameters
Return [[float, float]]

Note

for more examples, see unittests

>>> reconstruct_ray_features_2d((10., 10), np.array([1] * 4))
array([[ 10.,  11.],
       [ 11.,  10.],
       [ 10.,   9.],
       [  9.,  10.]])
>>> reconstruct_ray_features_2d((10., 10), np.array([-1, 0, 1, np.inf]))
array([[ 10.,  10.],
       [ 10.,   9.]])
imsegm.descriptors.reduce_close_points(points, dist_thr)[source]

reduce remove points with smaller internal distance then treshold assumption, the points are in sequence geometrically ordered)

Parameters
  • float]] points ([[float,) –

  • dist_thr (float) – distance threshold

Return [[float, float]]

>>> points = np.array([range(10), range(10)]).T
>>> reduce_close_points(points, 2)
array([[0, 0],
       [2, 2],
       [4, 4],
       [6, 6],
       [8, 8]])
>>> points = np.array([[0, 0], [1, 1], [0, 2]])
>>> reduce_close_points(points, 2)
array([[0, 0],
       [0, 2]])
>>> reduce_close_points(np.ones((10, 2)), 2)
array([[ 1.,  1.]])
imsegm.descriptors.shift_ray_features(ray_dist, method='phase')[source]

shift Ray features ti the global maxim to be rotation invariant

Parameters
  • ray_dist (list(float)) – array of features

  • method (str) – use method for estimate shift maxima (phase or max)

Return list(float)

>>> vec = np.array([43, 46, 44, 39, 28, 18, 12, 10,  9, 12, 22, 28])
>>> ray, shift = shift_ray_features(vec)
>>> shift   
41.50...
>>> ray
array([46, 44, 39, 28, 18, 12, 10,  9, 12, 22, 28, 43])
>>> ray2, shift = shift_ray_features(ray)
>>> shift  
11.50...
>>> np.array_equal(ray, ray2)
True
>>> ray, shift = shift_ray_features(vec, method='max')
>>> shift   
30.0...
imsegm.descriptors.DEFAULT_FILTERS_SIGMAS = (1.4142135623730951, 2, 2.8284271247461903, 4)[source]

define sigmas for Lewen-Malik filter bank

imsegm.descriptors.FEATURES_SET_ALL = {'color': ('mean', 'std', 'energy', 'median', 'meanGrad'), 'tLM': ('mean', 'std', 'energy', 'median', 'meanGrad')}[source]

define the richest version of computed superpixel features

imsegm.descriptors.FEATURES_SET_COLOR = {'color': ('mean', 'std', 'energy')}[source]

define basic color features for supepixels

imsegm.descriptors.FEATURES_SET_TEXTURE = {'tLM': ('mean', 'std', 'energy')}[source]

define basic texture features (complete LM filter bank) for supepixels

imsegm.descriptors.FEATURES_SET_TEXTURE_SHORT = {'tLM_short': ('mean', 'std', 'energy')}[source]

define basic color features for (small LM filter bank) supepixels

imsegm.descriptors.HIST_CIRCLE_DIAGONALS = (10, 20, 30, 40, 50)[source]

define circular diamters for computing label histogram

imsegm.descriptors.MAX_SIGNAL_RESPONSE = 1000000.0[source]

maximal response is bounded by fix number to prevent overflowing (for LM filer bank)

imsegm.descriptors.NAMES_FEATURE_FLAGS = ('mean', 'std', 'energy', 'median', 'meanGrad')[source]

define all available statistic computed on superpixels

imsegm.descriptors.SHORT_FILTERS_SIGMAS = (1.4142135623730951, 2, 4)[source]

define small list/range of sigmas for Lewen-Malik filter bank