首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建一个自定义skimage.future.graph.rag,作为cut_normalized和ncut的输入?

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

Stack Overflow用户
提问于 2018-11-25 04:43:03
回答 1查看 459关注 0票数 1

我试图用RAG创建一个自定义邻接图,但所有示例都只使用rag = graph.rag_mean_color(img, labels)创建了图形。

我不想使用这个函数,并且希望用我的自定义度量来定义权重。所以我写了下面的代码

代码语言:javascript
复制
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)

..。

代码语言:javascript
复制
 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))

但是,当使用这个图进行归一化切割时,我的输出是错误的。

代码语言:javascript
复制
labels3 = graph.cut_normalized(labels1, g_seg,5.0,10)

因此,我的理解是,在创建图形的同时,我正在破坏图像的特殊结构,因为我将节点作为一个一维数组,丢弃它们的2D坐标。因此,我需要帮助理解如何创建一个图形,使图像的2D结构保持不变,并给出我们使用rag = graph.rag_mean_color(img, labels)获得的结果。

EN

回答 1

Stack Overflow用户

发布于 2019-03-10 14:54:12

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

代码语言:javascript
复制
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.The的源代码,上面的代码是基于此的。颜色源代码

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53464711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档