Cleaned up rag.py

This commit is contained in:
Vighnesh Birodkar
2014-08-05 23:33:22 +05:30
parent ddf11bdfe8
commit 0971c11db6
+4 -43
View File
@@ -211,51 +211,12 @@ def rag_mean_color(image, labels, connectivity=2):
graph[x][y]['weight'] = np.linalg.norm(diff)
return graph
def rag_similarity(image, labels, connectivity=2, sigma = 30.0):
graph = RAG()
# 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.
fp = nd.generate_binary_structure(labels.ndim, connectivity)
for d in range(fp.ndim):
fp = fp.swapaxes(0, d)
fp[0, ...] = 0
fp = fp.swapaxes(0, d)
filters.generic_filter(
labels,
function=_add_edge_filter,
footprint=fp,
mode='nearest',
output=np.zeros(labels.shape, dtype=np.uint8),
extra_arguments=(graph,))
def rag_similarity(image, labels, connectivity=2, sigma=30.0):
graph = rag_mean_color(image, labels, connectivity)
for n in graph:
graph.node[n].update({'labels': [n],
'pixel count': 0,
'total color': np.array([0, 0, 0],
dtype=np.double)})
for x, w, d in graph.edges_iter(data=True):
d['weight'] = math.e ** (-(d['weight'] ** 2) / sigma)
for index in np.ndindex(labels.shape):
current = labels[index]
graph.node[current]['pixel count'] += 1
graph.node[current]['total color'] += image[index]
for n in graph:
graph.node[n]['mean color'] = (graph.node[n]['total color'] /
graph.node[n]['pixel count'])
for x, y in graph.edges_iter():
diff = graph.node[x]['mean color'] - graph.node[y]['mean color']
diff = np.linalg.norm(diff)
#if diff == 0:
# graph[x][y]['weight'] = 99999
#else:
graph[x][y]['weight'] = math.e**(-(diff**2)/sigma)
#print graph[x][y]['weight']
#print "diff",diff,"weight",math.e**(-(diff**2)/sigma)
return graph