working code

This commit is contained in:
Vighnesh Birodkar
2015-01-27 21:33:21 +05:30
parent b6a1bcdb06
commit b35fe6948f
3 changed files with 55 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
from skimage import graph, data, io, segmentation, color
img = data.coffee()
labels = segmentation.slic(img, compactness=30, n_segments=400)
g = graph.rag_mean_color(img, labels)
labels2 = graph.merge_hierarchical(g, labels, 40)
g2 = graph.rag_mean_color(img, labels2)
out = color.label2rgb(labels2, img, kind='avg')
out = segmentation.mark_boundaries(out, labels2, (0, 0, 0))
io.imshow(out)
io.show()
+5
View File
@@ -1,7 +1,11 @@
from .spath import shortest_path
from .mcp import MCP, MCP_Geometric, MCP_Connect, MCP_Flexible, route_through_array
from .graph_cut import cut_threshold, cut_normalized
<<<<<<< HEAD
from .rag import rag_mean_color, RAG, draw_rag
=======
from .graph_merge import merge_hierarchical
>>>>>>> working code
ncut = cut_normalized
@@ -16,4 +20,5 @@ __all__ = ['shortest_path',
'cut_normalized',
'ncut',
'draw_rag',
'merge_hierarchical',
'RAG']
+37
View File
@@ -0,0 +1,37 @@
import numpy as np
def _hmerge(rag, x, y, n):
diff = rag.node[y]['mean color'] - rag.node[n]['mean color']
diff = np.linalg.norm(diff)
return diff
def merge_hierarchical(rag, labels, thresh):
min_wt = 0
while min_wt < thresh:
valid_edges = ((x, y, d)
for x, y, d in rag.edges(data=True) if x != y)
x, y, d = min(valid_edges, key=lambda x: x[2]['weight'])
min_wt = d['weight']
if min_wt < thresh:
total_color = (rag.node[y]['total color'] +
rag.node[x]['total color'])
n_pixels = rag.node[x]['pixel count'] + rag.node[y]['pixel count']
rag.node[y]['total color'] = total_color
rag.node[y]['pixel count'] = n_pixels
rag.node[y]['mean color'] = total_color / n_pixels
rag.merge_nodes(x, y, _hmerge)
count = 0
arr = np.arange(labels.max() + 1)
for n, d in rag.nodes_iter(data=True):
for l in d['labels']:
arr[l] = count
count += 1
return arr[labels]