mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-20 12:40:31 +08:00
37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
from skimage import data, io, segmentation, color
|
|
from skimage.future import graph
|
|
import numpy as np
|
|
|
|
|
|
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']
|
|
|
|
weight_src = graph[src][n]['weight']
|
|
weight_dst = graph[dst][n]['weight']
|
|
|
|
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]
|
|
|
|
|
|
def merge_boundary(graph, src, dst):
|
|
pass
|
|
|
|
img = data.coffee()
|
|
labels = segmentation.slic(img, compactness=30, n_segments=400)
|
|
g = graph.rag_mean_color(img, labels)
|
|
|
|
labels2 = graph.merge_hierarchical(labels, g, thresh=40, rag_copy=False,
|
|
in_place_merge=True,
|
|
merge_func=merge_boundary,
|
|
weight_func=weight_boundary)
|