Added comments and switched try with if

This commit is contained in:
Vighnesh Birodkar
2014-06-19 16:18:53 +05:30
parent 8a36040eb8
commit 20cefcf50a
2 changed files with 10 additions and 7 deletions
+1
View File
@@ -39,6 +39,7 @@ def threshold_cut(label, rag, thresh):
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.11.5274
"""
# Because deleting edges while iterating through them produces an error.
to_remove = [(x, y) for x, y, d in rag.edges_iter(data=True)
if d['weight'] >= thresh]
rag.remove_edges_from(to_remove)
+9 -7
View File
@@ -50,9 +50,9 @@ class RAG(nx.Graph):
def _add_edge_filter(values, g):
"""Adds an edge between first element in `values` and
all other elements of ` in the graph `g`.
all other elements of `values` in the graph `g`.
Paramteres
Parameters
----------
values : array
The array to process.
@@ -116,22 +116,24 @@ def rag_meancolor(img, arr):
slc = slice(1, None, None)
fp[(slc,) * arr.ndim] = 1
# The footprint is constructed in such a way that the first
# element in the array being passed to _add_edge_filter is
# the central value.
filters.generic_filter(
arr,
function=_add_edge_filter,
footprint=fp,
mode='constant',
cval=-1,
extra_arguments=(g,
))
extra_arguments=(g,))
for index in np.ndindex(arr.shape):
current = arr[index]
try:
if 'pixel count' in g.node[current]:
g.node[current]['pixel count'] += 1
g.node[current]['total color'] += img[index]
except KeyError:
g.add_node(current)
else:
g.node[current]['pixel count'] = 1
g.node[current]['total color'] = img[index].astype(np.double)
g.node[current]['labels'] = [arr[index]]