如何创建自定义的skimage.future.graph.rag作为cut_normalized和ncut的输入?

问题描述 投票:1回答:1

我正在尝试使用RAG创建自定义邻接图,但所有示例仅使用rag = graph.rag_mean_color(img, labels)创建图形

我不想使用此功能,并希望使用我的自定义度量定义权重。所以我写了下面的代码

labels1 = segmentation.slic(img_i.reshape(img.shape[0],img.shape[1]), compactness=30, n_segments=200)
out1 = color.label2rgb(labels1, img_i.reshape(img.shape[0],img.shape[1]), kind='avg')
plt.axis('off')
plt.imshow(out1)
print(labels1.shape)

enter image description here

...

 g_seg = graph.rag.RAG() 
    for ix in range(0,img.shape[0]):
        for iy in range(0,img.shape[1]):
            idx = ix*img.shape[1] + iy
            g_seg.add_node(idx,labels=[labels_slic[idx]]) 
    win_rad = 7 
    for i in range(0,img.shape[0]):
        for j in range(0,img.shape[1]):
            for ii in range(-int(win_rad),int(win_rad)):
                for jj in range(-int(win_rad),int(win_rad)):
                    if i+ii>0 and i+ii<img.shape[0] and j+jj>0 and j+jj<img.shape[1]: 
                        idx = i*img.shape[1] + j
                        idc = (i+ii)*img.shape[1] + (j+jj)
                        w_tx = g_tx[idx][idc]['weight']
                        w_ic = g_ic[idx][idc]['weight']
                        g_seg.add_edge(idx, idc, weight=(w_tx*w_ic))

但是当使用此图表进行标准化切割时,输出错误

labels3 = graph.cut_normalized(labels1, g_seg,5.0,10)

enter image description here

所以我的理解是,在创建图形时我正在破坏图像的特殊结构,因为我将节点作为一维阵列丢弃它们的2D坐标。所以我需要帮助来理解如何创建一个图形,保持图像的二维结构完好无损,并给出我们用rag = graph.rag_mean_color(img, labels)得到的结果

python-3.x graph scikit-image
1个回答
0
投票

您可以在相邻节点之间使用自己的自定义权重创建自己的RAG版本,如下所示:

from skimage.future.graph import RAG
import numpy as np

def rag(image, labels):
   #initialize the RAG
   graph = RAG(labels, connectivity=2)

   #lets say we want for each node on the graph a label, a pixel count and a total color 
   for n in graph:
       graph.node[n].update({'labels': [n],'pixel count': 0,
                             'total color': np.array([0, 0, 0],
                             dtype=np.double)})
   #give them values
   for index in np.ndindex(labels.shape):
       current = labels[index]
       graph.node[current]['pixel count'] += 1
       graph.node[current]['total color'] += image[index]

   #calculate your own weights here
   for x, y, d in graph.edges(data=True):
       my_weight = "do whatever"
       d['weight'] = my_weight

   return graph
  • 图像:您的输入图像
  • 标签:图像每个像素的标签

您还应该查看graph.rag_mean_color的源代码。上面的代码基于此。 rag_mean_color source code

© www.soinside.com 2019 - 2024. All rights reserved.