From 39dfcc414499f7a84ebf53900b93ae192f4eb54f Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Tue, 29 Jul 2014 01:05:10 +0530 Subject: [PATCH 01/21] Frist working copy of RAG draw --- doc/examples/plot_rag_draw.py | 21 ++++++++++++++ skimage/graph/rag.py | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 doc/examples/plot_rag_draw.py diff --git a/doc/examples/plot_rag_draw.py b/doc/examples/plot_rag_draw.py new file mode 100644 index 00000000..22fb9e63 --- /dev/null +++ b/doc/examples/plot_rag_draw.py @@ -0,0 +1,21 @@ +from skimage import graph, data, io, segmentation, color +from matplotlib import pyplot as plt + + +img = data.coffee() + +labels = segmentation.slic(img, compactness=30, n_segments=400) + +g = graph.rag_mean_color(img, labels) + +out = graph.rag.rag_draw(labels, g, img) +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) +plt.figure() +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 5ef9a83b..42e4c4e3 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -13,6 +13,7 @@ import numpy as np from scipy.ndimage import filters from scipy import ndimage as nd import math +from .. import draw, measure, segmentation def min_weight(graph, src, dst, n): @@ -236,3 +237,55 @@ def rag_mean_color(image, labels, connectivity=2, mode='distance', raise ValueError("The mode '%s' is not recognised" % mode) 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 ): + rag = rag.copy() + rag_labels = labels.copy() + out = img.copy() + + low_color = np.array(low_color) + + 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'] : + rag_labels[labels == l] = offset + offset += 1 + + regions = measure.regionprops(rag_labels) + for region in regions: + # 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 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 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) + + 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 + else: + out[line] = low_color + + circle = draw.circle(r1,c1,2) + out[circle] = node_color + + return out From f4aa0fc8e16d8c3ec24759441b8150aea98c0240 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Tue, 29 Jul 2014 02:36:20 +0530 Subject: [PATCH 02/21] 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 From 5c662b447227ed422bdbf2b1ae51602d0c846780 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Tue, 29 Jul 2014 03:12:22 +0530 Subject: [PATCH 03/21] Docstrings --- doc/examples/plot_rag_draw.py | 16 +++++++--- skimage/graph/__init__.py | 4 +++ skimage/graph/rag.py | 60 ++++++++++++++++++++++++++++++----- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/doc/examples/plot_rag_draw.py b/doc/examples/plot_rag_draw.py index e554e1a7..1c47743e 100644 --- a/doc/examples/plot_rag_draw.py +++ b/doc/examples/plot_rag_draw.py @@ -1,22 +1,28 @@ +""" +=========== +RAG Drawing +=========== + +This example constructs a Region Adjacency Graph (RAG) and draws it with +the `rag_draw` method. +""" from skimage import graph, data, segmentation from matplotlib import pyplot as plt img = data.coffee() - labels = segmentation.slic(img, compactness=30, n_segments=400) - g = graph.rag_mean_color(img, labels) -out = graph.rag.rag_draw(labels, g, img) +out = graph.rag_draw(labels, g, img) 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_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.imshow(out) +plt.imshow(out) plt.show() diff --git a/skimage/graph/__init__.py b/skimage/graph/__init__.py index aca73bc4..2f1162d3 100644 --- a/skimage/graph/__init__.py +++ b/skimage/graph/__init__.py @@ -2,8 +2,11 @@ from .spath import shortest_path from .mcp import MCP, MCP_Geometric, MCP_Connect, MCP_Flexible, route_through_array from .rag import rag_mean_color, RAG from .graph_cut import cut_threshold, cut_normalized +from .rag import rag_mean_color, RAG, rag_draw +from .graph_cut import cut_threshold ncut = cut_normalized + __all__ = ['shortest_path', 'MCP', 'MCP_Geometric', @@ -14,4 +17,5 @@ __all__ = ['shortest_path', 'cut_threshold', 'cut_normalized', 'ncut', + 'rag_draw', 'RAG'] diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index 5a3db12a..d01d6e12 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -241,6 +241,52 @@ def rag_mean_color(image, labels, connectivity=2, mode='distance', 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): + """Draw a Region Adjacency Graph on an image. + + Given a labelled image and its corresponding RAG, draw the nodes and edges + of the RAG on the image with the specified colors. Nodes are markes by + the centroids of the corresposning regions. + + Parameters + ---------- + labels : ndarray, shape(M, N, [..., P,]) + The labelled image. This should have one dimension less than + `img`. If `image` has dimensions `(M, N, 3)` `labels` should have + dimensions `(M, N)`. + rag : RAG + The Region Adjacency Graph. + img : ndarray, shape(M, N, [..., P,] 3) + Input image. + border_color : length-3 sequence, optional + RGB color of the corder of regions. Specifying `None` won't draw + the border. + node_color : length-3 sequeunce, optional + RGB color of the centroid of nodes. Yellow by default. + low_color : length-3 sequeunce, optional + RGB color of the edges. If `high_color` is not specified, all edges + are draw with `low_color`. Green by default. + high_color : length-3 sequeunce, optional + RGB color of the edges with high weight. If specified, the edges are + color mapped between `low_color` and `high_color` depending on their + weight. Edges with low weights are more like `low_color` whereas edges + with high weights are more like `high_color`. + thresh : float, optiona; + Edges with weight below `thresh` are not drawn, or considered for color + mapping in case `high_color` is specified. + + Returns + ------- + out : ndarray, shape(M, N, [..., P,] 3) + The image with the RAG drawn. + + Examples + -------- + >>> from skimage import data, graph, segmentation + >>> img = data.lena() + >>> labels = segmentation.slic(img) + >>> g = graph.rag_mean_color(img, labels) + >>> out = graph.rag_draw(labels, g, img) + """ rag = rag.copy() rag_labels = labels.copy() out = img.copy() @@ -251,6 +297,7 @@ def rag_draw(labels, rag, img, border_color=(0, 0, 0), node_color = (1, 1, 0), high_color = np.array(high_color) # Handling the case where one node has multiple labels + # offset is 1 so that regionprops does not ignore 0 offset = 1 for n, d in rag.nodes_iter(data=True): for l in d['labels']: @@ -263,10 +310,7 @@ def rag_draw(labels, rag, img, border_color=(0, 0, 0), node_color = (1, 1, 0), rag.node[region['label'] - 1]['centroid'] = region['centroid'] if not border_color is None: - out = segmentation.mark_boundaries( - out, - rag_labels, - color=border_color) + 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) @@ -284,10 +328,10 @@ def rag_draw(labels, rag, img, border_color=(0, 0, 0), node_color = (1, 1, 0), 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 From 6928e5d9ef3c85fbd18ed6f008d55007947e4f36 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Tue, 29 Jul 2014 03:15:24 +0530 Subject: [PATCH 04/21] Corrected docstrings --- skimage/graph/rag.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index d01d6e12..9368f558 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -251,15 +251,15 @@ def rag_draw(labels, rag, img, border_color=(0, 0, 0), node_color = (1, 1, 0), ---------- labels : ndarray, shape(M, N, [..., P,]) The labelled image. This should have one dimension less than - `img`. If `image` has dimensions `(M, N, 3)` `labels` should have + `img`. If `img` has dimensions `(M, N, 3)` `labels` should have dimensions `(M, N)`. rag : RAG The Region Adjacency Graph. img : ndarray, shape(M, N, [..., P,] 3) Input image. border_color : length-3 sequence, optional - RGB color of the corder of regions. Specifying `None` won't draw - the border. + RGB color of the border of regions. Specifying `None` won't draw + the border. Black by default. node_color : length-3 sequeunce, optional RGB color of the centroid of nodes. Yellow by default. low_color : length-3 sequeunce, optional From 78caebf6d25b6bbba070b0d3b74abc26800dfa36 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Tue, 29 Jul 2014 03:31:12 +0530 Subject: [PATCH 05/21] string wrap --- doc/examples/plot_rag_draw.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/examples/plot_rag_draw.py b/doc/examples/plot_rag_draw.py index 1c47743e..51a33dbd 100644 --- a/doc/examples/plot_rag_draw.py +++ b/doc/examples/plot_rag_draw.py @@ -21,8 +21,8 @@ plt.imshow(out) out = graph.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() From 4005749a61f6022e3adc509b775f9d4c8a5a2c2d Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Tue, 5 Aug 2014 23:05:15 +0530 Subject: [PATCH 06/21] rebase and change API to support mpl colorspec --- doc/examples/plot_rag_draw.py | 13 ++--- skimage/graph/__init__.py | 2 + skimage/graph/rag.py | 89 ++++++++++++++++++----------------- 3 files changed, 56 insertions(+), 48 deletions(-) diff --git a/doc/examples/plot_rag_draw.py b/doc/examples/plot_rag_draw.py index 51a33dbd..21dbbc28 100644 --- a/doc/examples/plot_rag_draw.py +++ b/doc/examples/plot_rag_draw.py @@ -1,25 +1,26 @@ """ -=========== -RAG Drawing -=========== +===================================== +Drawing Region Adjacency Graphs (RAGs) +====================================== This example constructs a Region Adjacency Graph (RAG) and draws it with the `rag_draw` method. """ from skimage import graph, data, segmentation -from matplotlib import pyplot as plt +from matplotlib import pyplot as plt, colors img = data.coffee() labels = segmentation.slic(img, compactness=30, n_segments=400) g = graph.rag_mean_color(img, labels) -out = graph.rag_draw(labels, g, img) +out = graph.draw_rag(labels, g, img) plt.figure() plt.title("RAG with all edges shown in green.") plt.imshow(out) -out = graph.rag_draw(labels, g, img, high_color=(1, 0, 0), thresh=30) +cmap = colors.ListedColormap(['cyan', 'red']) +out = graph.draw_rag(labels, g, img, colormap=cmap, thresh=30) plt.figure() plt.title("RAG with edge weights less than 30, color " "mapped between green and red.") diff --git a/skimage/graph/__init__.py b/skimage/graph/__init__.py index 2f1162d3..2cd422ba 100644 --- a/skimage/graph/__init__.py +++ b/skimage/graph/__init__.py @@ -3,6 +3,7 @@ from .mcp import MCP, MCP_Geometric, MCP_Connect, MCP_Flexible, route_through_ar from .rag import rag_mean_color, RAG from .graph_cut import cut_threshold, cut_normalized from .rag import rag_mean_color, RAG, rag_draw +from .rag import rag_mean_color, RAG, draw_rag from .graph_cut import cut_threshold ncut = cut_normalized @@ -18,4 +19,5 @@ __all__ = ['shortest_path', 'cut_normalized', 'ncut', 'rag_draw', + 'draw_rag', 'RAG'] diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index 9368f558..4447c4e3 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -9,11 +9,21 @@ except ImportError: raise ImportError(msg) import warnings warnings.warn(msg) + import numpy as np from scipy.ndimage import filters from scipy import ndimage as nd +<<<<<<< HEAD import math from .. import draw, measure, segmentation +======= +from .. import draw +from .. import measure +from .. import segmentation +from matplotlib import colors +from matplotlib import cm +from .. import util +>>>>>>> rebase and change API to support mpl colorspec def min_weight(graph, src, dst, n): @@ -239,8 +249,8 @@ 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 draw_rag(labels, rag, img, border_color=None, node_color='yellow', + edge_color='green', colormap=None, thresh=np.inf): """Draw a Region Adjacency Graph on an image. Given a labelled image and its corresponding RAG, draw the nodes and edges @@ -249,30 +259,24 @@ def rag_draw(labels, rag, img, border_color=(0, 0, 0), node_color = (1, 1, 0), Parameters ---------- - labels : ndarray, shape(M, N, [..., P,]) - The labelled image. This should have one dimension less than - `img`. If `img` has dimensions `(M, N, 3)` `labels` should have - dimensions `(M, N)`. + labels : ndarray, shape(M, N) + The labelled image. rag : RAG The Region Adjacency Graph. - img : ndarray, shape(M, N, [..., P,] 3) + img : ndarray, shape(M, N, 3) Input image. - border_color : length-3 sequence, optional - RGB color of the border of regions. Specifying `None` won't draw - the border. Black by default. - node_color : length-3 sequeunce, optional - RGB color of the centroid of nodes. Yellow by default. - low_color : length-3 sequeunce, optional - RGB color of the edges. If `high_color` is not specified, all edges - are draw with `low_color`. Green by default. - high_color : length-3 sequeunce, optional - RGB color of the edges with high weight. If specified, the edges are - color mapped between `low_color` and `high_color` depending on their - weight. Edges with low weights are more like `low_color` whereas edges - with high weights are more like `high_color`. - thresh : float, optiona; + border_color : colorspec, optional + Any matplotlib colorspec. + node_color : colorspec, optional + Any matplotlib colorspec. Yellow by default. + edge_color : colorspec, optional + Any matplotlib colorspec. Green by default. + colormap : colormap, optional + Any matplotlib colormap. If specified the edges are colormapped with + the specified color map. + thresh : float, optional Edges with weight below `thresh` are not drawn, or considered for color - mapping in case `high_color` is specified. + mapping. Returns ------- @@ -282,41 +286,44 @@ def rag_draw(labels, rag, img, border_color=(0, 0, 0), node_color = (1, 1, 0), Examples -------- >>> from skimage import data, graph, segmentation - >>> img = data.lena() + >>> img = data.coffee() >>> labels = segmentation.slic(img) >>> g = graph.rag_mean_color(img, labels) >>> out = graph.rag_draw(labels, g, img) """ rag = rag.copy() - rag_labels = labels.copy() - out = img.copy() + out = util.img_as_float(img) + cc = colors.ColorConverter() - low_color = np.array(low_color) - - if not high_color is None: - high_color = np.array(high_color) + edge_color = cc.to_rgb(edge_color) + node_color = cc.to_rgb(node_color) # Handling the case where one node has multiple labels # offset is 1 so that regionprops does not ignore 0 offset = 1 + map_array = np.arange(labels.max() + 1) for n, d in rag.nodes_iter(data=True): - for l in d['labels']: - rag_labels[labels == l] = offset + for label in d['labels']: + map_array[label] = offset offset += 1 + rag_labels = map_array[labels] + regions = measure.regionprops(rag_labels) for region in regions: # Because we kept the offset as 1 rag.node[region['label'] - 1]['centroid'] = region['centroid'] if not border_color is None: + border_color = cc.to_rgb(border_color) 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]) + if colormap is not None: + edge_weight_list = [d['weight'] for x, y, d in + rag.edges_iter(data=True) if d['weight'] < thresh] + norm = colors.Normalize() + norm.autoscale(edge_weight_list) + smap = cm.ScalarMappable(norm, colormap) for n1, n2, data in rag.edges_iter(data=True): @@ -327,13 +334,11 @@ def rag_draw(labels, rag, img, border_color=(0, 0, 0), node_color = (1, 1, 0), 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) + if colormap is not None: + current_color = smap.to_rgba([data['weight']])[0][:-1] + out[line] = current_color else: - out[line] = low_color + out[line] = edge_color circle = draw.circle(r1, c1, 2) out[circle] = node_color From 95b20adee7ab33880163ffa81d51bd7f836a33e0 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Tue, 5 Aug 2014 23:07:39 +0530 Subject: [PATCH 07/21] None comparison --- skimage/graph/rag.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index 4447c4e3..f3c22f83 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -314,7 +314,7 @@ def draw_rag(labels, rag, img, border_color=None, node_color='yellow', # Because we kept the offset as 1 rag.node[region['label'] - 1]['centroid'] = region['centroid'] - if not border_color is None: + if border_color is not None: border_color = cc.to_rgb(border_color) out = segmentation.mark_boundaries(out, rag_labels, color=border_color) From adeb8689afaeea1852cb3d0dd9ccf76c0ccb509a Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Tue, 5 Aug 2014 23:43:07 +0530 Subject: [PATCH 08/21] Fixed doctest --- skimage/graph/rag.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index f3c22f83..7e87a784 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -289,7 +289,7 @@ def draw_rag(labels, rag, img, border_color=None, node_color='yellow', >>> img = data.coffee() >>> labels = segmentation.slic(img) >>> g = graph.rag_mean_color(img, labels) - >>> out = graph.rag_draw(labels, g, img) + >>> out = graph.draw_rag(labels, g, img) """ rag = rag.copy() out = util.img_as_float(img) From 5bd55071bc28a9ff402e966b976ef7d379c4e89d Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Wed, 6 Aug 2014 00:32:47 +0530 Subject: [PATCH 09/21] handled matplotlib not present case --- skimage/graph/rag.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index 7e87a784..e3849184 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -13,17 +13,14 @@ except ImportError: import numpy as np from scipy.ndimage import filters from scipy import ndimage as nd -<<<<<<< HEAD import math -from .. import draw, measure, segmentation -======= -from .. import draw -from .. import measure -from .. import segmentation -from matplotlib import colors -from matplotlib import cm -from .. import util ->>>>>>> rebase and change API to support mpl colorspec +from .. import draw, measure, segmentation, util +try: + from matplotlib import colors + from matplotlib import cm +except ImportError: + pass + def min_weight(graph, src, dst, n): From bcbfdb28f59170c2c96aea9444c093e9556f6df2 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Thu, 14 Aug 2014 01:20:55 +0530 Subject: [PATCH 10/21] add desaturate option --- doc/examples/plot_rag_draw.py | 4 ++-- skimage/graph/rag.py | 38 ++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/doc/examples/plot_rag_draw.py b/doc/examples/plot_rag_draw.py index 21dbbc28..61d7e134 100644 --- a/doc/examples/plot_rag_draw.py +++ b/doc/examples/plot_rag_draw.py @@ -19,8 +19,8 @@ plt.figure() plt.title("RAG with all edges shown in green.") plt.imshow(out) -cmap = colors.ListedColormap(['cyan', 'red']) -out = graph.draw_rag(labels, g, img, colormap=cmap, thresh=30) +cmap = colors.ListedColormap(['#00ff00', '#ff0000']) +out = graph.draw_rag(labels, g, img,colormap=cmap, thresh=30, desaturate=True) plt.figure() plt.title("RAG with edge weights less than 30, color " "mapped between green and red.") diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index e3849184..e1ab79ca 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -14,7 +14,7 @@ import numpy as np from scipy.ndimage import filters from scipy import ndimage as nd import math -from .. import draw, measure, segmentation, util +from .. import draw, measure, segmentation, util, color try: from matplotlib import colors from matplotlib import cm @@ -246,8 +246,9 @@ def rag_mean_color(image, labels, connectivity=2, mode='distance', return graph -def draw_rag(labels, rag, img, border_color=None, node_color='yellow', - edge_color='green', colormap=None, thresh=np.inf): +def draw_rag(labels, rag, img, border_color=None, node_color='#ffff00', + edge_color='#00ff00', colormap=None, thresh=np.inf, + desaturate=False, in_place=True): """Draw a Region Adjacency Graph on an image. Given a labelled image and its corresponding RAG, draw the nodes and edges @@ -256,11 +257,11 @@ def draw_rag(labels, rag, img, border_color=None, node_color='yellow', Parameters ---------- - labels : ndarray, shape(M, N) + labels : ndarray, shape (M, N) The labelled image. rag : RAG The Region Adjacency Graph. - img : ndarray, shape(M, N, 3) + img : ndarray, shape (M, N, 3) Input image. border_color : colorspec, optional Any matplotlib colorspec. @@ -274,10 +275,16 @@ def draw_rag(labels, rag, img, border_color=None, node_color='yellow', thresh : float, optional Edges with weight below `thresh` are not drawn, or considered for color mapping. + desaturate : bool, optional + Convert the image to grayscale before displaying. Particularly helps + visualiztion when using the `colormap` option. + in_place : bool, optional + If set, the RAG is modified in place. For each node `n` the function + will set a new attribute ``rag.node[n]['centroid']``. Returns ------- - out : ndarray, shape(M, N, [..., P,] 3) + out : ndarray, shape (M, N, 3) The image with the RAG drawn. Examples @@ -288,7 +295,13 @@ def draw_rag(labels, rag, img, border_color=None, node_color='yellow', >>> g = graph.rag_mean_color(img, labels) >>> out = graph.draw_rag(labels, g, img) """ - rag = rag.copy() + if not in_place: + rag = rag.copy() + + if desaturate: + img = color.rgb2gray(img) + img = color.gray2rgb(img) + out = util.img_as_float(img) cc = colors.ColorConverter() @@ -305,11 +318,10 @@ def draw_rag(labels, rag, img, border_color=None, node_color='yellow', offset += 1 rag_labels = map_array[labels] - regions = measure.regionprops(rag_labels) - for region in regions: - # Because we kept the offset as 1 - rag.node[region['label'] - 1]['centroid'] = region['centroid'] + + for (n, data), region in zip(rag.nodes_iter(data=True), regions): + data['centroid'] = region['centroid'] if border_color is not None: border_color = cc.to_rgb(border_color) @@ -328,12 +340,10 @@ def draw_rag(labels, rag, img, border_color=None, node_color='yellow', continue r1, c1 = map(int, rag.node[n1]['centroid']) r2, c2 = map(int, rag.node[n2]['centroid']) - line = draw.line(r1, c1, r2, c2) if colormap is not None: - current_color = smap.to_rgba([data['weight']])[0][:-1] - out[line] = current_color + out[line] = smap.to_rgba([data['weight']])[0][:-1] else: out[line] = edge_color From 3553809c55cb9042f197cfee1fdd89b8fafc5da3 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Thu, 14 Aug 2014 01:24:02 +0530 Subject: [PATCH 11/21] pep8 --- doc/examples/plot_rag_draw.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/examples/plot_rag_draw.py b/doc/examples/plot_rag_draw.py index 61d7e134..9c9d68b0 100644 --- a/doc/examples/plot_rag_draw.py +++ b/doc/examples/plot_rag_draw.py @@ -20,7 +20,7 @@ plt.title("RAG with all edges shown in green.") plt.imshow(out) cmap = colors.ListedColormap(['#00ff00', '#ff0000']) -out = graph.draw_rag(labels, g, img,colormap=cmap, thresh=30, desaturate=True) +out = graph.draw_rag(labels, g, img, colormap=cmap, thresh=30, desaturate=True) plt.figure() plt.title("RAG with edge weights less than 30, color " "mapped between green and red.") From 5c5b60df4cde95d1115c8c303ca9f48fe708f169 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Fri, 15 Aug 2014 02:32:23 +0530 Subject: [PATCH 12/21] Chnaged palette in example --- doc/examples/plot_rag_draw.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/examples/plot_rag_draw.py b/doc/examples/plot_rag_draw.py index 9c9d68b0..bb8042d8 100644 --- a/doc/examples/plot_rag_draw.py +++ b/doc/examples/plot_rag_draw.py @@ -19,8 +19,11 @@ plt.figure() plt.title("RAG with all edges shown in green.") plt.imshow(out) -cmap = colors.ListedColormap(['#00ff00', '#ff0000']) -out = graph.draw_rag(labels, g, img, colormap=cmap, thresh=30, desaturate=True) +# The color palette used was taken from +# http://www.colorcombos.com/color-schemes/2/ColorCombo2.html +cmap = colors.ListedColormap(['#6599FF', '#ff9900']) +out = graph.draw_rag(labels, g, img, node_color="#ffde00", colormap=cmap, + thresh=30, desaturate=True) plt.figure() plt.title("RAG with edge weights less than 30, color " "mapped between green and red.") From cfaa83cf26f3d12f0454b1a62e41fabc0e76a63b Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Fri, 15 Aug 2014 02:33:09 +0530 Subject: [PATCH 13/21] Chnaged palette in example --- doc/examples/plot_rag_draw.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/examples/plot_rag_draw.py b/doc/examples/plot_rag_draw.py index bb8042d8..49bdf42c 100644 --- a/doc/examples/plot_rag_draw.py +++ b/doc/examples/plot_rag_draw.py @@ -18,6 +18,7 @@ out = graph.draw_rag(labels, g, img) plt.figure() plt.title("RAG with all edges shown in green.") plt.imshow(out) +io.imsave("/home/vighnesh/Desktop/1.png") # The color palette used was taken from # http://www.colorcombos.com/color-schemes/2/ColorCombo2.html @@ -28,5 +29,6 @@ plt.figure() plt.title("RAG with edge weights less than 30, color " "mapped between green and red.") +io.imsave("/home/vighnesh/Desktop/2.png") plt.imshow(out) plt.show() From 82f158482e38e519cdb9af776e7937c23dbbe67f Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Fri, 15 Aug 2014 02:35:28 +0530 Subject: [PATCH 14/21] removed file save --- doc/examples/plot_rag_draw.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/doc/examples/plot_rag_draw.py b/doc/examples/plot_rag_draw.py index 49bdf42c..bb8042d8 100644 --- a/doc/examples/plot_rag_draw.py +++ b/doc/examples/plot_rag_draw.py @@ -18,7 +18,6 @@ out = graph.draw_rag(labels, g, img) plt.figure() plt.title("RAG with all edges shown in green.") plt.imshow(out) -io.imsave("/home/vighnesh/Desktop/1.png") # The color palette used was taken from # http://www.colorcombos.com/color-schemes/2/ColorCombo2.html @@ -29,6 +28,5 @@ plt.figure() plt.title("RAG with edge weights less than 30, color " "mapped between green and red.") -io.imsave("/home/vighnesh/Desktop/2.png") plt.imshow(out) plt.show() From e42c0652434535459d3f4c903d7dd324091c8090 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Fri, 15 Aug 2014 02:37:59 +0530 Subject: [PATCH 15/21] corrected title --- doc/examples/plot_rag_draw.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/examples/plot_rag_draw.py b/doc/examples/plot_rag_draw.py index bb8042d8..2572a3fb 100644 --- a/doc/examples/plot_rag_draw.py +++ b/doc/examples/plot_rag_draw.py @@ -26,7 +26,7 @@ out = graph.draw_rag(labels, g, img, node_color="#ffde00", colormap=cmap, thresh=30, desaturate=True) plt.figure() plt.title("RAG with edge weights less than 30, color " - "mapped between green and red.") + "mapped between blue and orange.") plt.imshow(out) plt.show() From 7730eb004e68ffe382cf3a57ae0ca26f4ec618ef Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Fri, 15 Aug 2014 14:13:36 +0530 Subject: [PATCH 16/21] fixed init --- skimage/graph/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/skimage/graph/__init__.py b/skimage/graph/__init__.py index 2cd422ba..29bcb031 100644 --- a/skimage/graph/__init__.py +++ b/skimage/graph/__init__.py @@ -2,7 +2,6 @@ from .spath import shortest_path from .mcp import MCP, MCP_Geometric, MCP_Connect, MCP_Flexible, route_through_array from .rag import rag_mean_color, RAG from .graph_cut import cut_threshold, cut_normalized -from .rag import rag_mean_color, RAG, rag_draw from .rag import rag_mean_color, RAG, draw_rag from .graph_cut import cut_threshold ncut = cut_normalized @@ -18,6 +17,5 @@ __all__ = ['shortest_path', 'cut_threshold', 'cut_normalized', 'ncut', - 'rag_draw', 'draw_rag', 'RAG'] From 4bb4076959b0c6e9f6986dbc7ef4381eb7bc21de Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Fri, 15 Aug 2014 14:15:02 +0530 Subject: [PATCH 17/21] fixed imports --- skimage/graph/__init__.py | 2 -- skimage/graph/rag.py | 1 - 2 files changed, 3 deletions(-) diff --git a/skimage/graph/__init__.py b/skimage/graph/__init__.py index 29bcb031..6da4fcc8 100644 --- a/skimage/graph/__init__.py +++ b/skimage/graph/__init__.py @@ -1,9 +1,7 @@ from .spath import shortest_path from .mcp import MCP, MCP_Geometric, MCP_Connect, MCP_Flexible, route_through_array -from .rag import rag_mean_color, RAG from .graph_cut import cut_threshold, cut_normalized from .rag import rag_mean_color, RAG, draw_rag -from .graph_cut import cut_threshold ncut = cut_normalized diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index e1ab79ca..74e4ab07 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -22,7 +22,6 @@ except ImportError: pass - def min_weight(graph, src, dst, n): """Callback to handle merging nodes by choosing minimum weight. From 6e09628fb909110bf1b79f333a411b4a02a5b57f Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Fri, 15 Aug 2014 14:37:14 +0530 Subject: [PATCH 18/21] update relase doc --- doc/release/release_dev.txt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/release/release_dev.txt b/doc/release/release_dev.txt index 1123eb77..cbeded27 100644 --- a/doc/release/release_dev.txt +++ b/doc/release/release_dev.txt @@ -14,9 +14,12 @@ http://scikit-image.org New Features ------------ - - - +Region Adjacency Graphs + - Color distance RAGs (#1031) + - Threshold Cut on RAGs (#1031) + - Similarity RAGs (#1080) + - Normalized Cut on RAGs (#1080) + - RAG Drawing (#1087) Improvements ------------ From c43180fa27b0add7a0ad71d9e159efdd156fac06 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Fri, 15 Aug 2014 17:02:35 +0530 Subject: [PATCH 19/21] force copy --- skimage/graph/rag.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index 74e4ab07..edd9ecde 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -301,7 +301,7 @@ def draw_rag(labels, rag, img, border_color=None, node_color='#ffff00', img = color.rgb2gray(img) img = color.gray2rgb(img) - out = util.img_as_float(img) + out = util.img_as_float(img, force_copy=True) cc = colors.ColorConverter() edge_color = cc.to_rgb(edge_color) From 344e2a51c0cd4ec79843805393514402f1a18df9 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Fri, 15 Aug 2014 17:52:25 +0530 Subject: [PATCH 20/21] cast labels in ncut --- skimage/graph/graph_cut.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/graph/graph_cut.py b/skimage/graph/graph_cut.py index 3f973e22..10ea77c4 100644 --- a/skimage/graph/graph_cut.py +++ b/skimage/graph/graph_cut.py @@ -120,7 +120,7 @@ def cut_normalized(labels, rag, thresh=0.001, num_cuts=10, in_place=True): _ncut_relabel(rag, thresh, num_cuts) - map_array = np.zeros(labels.max() + 1) + map_array = np.zeros(labels.max() + 1, dtype=labels.dtype) # Mapping from old labels to new for n, d in rag.nodes_iter(data=True): map_array[d['labels']] = d['ncut label'] From 3f7ce2e670b1be9c9f79ed8c7b51172c682c46b1 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Fri, 15 Aug 2014 21:28:47 +0530 Subject: [PATCH 21/21] Added example with cubehelix --- doc/examples/plot_rag_draw.py | 7 +++++++ skimage/graph/rag.py | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/doc/examples/plot_rag_draw.py b/doc/examples/plot_rag_draw.py index 2572a3fb..abf2ba08 100644 --- a/doc/examples/plot_rag_draw.py +++ b/doc/examples/plot_rag_draw.py @@ -27,6 +27,13 @@ out = graph.draw_rag(labels, g, img, node_color="#ffde00", colormap=cmap, plt.figure() plt.title("RAG with edge weights less than 30, color " "mapped between blue and orange.") +plt.imshow(out) + +plt.figure() +plt.title("All edges drawn with cubehelix colormap") +cmap = plt.get_cmap('cubehelix') +out = graph.draw_rag(labels, g, img, colormap=cmap, + desaturate=True) plt.imshow(out) plt.show() diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index edd9ecde..edaec4b3 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -251,8 +251,8 @@ def draw_rag(labels, rag, img, border_color=None, node_color='#ffff00', """Draw a Region Adjacency Graph on an image. Given a labelled image and its corresponding RAG, draw the nodes and edges - of the RAG on the image with the specified colors. Nodes are markes by - the centroids of the corresposning regions. + of the RAG on the image with the specified colors. Nodes are marked by + the centroids of the corresponding regions. Parameters ---------- @@ -276,7 +276,7 @@ def draw_rag(labels, rag, img, border_color=None, node_color='#ffff00', mapping. desaturate : bool, optional Convert the image to grayscale before displaying. Particularly helps - visualiztion when using the `colormap` option. + visualization when using the `colormap` option. in_place : bool, optional If set, the RAG is modified in place. For each node `n` the function will set a new attribute ``rag.node[n]['centroid']``.