Formatting

This commit is contained in:
Vighnesh Birodkar
2014-07-29 02:36:20 +05:30
parent 39dfcc4144
commit f4aa0fc8e1
2 changed files with 31 additions and 24 deletions
+4 -3
View File
@@ -1,4 +1,4 @@
from skimage import graph, data, io, segmentation, color
from skimage import graph, data, segmentation
from matplotlib import pyplot as plt
@@ -13,9 +13,10 @@ plt.figure()
plt.title("RAG with all edges shown in green.")
plt.imshow(out)
out = graph.rag.rag_draw(labels, g, img, high_color=(1,0,0), thresh=30)
out = graph.rag.rag_draw(labels, g, img, high_color=(1, 0, 0), thresh=30)
plt.figure()
plt.title("RAG with edge weights less than 30, color mapped between green and red.")
plt.title("RAG with edge weights less than 30,\
color mapped between green and red.")
plt.imshow(out)
plt.show()
+27 -21
View File
@@ -239,22 +239,21 @@ def rag_mean_color(image, labels, connectivity=2, mode='distance',
return graph
def rag_draw(labels, rag, img, border_color = (0,0,0), node_color = (1,1,0),
low_color = (0,1,0), high_color=None, thresh=np.inf ):
def rag_draw(labels, rag, img, border_color=(0, 0, 0), node_color = (1, 1, 0),
low_color = (0, 1, 0), high_color=None, thresh=np.inf):
rag = rag.copy()
rag_labels = labels.copy()
out = img.copy()
low_color = np.array(low_color)
if not high_color is None :
if not high_color is None:
high_color = np.array(high_color)
# Handling the case where one node has multiple labels
offset = 1
for n, d in rag.nodes_iter(data=True):
for l in d['labels'] :
for l in d['labels']:
rag_labels[labels == l] = offset
offset += 1
@@ -263,29 +262,36 @@ def rag_draw(labels, rag, img, border_color = (0,0,0), node_color = (1,1,0),
# Because we kept the offset as 1
rag.node[region['label'] - 1]['centroid'] = region['centroid']
if not border_color is None :
out = segmentation.mark_boundaries(out, rag_labels, color = border_color)
if not border_color is None:
out = segmentation.mark_boundaries(
out,
rag_labels,
color=border_color)
if not high_color is None :
max_weight = max([d['weight'] for x, y, d in rag.edges_iter(data=True) if d['weight'] < thresh ])
min_weight = min([d['weight'] for x, y, d in rag.edges_iter(data=True) if d['weight'] < thresh ])
for n1,n2,data in rag.edges_iter(data=True):
if not high_color is None:
max_weight = max([d['weight'] for x, y, d in rag.edges_iter(data=True)
if d['weight'] < thresh])
min_weight = min([d['weight'] for x, y, d in rag.edges_iter(data=True)
if d['weight'] < thresh])
if data['weight'] >= thresh :
for n1, n2, data in rag.edges_iter(data=True):
if data['weight'] >= thresh:
continue
r1, c1 = map(int, rag.node[n1]['centroid'])
r2, c2 = map(int, rag.node[n2]['centroid'])
line = draw.line(r1, c1, r2, c2)
line = draw.line(r1, c1, r2, c2)
if not high_color is None:
norm_weight = ( rag[n1][n2]['weight'] - min_weight ) / ( max_weight - min_weight )
out[line] = norm_weight*high_color + (1 - norm_weight)*low_color
norm_weight = (rag[n1][n2]['weight'] - min_weight) / (
max_weight - min_weight)
out[line] = norm_weight * high_color + \
(1 - norm_weight) * low_color
else:
out[line] = low_color
circle = draw.circle(r1,c1,2)
circle = draw.circle(r1, c1, 2)
out[circle] = node_color
return out