分割后图像块合并方法之区域邻接图

区域邻接图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
1.jpg
2.jpg
区块合并

评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

如雾如电

随缘

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值