如何从图像生成图形,以像素作为节点和条件边

发布于 2025-01-09 10:55:57 字数 692 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文