From 6cc0cf4a88f62d28b83aacbbe7e2c76dd31aa76b Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Thu, 24 Jul 2014 00:00:09 +0530 Subject: [PATCH] use map_array instead of modifying graph --- skimage/graph/graph_cut.py | 29 +++++++++++++---------------- skimage/graph/rag.py | 2 +- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/skimage/graph/graph_cut.py b/skimage/graph/graph_cut.py index 77e92bbd..a5a54141 100644 --- a/skimage/graph/graph_cut.py +++ b/skimage/graph/graph_cut.py @@ -7,8 +7,6 @@ import numpy as np from . import _ncut from . import _ncut_cy from scipy.sparse import linalg -from scipy.sparse.linalg.eigen.arpack.arpack import ArpackNoConvergence -from scipy.sparse.linalg.eigen.arpack.arpack import ArpackError def cut_threshold(labels, rag, thresh): @@ -107,22 +105,19 @@ def cut_n(labels, rag, thresh=0.001, num_cuts=10): IEEE Transactions on , vol.22, no.8, pp.888,905, Aug 2000 """ - _ncut_relabel(rag, thresh, num_cuts) - - from_ = range(labels.max() + 1) - to = [rag.node[x]['ncut label'] for x in from_] - map_array = np.array(to) + map_array = np.arange(labels.max() + 1) + _ncut_relabel(rag, thresh, num_cuts, map_array) return map_array[labels] -def _ncut_relabel(rag, thresh, num_cuts): +def _ncut_relabel(rag, thresh, num_cuts, map_array): """Perform Normalized Graph cut on the Region Adjacency Graph. Recursively partition the graph into 2, untill further subdividing yields a cut greather than `thresh` or such a cut cannot be computed. - For such a subgraph, assign a 'ncut label` attribute to all its nodes, - which is a their new unique label. + For such a subgraph, indices to labels of all its nodes map to a single + unique value. Parameters ---------- @@ -135,10 +130,12 @@ def _ncut_relabel(rag, thresh, num_cuts): value of the N-cut exceeds `thresh`. num_cuts : int The number or N-cuts to perform before determining the optimal one. + map_array : array + The array which maps old labels to new ones. This is modified inside + the function. """ d, w = _ncut.DW_matrix(rag) error = False - try: m = w.shape[0] @@ -154,7 +151,6 @@ def _ncut_relabel(rag, thresh, num_cuts): # k is too less, happens when the graph is of size 1 error = True - if not error: vals, vectors = np.real(vals), np.real(vectors) index2 = _ncut_cy.argmin2(vals) @@ -182,8 +178,8 @@ def _ncut_relabel(rag, thresh, num_cuts): sub1 = rag.subgraph(nodes1) sub2 = rag.subgraph(nodes2) - _ncut_relabel(sub1, thresh, num_cuts) - _ncut_relabel(sub2, thresh, num_cuts) + _ncut_relabel(sub1, thresh, num_cuts, map_array) + _ncut_relabel(sub2, thresh, num_cuts, map_array) return # Either an errornous condition occurred, or N-cut wasn't small enough. @@ -192,5 +188,6 @@ def _ncut_relabel(rag, thresh, num_cuts): # `labels` are unique, 'ncut label' is also unique. node = rag.nodes()[0] new_label = rag.node[node]['labels'][0] - for n in rag.nodes(): - rag.node[n]['ncut label'] = new_label + for n, d in rag.nodes_iter(data=True): + for l in d['labels']: + map_array[l] = new_label diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index 65dff3ba..209ee602 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -156,7 +156,7 @@ def rag_mean_color(image, labels, connectivity=2, mode='dissimilarity', It represents how similar two regions are. sigma : float, optional Used for computation when `mode='dissimilarity'`. It governs how close - to each other, two colors should be, for their corresponding edge + to each other two colors should be, for their corresponding edge weight to be significant. A very large value of `sigma` could make any two colors behave as though they were similar.