如何从图像生成图形,以像素作为节点和条件边
我有一个 0 ad 1 的二值灰度图像。我想创建一个图表,其中每个节点都是一个像素(值为 1),并在每个像素之间创建一条边缘,这些像素的隐含距离低于阈值。
基本上我使用的是networkx
库,但是我实现它的方式非常非常慢(边缘构建的部分)。有没有更快的代码实现(更低的复杂度)?
G = nx.Graph()
rows = img.shape[0]
columns = img.shape[1]
# Generatig Nodes
for x in range(0, rows):
for y in range(0, columns):
if img[x, y] == 1:
G.add_node(n, px=(x, y))
# Generating Edges based on the max distance allowed
max_dist = 10 # threshold
for x in G.nodes:
for y in G.nodes:
dist = distance.euclidean(G.nodes[x]['px'], G.nodes[y]['px'])
if dist <= max_dist:
G.add_edge(x, y, weight=dist)
请注意,我保存了像素标签,因为我需要将图形重新转换为图像
I have a binary gray image of 0 ad 1. I would like to create a graph where every node is a pixel (of value 1) and to create an edges between every pixels that have the ecluidean distance under a threshold.
Basically I was using the networkx
library, but the way I implemented it is very very slow (the part of edges building). There is a faster implementation of the code (lower complexity)?
G = nx.Graph()
rows = img.shape[0]
columns = img.shape[1]
# Generatig Nodes
for x in range(0, rows):
for y in range(0, columns):
if img[x, y] == 1:
G.add_node(n, px=(x, y))
# Generating Edges based on the max distance allowed
max_dist = 10 # threshold
for x in G.nodes:
for y in G.nodes:
dist = distance.euclidean(G.nodes[x]['px'], G.nodes[y]['px'])
if dist <= max_dist:
G.add_edge(x, y, weight=dist)
Note that I save the pixel labels because I need after to reconvert the graph into an image
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论