From f4aa0fc8e16d8c3ec24759441b8150aea98c0240 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Tue, 29 Jul 2014 02:36:20 +0530 Subject: [PATCH] Formatting --- doc/examples/plot_rag_draw.py | 7 ++--- skimage/graph/rag.py | 48 ++++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/doc/examples/plot_rag_draw.py b/doc/examples/plot_rag_draw.py index 22fb9e63..e554e1a7 100644 --- a/doc/examples/plot_rag_draw.py +++ b/doc/examples/plot_rag_draw.py @@ -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() diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index 42e4c4e3..5a3db12a 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -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