imsegm.region_growing module¶
- Framework for region growing
general GraphCut segmentation with and without shape model
region growing with shape prior - greedy & GraphCut
Copyright (C) 2016-2018 Jiri Borovec <jiri.borovec@fel.cvut.cz>
- imsegm.region_growing.compute_centre_moment_points(points)[source]¶
compute centre and moment from set of points
>>> points = list(zip([0] * 10, np.arange(10))) + [(0, 0)] * 5 >>> compute_centre_moment_points(points) (array([ 0., 3.]), 0.0) >>> points = list(zip(np.arange(10), [0] * 10)) + [(10, 0)] >>> compute_centre_moment_points(points) (array([ 5., 0.]), 90.0) >>> points = list(zip(-np.arange(10), -np.arange(10))) + [(0, 0)] * 5 >>> compute_centre_moment_points(points) (array([-3., -3.]), 45.0) >>> points = list(zip(-np.arange(10), np.arange(10))) + [(-10, 10)] >>> compute_centre_moment_points(points) (array([-5., 5.]), 135.0)
- imsegm.region_growing.compute_cumulative_distrib(means, stds, weights, max_dist)[source]¶
compute invers cumulative distribution based given means, covariance and weights for each segment
- Parameters
- Return list(list(float))
>>> cdist = compute_cumulative_distrib( ... np.array([[1, 2]]), np.array([[1.5, 0.5], [0.5, 1]]), np.array([0.5]), 6) >>> np.round(cdist, 2) array([[ 1. , 0.67, 0.34, 0.12, 0.03, 0. , 0. ], [ 1. , 0.98, 0.5 , 0.02, 0. , 0. , 0. ]])
- imsegm.region_growing.compute_data_costs_points(slic, slic_prob_fg, centres, labels)[source]¶
compute Look up Table ro date term costs
- imsegm.region_growing.compute_object_shapes(list_img_objects, ray_step=5, interp_order=3, smooth_coef=0, shift_method='phase')[source]¶
for all object in all images compute gravity center and Ray beatures (if object are not split already by different label is made here)
- Parameters
- Return tuple(list(list(int)),list(int))
>>> img1 = np.zeros((100, 100)) >>> img1[20:50, 30:60] = 1 >>> img1[40:80, 50:90] = 2 >>> img2 = np.zeros((100, 100)) >>> img2[10:40, 20:50] = 1 >>> img2[50:80, 20:50] = 1 >>> img2[50:80, 60:90] = 1 >>> list_imgs = [img1, img2] >>> list_rays, list_shifts = compute_object_shapes(list_imgs, ray_step=45) >>> np.array(list_rays).astype(int) array([[19, 17, 9, 17, 19, 14, 19, 14], [29, 21, 28, 20, 28, 20, 28, 21], [22, 16, 21, 15, 21, 15, 21, 16], [22, 16, 21, 15, 21, 15, 21, 16], [22, 16, 21, 15, 21, 15, 21, 16]]) >>> np.array(list_shifts) % 180 array([ 135., 45., 45., 45., 45.])
- imsegm.region_growing.compute_pairwise_penalty(edges, labels, prob_bg_fg=0.05, prob_fg1_fg2=0.01)[source]¶
compute cost of neighboring labels pionts
- Parameters
- Returns
>>> edges = np.array([[0, 1], [1, 2], [0, 3], [2, 3], [2, 4]]) >>> labels = np.array([0, 0, 1, 2, 1]) >>> compute_pairwise_penalty(edges, labels, 0.05, 0.01) array([ 0. , 2.99573227, 2.99573227, 4.60517019, 0. ])
- imsegm.region_growing.compute_rg_crit(labels, lut_data_cost, lut_shape_cost, slic_weights, edges, coef_data, coef_shape, coef_pairwise, prob_label_trans)[source]¶
- imsegm.region_growing.compute_segm_object_shape(img_object, ray_step=5, interp_order=3, smooth_coef=0, shift_method='phase')[source]¶
assuming single object in image and compute gravity centre and for this point compute Ray features and optionally: - interpolate missing values - smooth the Ray features
- Parameters
- Return tuple(list(int), int)
>>> img = np.zeros((100, 100)) >>> img[20:70, 30:80] = 1 >>> rays, shift = compute_segm_object_shape(img, ray_step=45) >>> rays [36.7..., 26.0..., 35.3..., 25.0..., 35.3..., 25.0..., 35.3..., 26.0...]
- imsegm.region_growing.compute_segm_prob_fg(slic, segm, labels_prob)[source]¶
compute probability being forground from input segmentation
>>> slic = np.array([[0, 0, 0, 0, 1, 1, 1, 1], [2, 2, 2, 2, 3, 3, 3, 3]]) >>> segm = np.array([0, 1, 1, 0])[slic] >>> compute_segm_prob_fg(slic, segm, [0.3, 0.8]) array([ 0.3, 0.8, 0.8, 0.3])
- imsegm.region_growing.compute_shape_prior_table_cdf(point, cum_distribution, centre, angle_shift=0)[source]¶
compute shape prior for a point based on centre, rotation shift and cumulative histogram
- Parameters
- Return float
>>> chist = [[1.0, 1.0, 0.8, 0.7, 0.6, 0.5, 0.3, 0.0, 0.0], ... [1.0, 1.0, 0.9, 0.8, 0.7, 0.3, 0.2, 0.2, 0.0], ... [1.0, 1.0, 1.0, 0.7, 0.6, 0.5, 0.3, 0.1, 0.1], ... [1.0, 1.0, 0.6, 0.5, 0.4, 0.3, 0.2, 0.0, 0.0]] >>> centre = (1, 1) >>> compute_cdf = compute_shape_prior_table_cdf >>> compute_cdf([1, 1], chist, centre) 1.0 >>> compute_cdf([10, 10], chist, centre) 0.0 >>> compute_cdf([10, -10], chist, centre) 0.100... >>> compute_cdf([2, 3], chist, centre) 0.805... >>> compute_cdf([-3, -2], chist, centre) 0.381... >>> compute_cdf([3, -2], chist, centre) 0.676... >>> compute_cdf([2, 3], chist, centre, angle_shift=270) 0.891...
- imsegm.region_growing.compute_update_shape_costs_points_close_mean_cdf(lut_shape_cost, slic, points, labels, init_centres, centres, shifts, volumes, shape_model_cdfs, selected_idx=None, swap_shift=False, dict_thresholds=None)[source]¶
update the shape prior for given segmentation (new centre is computed), set of points and cumulative histogram representing the shape model
- Parameters
lut_shape_cost – look-up-table for shape cost for GC
slic (ndarray) – superpixel segmentation
points (list(tuple(int,int))) – subsample space, points = superpixel centres
labels (list(int)) – labels for points to be assigned to an object
init_centres (list(tuple(int,int))) – initial centre position for compute center shift during the iterations
shape_model_cdfs – represent the shape prior and histograms
swap_shift (bool) – allow swapping orientation by 90 degree, try to get out from local optimal
dict_thresholds (dict|None) – configuration with thresholds
dict_thresholds – set some threshold updating shape prior
- Return tuple(list(float),list(int))
>>> np.random.seed(0) >>> h, w, step = 8, 8, 2 >>> slic = np.array([[ 0, 0, 1, 1, 2, 2, 3, 3], ... [ 0, 0, 1, 1, 2, 2, 3, 3], ... [ 4, 4, 5, 5, 6, 6, 7, 7], ... [ 4, 4, 5, 5, 6, 6, 7, 7], ... [ 8, 8, 9, 9, 10, 10, 11, 11], ... [ 8, 8, 9, 9, 10, 10, 11, 11], ... [12, 12, 13, 13, 14, 14, 15, 15], ... [12, 12, 13, 13, 14, 14, 15, 15]]) >>> points = np.array([(0, 0), (0, 2), (0, 4), (0, 6), (2, 0), (2, 2), ... (2, 4), (2, 6), (4, 0), (4, 2), (4, 4), (4, 6), ... (6, 0), (6, 2), (6, 4), (6, 6)]) >>> labels = np.array([0] * 4 + [0, 1, 1, 0, 0, 1, 1, 0] + [0] * 4) >>> cdf1, cdf2 = np.zeros((8, 10)), np.zeros((8, 7)) >>> cdf1[:7] = 0.5 >>> cdf1[:4] = 1.0 >>> cdf2[:6] = 1.0 >>> set_m_cdf = [([4] * 8, cdf1), ([5] * 8, cdf2)] >>> s_costs = np.zeros((len(points), 2)) >>> mm = mixture.GaussianMixture(2).fit(np.random.random((100, 8))) >>> s_costs, centres, shifts, _ = compute_update_shape_costs_points_close_mean_cdf( ... s_costs, slic, points, labels, [(0, 0)], ... [(np.Inf, np.Inf)], [0], [0], (mm, set_m_cdf)) >>> centres array([[3, 3]]) >>> shifts array([ 90.]) >>> np.round(s_costs, 3) array([[ 0. , -0.01 ], [ 0. , -0.01 ], [ 0. , -0.01 ], [ 0. , -0.01 ], [ 0. , -0.01 ], [ 0. , -0.01 ], [ 0. , -0.01 ], [ 0. , 0.868], [ 0. , -0.01 ], ... [ 0. , 4.605]])
- imsegm.region_growing.compute_update_shape_costs_points_table_cdf(lut_shape_cost, points, labels, init_centres, centres, shifts, volumes, shape_chist, selected_idx=None, swap_shift=False, dict_thresholds=None)[source]¶
update the shape prior for given segmentation (new centre is computed), set of points and cumulative histogram representing the shape model
- Parameters
lut_shape_cost – look-up-table for shape cost for GC
points (list(tuple(int,int))) – subsample space, points = superpixel centres
labels (list(int)) – labels for points to be assigned to an object
init_centres (list(tuple(int,int))) – initial centre position for compute center shift during the iteretions
shape_chist – represent the shape prior and histograms
swap_shift (bool) – allow swapping orientation by 90 degree, try to get out from local optimal
dict_thresholds (dict|None) – configuration with thresholds
dict_thresholds – set some threshold updating shape prior
- Return tuple(list(float),list(int))
>>> cdf = np.zeros((8, 20)) >>> cdf[:10] = 0.5 >>> cdf[:4] = 1.0 >>> points = np.array([[13, 16], [1, 5], [10, 15], [15, 25], [10, 5]]) >>> labels = np.ones(len(points)) >>> s_costs = np.zeros((len(points), 2)) >>> s_costs, centres, shifts, _ = compute_update_shape_costs_points_table_cdf( ... s_costs, points, labels, [(0, 0)], [(np.Inf, np.Inf)], [0], [0], (None, cdf)) >>> centres array([[10, 13]]) >>> shifts array([ 209.]) >>> np.round(s_costs, 3) array([[ 0. , 0.673], [ 0. , -0.01 ], [ 0. , 0.184], [ 0. , 0.543], [ 0. , 0.374]]) >>> dict_thrs = RG2SP_THRESHOLDS >>> dict_thrs['centre_init'] = 1 >>> _, centres, _, _ = compute_update_shape_costs_points_table_cdf( ... s_costs, points, labels, [(7, 18)], [(np.Inf, np.Inf)], [0], [0], (None, cdf), ... dict_thresholds=dict_thrs) >>> np.round(centres, 1) array([[ 7.5, 17.1]])
- imsegm.region_growing.enforce_center_labels(slic, labels, centres)[source]¶
force the labels to hold label of the center, prevention of desepearing labels of any center in list
- Parameters
slic –
labels –
centres –
- Returns
- imsegm.region_growing.get_neighboring_candidates(slic_neighbours, labels, object_idx, use_other_obj=True)[source]¶
get neighboring candidates from background and optionally also from foreground if it is allowed
- Parameters
- Return list(int)
>>> neighbours = [[1], [0, 2, 3], [1, 3], [1, 2]] >>> labels = np.array([0, 0, 1, 1]) >>> get_neighboring_candidates(neighbours, labels, 1) [1]
- imsegm.region_growing.object_segmentation_graphcut_pixels(segm, centres, labels_fg_prob=(0.1, 0.9), gc_regul=1, seed_size=0, coef_shape=0.0, shape_mean_std=(50.0, 10.0), debug_visual=None)[source]¶
object segmentation using Graph Cut directly on pixel level
- Parameters
segm (ndarray) – input structure segmentation
centres – superpixel centres
labels_fg_prob (list(float)) – set how much particular label belongs to foreground
gc_regul (float) – regularisation for GC
seed_size (int) – create circular neighborhood around initial centre
coef_shape (float) – set the weight of shape prior
shape_mean_std – mean and STD for shape prior
debug_visual (dict) – dictionary with some intermediate results
- Return list(list(int))
>>> segm = np.array([[0] * 10, ... [1] * 5 + [0] * 5, [1] * 4 + [0] * 6, ... [0] * 6 + [1] * 4, [0] * 5 + [1] * 5, ... [0] * 10]) >>> centres = [(1, 2), (4, 8)] >>> object_segmentation_graphcut_pixels(segm, centres, gc_regul=0., coef_shape=0.5) array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 2, 1, 2, 2, 0, 0, 0, 0, 0], [2, 2, 2, 2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 2, 2, 2], [0, 0, 0, 0, 0, 2, 2, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32) >>> object_segmentation_graphcut_pixels(segm, centres, gc_regul=.5, seed_size=1) array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2, 2, 2, 2], [0, 0, 0, 0, 0, 2, 2, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)
- imsegm.region_growing.object_segmentation_graphcut_slic(slic, segm, centres, labels_fg_prob=(0.1, 0.9), gc_regul=1, edge_coef=0.5, edge_type='model', coef_shape=0.0, shape_mean_std=(50.0, 10.0), add_neighbours=False, debug_visual=None)[source]¶
object segmentation using Graph Cut directly on super-pixel level
- Parameters
slic (ndarray) – superpixel pre-segmentation
segm (ndarray) – input structure segmentation
labels_fg_prob (list(float)) – weight for particular label belongs to FG
gc_regul (float) – regularisation for GC
edge_coef (float) – weight og edges on GC
edge_type (str) – select the egde weights on graph
coef_shape (float) – set the weight of shape prior
shape_mean_std – mean and STD for shape prior
add_neighbours (bool) – add also neighboring supepixels to the center
debug_visual (dict) – dictionary with some intermediate results
- Return list(list(int))
>>> slic = np.array([[0] * 3 + [1] * 3 + [2] * 3 + [3] * 3 + [4] * 3, ... [5] * 3 + [6] * 3 + [7] * 3 + [8] * 3 + [9] * 3]) >>> segm = np.array([[0] * 15, [1] * 12 + [0] * 3]) >>> object_segmentation_graphcut_slic(slic, segm, [(1, 7)], gc_regul=0., edge_coef=1., coef_shape=1.) array([0, 0, 0, 0, 0, 1, 1, 1, 1, 0], dtype=int32) >>> object_segmentation_graphcut_slic(slic, segm, [(1, 7)], gc_regul=1., edge_coef=1., debug_visual={}) array([0, 0, 0, 0, 0, 1, 1, 1, 1, 0], dtype=int32)
- imsegm.region_growing.prepare_graphcut_variables(candidates, slic_points, slic_neighbours, slic_weights, labels, nb_centres, lut_data_cost, lut_shape_cost, coef_data, coef_shape, coef_pairwise, prob_label_trans)[source]¶
for boundary get connected points in BG and FG construct graph and set potentials and hard connect BG and FG in unary
- Parameters
candidates (list(int)) – list of candidates, neighbours of actual objects
slic_neighbours (list(list(int))) – list of neighboring superpixel for each one
nb_centres (int) – number of centres - classes
lut_data_cost (ndarray) – look-up-table for data cost for each object (class) with superpixel as first index
lut_shape_cost (ndarray) – look-up-table for shape cost for each object (class) with superpixel as first index
coef_data (float) – weight for data priors
coef_shape (float) – weight for shape priors
coef_pairwise (float) – CG pairwise coeficient
prob_label_trans – probability transition between background (first) and objects and among objects (second)
- Returns
- imsegm.region_growing.region_growing_shape_slic_graphcut(slic, slic_prob_fg, centres, shape_model, shape_type='cdf', coef_data=1.0, coef_shape=1, coef_pairwise=2, prob_label_trans=(0.1, 0.03), optim_global=True, allow_obj_swap=True, dict_thresholds=None, nb_iter=999, debug_history=None)[source]¶
Region growing method with given shape prior on pre-segmented images it uses the GraphCut strategy on neigbouring superpixels
- Parameters
slic (ndarray) – superpixel segmentation
slic_prob_fg (list(float)) – weight for particular superpixel belongs to FG
shape_model – represent the shape prior and histograms
shape_type (str) – identification of used shape model
coef_data (float) – weight for data prior
coef_shape (float) – weight for shape prior
coef_pairwise (float) – setting for pairwise cost
prob_label_trans – probability transition between background (first) and objects and among objects (second)
optim_global (bool) – optimise the GC as global or per object
allow_obj_swap (bool) – allow swapping foreground object labels
dict_thresholds (dict|None) – configuration with thresholds
nb_iter (int) – maximal number of iterations
dict_thresholds – set some threshold updating shape prior
>>> h, w, step = 15, 20, 2 >>> segm = np.zeros((h, w), dtype=int) >>> segm[3:12, 5:17] = 1 >>> segm array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) >>> slic = 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) ... slic[i_step:int(i_step + step), j_step:int(j_step + step)] = val >>> centres = [(7.5, 10)] >>> chist = [[1.] * 3 + [0.8, 0.7, 0.6, 0.5, 0.3, 0.1, 0.0], ... [1.] * 3 + [0.9, 0.8, 0.7, 0.3, 0.2, 0.2, 0.1], ... [1.] * 3 + [1.0, 0.7, 0.6, 0.5, 0.3, 0.1, 0.1], ... [1.] * 3 + [0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]] >>> dict_debug = {} >>> slic_prob_fg = compute_segm_prob_fg(slic, segm, [0.1, 0.9]) >>> labels = region_growing_shape_slic_graphcut(slic, slic_prob_fg, centres, ... (None, chist), coef_pairwise=0, ... debug_history=dict_debug) >>> np.round(dict_debug['criteria']).astype(int) array([397, 325, 206, 111, 81, 81]) >>> labels[slic] array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) >>> labels = region_growing_shape_slic_graphcut(slic, slic_prob_fg, centres, ... (None, chist), coef_pairwise=2, ... debug_history=dict_debug) >>> np.round(dict_debug['criteria']).astype(int) array([415, 380, 289, 193, 164, 164]) >>> labels[slic] array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) >>> segm = np.ones((h, w), dtype=int) >>> chist = np.zeros((16, 9)) >>> chist[:, :5] = 1. >>> dict_debug = {} >>> slic_prob_fg = compute_segm_prob_fg(slic, segm, [0.1, 0.9]) >>> labels = region_growing_shape_slic_graphcut(slic, slic_prob_fg, [(6.5, 9)], ... (None, chist), coef_shape=10., ... coef_pairwise=1, ... debug_history=dict_debug) >>> np.round(dict_debug['criteria']).astype(int) array([7506, 7120, 6328, 5719, 5719]) >>> labels[slic] array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
- imsegm.region_growing.region_growing_shape_slic_greedy(slic, slic_prob_fg, centres, shape_model, shape_type='cdf', coef_data=1.0, coef_shape=1, coef_pairwise=1, prob_label_trans=(0.1, 0.01), allow_obj_swap=True, greedy_tol=0.001, dict_thresholds=None, nb_iter=999, debug_history=None)[source]¶
Region growing method with given shape prior on pre-segmented images it uses the Greedy strategy and set some stopping criterion
- Parameters
slic (ndarray) – superpixel segmentation
slic_prob_fg (list(float)) – weight for particular superpixel belongs to FG
shape_model – represent the shape prior and histograms
shape_type (str) – identification of used shape model
coef_data (float) – weight for data prior
coef_shape (float) – weight for shape prior
coef_pairwise (float) – setting for pairwise cost
prob_label_trans – probability transition between background (first) and objects and among objects (second)
allow_obj_swap (bool) – allow swapping foreground object labels
greedy_tol (float) – stopping criterion - energy change between inters
dict_thresholds (dict|None) – configuration with thresholds
nb_iter (int) – maximal number of iterations
dict_thresholds – set some threshold updating shape prior
- Returns
>>> np.random.seed(0) >>> h, w, step = 15, 20, 2 >>> segm = np.zeros((h, w), dtype=int) >>> segm[3:12, 5:17] = 1 >>> segm array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) >>> slic = 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) ... slic[i_step:int(i_step + step), j_step:int(j_step + step)] = val >>> centres = [(7.5, 10)] >>> chist = [[1.] * 3 + [0.8, 0.7, 0.6, 0.5, 0.3, 0.1, 0.0], ... [1.] * 3 + [0.9, 0.8, 0.7, 0.3, 0.2, 0.2, 0.1], ... [1.] * 3 + [1.0, 0.7, 0.6, 0.5, 0.3, 0.1, 0.1], ... [1.] * 3 + [0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0]] >>> dict_debug = {} >>> slic_prob_fg = compute_segm_prob_fg(slic, segm, [0.1, 0.9]) >>> labels = region_growing_shape_slic_greedy(slic, slic_prob_fg, centres, ... (None, chist), coef_pairwise=0, ... debug_history=dict_debug) >>> np.round(dict_debug['criteria']).astype(int) array([397, 325, 307, 289, 272, 238, 204, 188, 173, ..., 81, 81]) >>> labels[slic] array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) >>> labels = region_growing_shape_slic_greedy(slic, slic_prob_fg, centres, ... (None, chist), coef_pairwise=1, ... debug_history=dict_debug) >>> np.round(dict_debug['criteria']).astype(int) array([406, 352, 334, 316, 300, 283, 270, 254, 238, 226, 210, ..., 123, 123]) >>> labels[slic] array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) >>> segm = np.ones((h, w), dtype=int) >>> chist = np.zeros((16, 9)) >>> chist[:, :5] = 1. >>> slic_prob_fg = compute_segm_prob_fg(slic, segm, [0.1, 0.9]) >>> labels = region_growing_shape_slic_greedy(slic, slic_prob_fg, [(6.5, 9)], ... (None, chist), coef_shape=10, ... coef_pairwise=1, ... debug_history=dict_debug) >>> np.round(dict_debug['criteria']).astype(int) array([7506, 7120, 6715, 6328, 5719, 5719]) >>> labels[slic] array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
- imsegm.region_growing.transform_rays_model_cdf_histograms(list_rays, nb_bins=10)[source]¶
from list of all measured rays create cumulative histogram for each ray
- Parameters
- Returns
>>> list_rays = [[9, 4, 9], [4, 9, 7], [9, 7, 11], [10, 8, 10], ... [9, 11, 8], [4, 8, 5], [8, 10, 6], [9, 7, 11]] >>> chist = transform_rays_model_cdf_histograms(list_rays, nb_bins=5) >>> chist [[1.0, 1.0, 1.0, 1.0, 0.75, 0.75, 0.75, 0.625, 0.625, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0, 0.875, 0.875, 0.875, 0.375, 0.25, 0.25, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0, 0.75, 0.625, 0.5, 0.375, 0.375, 0.0, 0.0]]
- imsegm.region_growing.transform_rays_model_cdf_kmeans(list_rays, nb_components=None)[source]¶
compute the mixture model and transform it into cumulative distribution
- Parameters
- Return any, list(list(int))
mixture model, list of stat/param of models
>>> np.random.seed(0) >>> list_rays = [[9, 4, 9], [4, 9, 7], [9, 7, 11], [10, 8, 10], ... [9, 11, 8], [4, 8, 5], [8, 10, 6], [9, 7, 11]] >>> mm, cdist = transform_rays_model_cdf_kmeans(list_rays) >>> np.round(cdist, 1).tolist() [[1.0, 1.0, 1.0, 1.0, 0.9, 0.8, 0.7, 0.7, 0.6, 0.4, 0.2, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0, 0.9, 0.9, 0.8, 0.7, 0.5, 0.3, 0.2, 0.1, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0, 0.9, 0.8, 0.7, 0.5, 0.4, 0.2, 0.1, 0.0]] >>> mm, cdist = transform_rays_model_cdf_kmeans(list_rays, nb_components=2)
- imsegm.region_growing.transform_rays_model_cdf_mixture(list_rays, coef_components=1)[source]¶
compute the mixture model and transform it into cumulative distribution
- Parameters
- Return any, list(list(int))
mixture model, cumulative distribution
>>> np.random.seed(0) >>> list_rays = [[9, 4, 9], [4, 9, 7], [9, 7, 11], [10, 8, 10], ... [9, 11, 8], [4, 8, 5], [8, 10, 6], [9, 7, 11]] >>> mm, cdist = transform_rays_model_cdf_mixture(list_rays) >>> # the rounding variate a bit according GMM estimated model >>> np.round(np.array(cdist) * 4) / 4. array([[ 1. , 1. , 1. , 1. , 1. , 1. , 0.75, 0.75, 0.5 , 0.25, 0. ], [ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.75, 0.5 , 0.25, 0. ], [ 1. , 1. , 1. , 1. , 1. , 1. , ..., 0.75, 0.5 , 0.25, 0. ]])
- imsegm.region_growing.transform_rays_model_cdf_spectral(list_rays, nb_components=5)[source]¶
compute the mixture model and transform it into cumulative distribution
- Parameters
- Return tuple(any,list(list(int)))
mixture model, list of stat/param of models
>>> np.random.seed(0) >>> list_rays = [[9, 4, 9], [4, 9, 7], [9, 7, 11], [10, 8, 10], ... [9, 11, 8], [4, 8, 5], [8, 10, 6], [9, 7, 11]] >>> mm, cdist = transform_rays_model_cdf_spectral(list_rays) >>> np.round(cdist, 1).tolist() [[1.0, 1.0, 1.0, 1.0, 1.0, 0.9, 0.8, 0.6, 0.5, 0.2, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0, 0.9, 0.9, 0.7, 0.5, 0.2, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0, 0.9, 0.8, 0.7, 0.5, 0.3, 0.0]]
- imsegm.region_growing.transform_rays_model_sets_mean_cdf_kmeans(list_rays, nb_components=5)[source]¶
compute the mixture model and transform it into cumulative distribution
- Parameters
- Return tuple(any,list(list(int)))
mixture model, list of stat/param of models
>>> np.random.seed(0) >>> list_rays = [[9, 4, 9], [4, 9, 7], [9, 7, 11], [10, 8, 10], ... [9, 11, 8], [4, 8, 5], [8, 10, 6], [9, 7, 11]] >>> mm, mean_cdf = transform_rays_model_sets_mean_cdf_kmeans(list_rays, 2) >>> len(mean_cdf) 2
- imsegm.region_growing.transform_rays_model_sets_mean_cdf_mixture(list_rays, nb_components=5, slic_size=15)[source]¶
compute the mixture model and transform it into cumulative distribution
- Parameters
- Return tuple(any,list(list(int)))
mixture model, list of stat/param of models
>>> np.random.seed(0) >>> list_rays = [[9, 4, 9], [4, 9, 7], [9, 7, 11], [10, 8, 10], ... [9, 11, 8], [4, 8, 5], [8, 10, 6], [9, 7, 11]] >>> mm, mean_cdf = transform_rays_model_sets_mean_cdf_mixture(list_rays, 2) >>> len(mean_cdf) 2
- imsegm.region_growing.update_shape_costs_points(lut_shape_cost, slic, points, labels, init_centres, centres, shifts, volumes, shape_model, shape_type, selected_idx=None, swap_shift=False, dict_thresholds=None)[source]¶
update the shape prior for given segmentation (new centre is computed), set of points and shape model
- Parameters
lut_shape_cost – look-up-table for shape cost for GC
slic (nadarray) – superpixel segmentation
points (list(tuple(int,int))) – subsample space, points = superpixel centres
labels (list(int)) – labels for points to be assigned to an object
init_centres (list(tuple(int,int))) – initial centre position for compute center shift during the iteretions
shape_model – represent the shape prior and histograms
shape_type (str) – type or shape model
swap_shift (bool) – allow swapping orientation by 90 degree, try to get out from local optima
dict_thresholds (dict|None) – configuration with thresholds
dict_thresholds – set some threshold updating shape prior
- Return tuple(list(float),list(int))
- imsegm.region_growing.GC_REPLACE_INF = 100000.0[source]¶
all infinty values in Grah-Cut terms replace by this value
- imsegm.region_growing.MAX_UNARY_PROB = 0.99[source]¶
define maximal value of unary (being a class) term in Graph-Cut