Skip to content

Commit

Permalink
Merge 8c396a1 into 401c1fd
Browse files Browse the repository at this point in the history
  • Loading branch information
vighneshbirodkar committed Mar 16, 2016
2 parents 401c1fd + 8c396a1 commit 8743ed5
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 83 deletions.
7 changes: 3 additions & 4 deletions doc/examples/segmentation/plot_rag_boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from skimage.future import graph
from skimage import data, segmentation, color, filters, io
from skimage.util.colormap import viridis
from matplotlib import pyplot as plt


img = data.coffee()
Expand All @@ -19,9 +20,7 @@
edges_rgb = color.gray2rgb(edges)

g = graph.rag_boundary(labels, edges)
graph.show_rag(labels, g, edges_rgb, img_cmap=None, edge_cmap='viridis',
edge_width=1.2)

out = graph.draw_rag(labels, g, edges_rgb, node_color="#999999",
colormap=viridis)

io.imshow(out)
io.show()
27 changes: 7 additions & 20 deletions doc/examples/segmentation/plot_rag_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,20 @@
"""
from skimage import data, segmentation
from skimage.future import graph
from skimage.util.colormap import viridis
from matplotlib import pyplot as plt, colors
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.draw_rag(labels, g, img)
plt.figure()
plt.title("RAG with all edges shown in green.")
plt.imshow(out)

# 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 blue and orange.")
plt.imshow(out)
fig, ax = plt.subplots()
ax.set_title('RAG drawn will all edges')
graph.show_rag(labels, g, img, ax=ax)

plt.figure()
plt.title("All edges drawn with viridis colormap")
out = graph.draw_rag(labels, g, img, colormap=viridis,
desaturate=True)
fig, ax = plt.subplots()
ax.set_title('RAG drawn with edges having weight less than 30')
graph.show_rag(labels, g, img, thresh=30, ax=ax)

plt.imshow(out)
plt.show()
4 changes: 2 additions & 2 deletions skimage/future/graph/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from .graph_cut import cut_threshold, cut_normalized
from .rag import rag_mean_color, RAG, draw_rag, rag_boundary
from .rag import rag_mean_color, RAG, show_rag, rag_boundary
from .graph_merge import merge_hierarchical
ncut = cut_normalized

__all__ = ['rag_mean_color',
'cut_threshold',
'cut_normalized',
'ncut',
'draw_rag',
'show_rag',
'merge_hierarchical',
'rag_boundary',
'RAG']
110 changes: 53 additions & 57 deletions skimage/future/graph/rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
from scipy import ndimage as ndi
from scipy import sparse
import math
from ... import draw, measure, segmentation, util, color
try:
from matplotlib import colors
from matplotlib import cm
except ImportError:
pass
from ... import measure, segmentation, util, color
from matplotlib import colors, cm
from matplotlib import pyplot as plt


def _edge_generator_from_csr(csr_matrix):
Expand Down Expand Up @@ -420,14 +417,14 @@ def rag_boundary(labels, edge_map, connectivity=2):
return rag


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):
def show_rag(labels, rag, img, border_color='black', edge_width=1.5,
edge_cmap='magma', img_cmap='bone', thresh=np.inf, in_place=True,
ax=None):
"""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 marked by
the centroids of the corresponding regions.
of the RAG on the image with the specified colors. Edges are drawn between
the centroid of the 2 adjacent regions in the image.
Parameters
----------
Expand All @@ -438,28 +435,22 @@ def draw_rag(labels, rag, img, border_color=None, node_color='#ffff00',
img : ndarray, shape (M, N, 3)
Input image.
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.
Color with which the borders of regions are drawn.
edge_width : float, optional
The thickness with which the RAG edges are drawn.
edge_cmap : colormap, optional
Any matplotlib colormap with which the edges are drawn.
img_cmap : colormap, optional
Any matplotlib colormap with which the image is draw. If set to `None`
the image is drawn as it is.
thresh : float, optional
Edges with weight below `thresh` are not drawn, or considered for color
Edges with weight above `thresh` are not drawn, or considered for color
mapping.
desaturate : bool, optional
Convert the image to grayscale before displaying. Particularly helps
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']``.
Returns
-------
out : ndarray, shape (M, N, 3)
The image with the RAG drawn.
ax : matplotlib axes, optional
The axes to draw on.
Examples
--------
Expand All @@ -468,20 +459,31 @@ def draw_rag(labels, rag, img, border_color=None, node_color='#ffff00',
>>> img = data.coffee()
>>> labels = segmentation.slic(img)
>>> g = graph.rag_mean_color(img, labels)
>>> out = graph.draw_rag(labels, g, img)
>>> graph.show_rag(labels, g, img)
"""

if not in_place:
rag = rag.copy()

if desaturate:
img = color.rgb2gray(img)
img = color.gray2rgb(img)

if ax is None:
fix, ax = plt.subplots()
out = util.img_as_float(img, force_copy=True)
cc = colors.ColorConverter()

edge_color = cc.to_rgb(edge_color)
node_color = cc.to_rgb(node_color)
if img_cmap is None:
if img.ndim < 3:
raise ValueError('The image needs to be RGB')
if img.ndim < 4:
r, c, _ = img.shape
new_img = np.empty((r, c, 4), dtype=np.float)
new_img[:, :, 0:3] = img
new_img[:, :, 3] = 1
out = new_img
else:
img_cmap = cm.get_cmap(img_cmap)
out = color.rgb2gray(img)
out = img_cmap(out)

edge_cmap = cm.get_cmap(edge_cmap)

# Handling the case where one node has multiple labels
# offset is 1 so that regionprops does not ignore 0
Expand All @@ -498,31 +500,25 @@ def draw_rag(labels, rag, img, border_color=None, node_color='#ffff00',
for (n, data), region in zip(rag.nodes_iter(data=True), regions):
data['centroid'] = region['centroid']

cc = colors.ColorConverter()
if border_color is not None:
border_color = cc.to_rgb(border_color)
border_color = cc.to_rgba(border_color)
out = segmentation.mark_boundaries(out, rag_labels, color=border_color)

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)
ax.imshow(out)

for n1, n2, data in rag.edges_iter(data=True):
edge_weight_list = [d['weight'] for x, y, d in
rag.edges_iter(data=True) if d['weight'] < thresh]

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)
norm = colors.Normalize()
norm.autoscale(edge_weight_list)
smap = cm.ScalarMappable(norm, edge_cmap)

if colormap is not None:
out[line] = smap.to_rgba([data['weight']])[0][:-1]
else:
out[line] = edge_color

circle = draw.circle(r1, c1, 2)
out[circle] = node_color
for n1, n2, data in rag.edges_iter(data=True):

return out
if data['weight'] < thresh:
r1, c1 = map(int, rag.node[n1]['centroid'])
r2, c2 = map(int, rag.node[n2]['centroid'])
col = smap.to_rgba([data['weight']])[0][:-1]
ax.add_artist(plt.Line2D([c1, c2], [r1, r2], color=col,
lw=edge_width))
Loading

0 comments on commit 8743ed5

Please sign in to comment.