区域邻接图Region Adjacency Graphs (RAGs)
下面记录下用rag解决分割问题的过程
用图:
from skimage import graph, data, io, segmentation, color
from matplotlib import pyplot as plt
from skimage.measure import regionprops
from skimage import draw
import numpy as np
def show_img(img):
width = 10.0
height = img.shape[0]*width/img.shape[1]
f = plt.figure(figsize=(width, height))
plt.imshow(img)
def _weight_mean_color(graph, src, dst, n):
"""Callback to handle merging nodes by recomputing mean color.
The method expects that the mean color of `dst` is already computed.
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"` attribute set as the absolute
difference of the mean color between node `dst` and `n`.
"""
diff = graph.nodes[dst]['mean color'] - graph.nodes[n]['mean color']
diff = np.linalg.norm(diff)
return {'weight': diff}
def merge_mean_color(graph, src, dst):
"""Callback called before merging two nodes of a mean color distance graph.
This method computes the mean color of `dst`.
Parameters
----------
graph : RAG
The graph under consideration.
src, dst : int
The vertices in `graph` to be merged.
"""
graph.nodes[dst]['total color'] += graph.nodes[src]['total color']
graph.nodes[dst]['pixel count'] += graph.nodes[src]['pixel count']
graph.nodes[dst]['mean color'] = (graph.nodes[dst]['total color'] /
graph.nodes[dst]['pixel count'])
if __name__ == "__main__":
img_path = "./sample.jpg"
img = io.imread(img_path)
# show_img(img)
labels = segmentation.slic(img, compactness=30, n_segments=400) #Using SLIC to segment pic 先用SLIC分割图像
labels = labels + 1 # So that no labelled region is 0 and ignored by regionprops
regions = regionprops(labels) #regionprops helps us compute various features of these regions. 计算每块的多种特征,用于后面按照特征合并用
label_rgb = color.label2rgb(labels, img, kind='avg') #The label2rgb function assigns a specific color to all pixels belonging to one region (having the same label).给属于同一类型的像素一样的颜色
label_rgb = segmentation.mark_boundaries(label_rgb, labels, (0, 0, 0)) #把分割线画出来
io.imsave('./1.jpg',label_rgb)
g = graph.rag_mean_color(img, labels) #计算每个小图块的权重,两个图块越相似权重越接近,这里用图块的像素均值作为每个图块的权重值
labels2 = graph.merge_hierarchical(labels, g, thresh=35,
rag_copy=False,
in_place_merge=True,
merge_func=merge_mean_color,
weight_func=_weight_mean_color)#合并权重相近的图块
label_rgb2 = color.label2rgb(labels2, img, kind='avg')
label_rgb2 = segmentation.mark_boundaries(label_rgb2, labels2, (0, 0, 0))
io.imsave('./2.jpg',label_rgb2)
上面中间过程对应的图片分别是:
1.jpg
2.jpg