Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用图像创建图形';s像素?_Python_Image_Graph - Fatal编程技术网

Python 如何使用图像创建图形';s像素?

Python 如何使用图像创建图形';s像素?,python,image,graph,Python,Image,Graph,现在,我有一个图像,我想生成一个加权图G=(V,E),其中V是顶点集,E是边集(图像中的每个像素作为图中的一个节点) 但我不知道怎么做。 有人能帮我吗?最好使用python。 多谢各位 问题补充 很抱歉,我对这个问题的描述不够清楚 我的目标是使用图像的像素作为节点网络来建立网络,然后分析网络的性质来检测目标(可能) 但在第一步,我需要建立这个网络。我的问题是如何使用图像的像素(RGB)作为网络的节点来建立用于分析图像的网络 这些节点的边缘可能基于其某些特征(位置、外观等) 所以,我只想知道如何建

现在,我有一个图像,我想生成一个加权图G=(V,E),其中V是顶点集,E是边集(图像中的每个像素作为图中的一个节点)

但我不知道怎么做。 有人能帮我吗?最好使用python。 多谢各位

问题补充

很抱歉,我对这个问题的描述不够清楚

我的目标是使用图像的像素作为节点网络来建立网络,然后分析网络的性质来检测目标(可能)

但在第一步,我需要建立这个网络。我的问题是如何使用图像的像素(RGB)作为网络的节点来建立用于分析图像的网络

这些节点的边缘可能基于其某些特征(位置、外观等)

所以,我只想知道如何建立这个网络?
只是一些简单的例子。谢谢你

我也在寻找很好的矢量化答案,但没有找到。最后,我自己做了这件事。我的目的也是尽可能快地加速这些计算

让我们从这张漂亮的28 x 27图像开始:

import numpy as np
x, y = np.meshgrid(np.linspace(-np.pi/2, np.pi/2, 30), np.linspace(-np.pi/2, np.pi/2, 30))
image = (np.sin(x**2+y**2)[1:-1,1:-2] > 0.9).astype(int) #boolean image
image

 [[0 0 0 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1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0]
 [0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0]
 [0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0]
 [0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0]
 [0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0]
 [0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0]
 [0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
Networkx 该算法的一个基本原理是识别右下方有伴星的单位像素的坐标。网络图的节点应该是任何可散列的对象,所以我们可以使用元组来标记它们。这很容易实现,但效率不高,因为它需要将
np.array
的项转换为元组:

#CONSTRUCTION OF HORIZONTAL EDGES
hx, hy = np.where(image[1:] & image[:-1]) #horizontal edge start positions
h_units = np.array([hx, hy]).T
h_starts = [tuple(n) for n in h_units]
h_ends = [tuple(n) for n in h_units + (1, 0)] #end positions = start positions shifted by vector (1,0)
horizontal_edges = zip(h_starts, h_ends)

#CONSTRUCTION OF VERTICAL EDGES
vx, vy = np.where(image[:,1:] & image[:,:-1]) #vertical edge start positions
v_units = np.array([vx, vy]).T
v_starts = [tuple(n) for n in v_units]
v_ends = [tuple(n) for n in v_units + (0, 1)] #end positions = start positions shifted by vector (0,1)
vertical_edges = zip(v_starts, v_ends)
让我们看看它的样子:

G = nx.Graph()
G.add_edges_from(horizontal_edges)
G.add_edges_from(vertical_edges)
pos = dict(zip(G.nodes(), G.nodes())) # map node names to coordinates
nx.draw_networkx(G, pos, with_labels=False, node_size=0)
labels={node: f'({node[0]},{node[1]})' for node in G.nodes()}
nx.draw_networkx_labels(G, pos, labels, font_size=6, font_family='serif', font_weight='bold', bbox = dict(fc='lightblue', ec="black", boxstyle="round", lw=1))
plt.show()

IGRAPHE Networkx完全是用Python构建的,在处理大数据(如数百万像素的图像)时运行缓慢。另一方面,Igraph是用C语言构建的,但它的支持较少。文档不太详细,使用内部可视化工具代替matplotlib。所以基本上,
igraph
可能是一个复杂的选择,但如果你这么做,那将是性能上的巨大胜利。在执行算法之前,有一些必须知道的重要事实:

  • 节点的索引应该是从0开始的整数。这意味着,如果您在igraph.add_vertices()中处理其他内容,它将被重新索引为0、1、2。。。所有索引的旧名称都保存在
    igraph.vs['name']
  • 使用
    igraph.add_edges()
考虑到这些要求,降低图像的维数是一个很好的选择,即将像素重命名为整数0,1,2。。。现在我们开始:

def create_from_edges(edgearray):
    #This function immitates behaviour nx.add_edges_from for empty graph
    g = ig.Graph()
    u, inv = np.unique(edgearray, return_inverse=True)
    e = inv.reshape(edgearray.shape)
    g.add_vertices(u) #add vertices, in any order
    g.add_edges(e) #add edges, in reindexed order
    return g #old indices are kept in g.vs['name']

#Create array of edges with image pixels enumerated from 1 to N
image_idx = np.arange(image.size).reshape(*image.shape) #pixels of image indexed with numbers 1 to N
X, Y = (units.reshape(image.size) for units in np.indices(image.shape)) #X and Y coordinates of image_idx
idx = np.array([X, Y]).T #layout of nodes

hx, hy = np.where(image[1:] & image[:-1]) #horizontal edges as 2D indices
h_starts_idx = image_idx[hx, hy] #image_idx where horizontal edge starts
h_ends_idx = image_idx[hx+1, hy] #image_idx where horizontal edge starts

vx, vy = np.where(image[:, 1:] & image[:, :-1]) #vertical edges as 2D indices
v_starts_idx = image_idx[vx, vy] #image_idx where verical edge starts
v_ends_idx = image_idx[vx, vy+1] #image_idx where vertical edge starts

edgearray = np.vstack([np.array([h_starts_idx, h_ends_idx]).T, 
                      np.array([v_starts_idx, v_ends_idx]).T])
g = create_from_edges(edgearray)
这是我的草图,展示了顶点名称的新顺序:

ig.plot(g, bbox=(450, 450), 
        layout = ig.Layout(idx[g.vs['name']].tolist()), #only lists can be passed in to layout
        vertex_color = 'lightblue', vertex_label = g.vs['name'], vertex_size=14, vertex_label_size=8)
要求:
pythonigraph
pycairo
(用于绘图)。

我不明白您想做什么,但您可以使用
PIL
读取图像并转换为
numpy.array
。或者使用
cv2
将其直接读取为
numpy.array
(但需要将
BGR
转换为
RGB
)。然后,您可以访问图像中的每个像素,并在图形中使用其值。@kkopen使用
networkx
igraph
可以将节点的属性设置为值,并为其指定任何名称(“权重”、“颜色”、“密度”等)。似乎您不清楚构建算法的方法,因此很难猜测什么是适合您的好解决方案。@kkopen我想您需要将rgb值转换为包含1项而不是3项的单个黑白值。然后,您可以添加一些阈值来定义其中哪些节点值得解释为节点。虽然,这只是一个建议,但不确定它如何有利于进一步分析。只是出于好奇,我们为什么要将图像表示为图形?