mirror of
https://github.com/wassname/scikit-image.git
synced 2026-08-15 12:54:54 +08:00
Added example for region boundary merging
This commit is contained in:
@@ -1,36 +1,78 @@
|
||||
from skimage import data, io, segmentation, color
|
||||
"""
|
||||
============================================
|
||||
Hierarchical Merging of Region Boundary RAGs
|
||||
============================================
|
||||
|
||||
TODO: Description
|
||||
"""
|
||||
|
||||
from skimage import data, segmentation, filters, color
|
||||
from skimage.future import graph
|
||||
import numpy as np
|
||||
from matplotlib import pyplot as plt
|
||||
|
||||
|
||||
def weight_boundary(graph, src, dst, n):
|
||||
if graph.has_edge(src, n) and graph.has_edge(dst, n):
|
||||
count_src = graph[src][n]['count']
|
||||
count_dst = graph[dst][n]['count']
|
||||
"""
|
||||
Callback to handle merging of nodes of a region boundary RAG.
|
||||
|
||||
weight_src = graph[src][n]['weight']
|
||||
weight_dst = graph[dst][n]['weight']
|
||||
This function computes the `"weight"` and the count `"count"`
|
||||
attributes of the edge between `n` and the node formed after
|
||||
merging `src` and `dst`.
|
||||
|
||||
count = count_src + count_dst
|
||||
return {
|
||||
'count': count,
|
||||
'weight': (count_src*weight_src + count_dst*weight_dst)/count
|
||||
}
|
||||
|
||||
elif graph.has_edge(src, n):
|
||||
return graph[src][n]
|
||||
elif graph.has_edge(dst, n):
|
||||
return graph[dst][n]
|
||||
Parameters
|
||||
----------
|
||||
graph : RAG
|
||||
The graph under consideration.
|
||||
src, dst : int
|
||||
The vertices in `graph` to be merged.
|
||||
n : int
|
||||
A neighbor of `src` or `dst` or both.
|
||||
|
||||
Returns
|
||||
-------
|
||||
data : dict
|
||||
A dictionary with the `"weight"` and `"count"` attributes to be
|
||||
assigned for the merged node.
|
||||
|
||||
"""
|
||||
default = {'weight': 0.0, 'count': 0}
|
||||
|
||||
count_src = graph[src].get(n, default)['count']
|
||||
count_dst = graph[dst].get(n, default)['count']
|
||||
|
||||
weight_src = graph[src].get(n, default)['weight']
|
||||
weight_dst = graph[dst].get(n, default)['weight']
|
||||
|
||||
count = count_src + count_dst
|
||||
return {
|
||||
'count': count,
|
||||
'weight': (count_src*weight_src + count_dst*weight_dst)/count
|
||||
}
|
||||
|
||||
|
||||
def merge_boundary(graph, src, dst):
|
||||
pass
|
||||
|
||||
img = data.coffee()
|
||||
edges = filters.sobel(color.rgb2gray(img))
|
||||
labels = segmentation.slic(img, compactness=30, n_segments=400)
|
||||
g = graph.rag_mean_color(img, labels)
|
||||
g = graph.rag_boundary(labels, edges)
|
||||
|
||||
labels2 = graph.merge_hierarchical(labels, g, thresh=40, rag_copy=False,
|
||||
graph.show_rag(labels, g, img)
|
||||
plt.title('Initial RAG')
|
||||
|
||||
labels2 = graph.merge_hierarchical(labels, g, thresh=0.08, rag_copy=False,
|
||||
in_place_merge=True,
|
||||
merge_func=merge_boundary,
|
||||
weight_func=weight_boundary)
|
||||
|
||||
graph.show_rag(labels, g, img)
|
||||
plt.title('RAG after hierarchical merging')
|
||||
|
||||
plt.figure()
|
||||
out = color.label2rgb(labels2, img, kind='avg')
|
||||
plt.imshow(out)
|
||||
plt.title('Final segmentation')
|
||||
|
||||
plt.show()
|
||||
|
||||
Reference in New Issue
Block a user