gpt4 book ai didi

python - Python遍历灰度图像中的连接组件

转载 作者:行者123 更新时间:2023-12-04 13:25:36 25 4
gpt4 key购买 nike

我有一个灰度图像,其值介于0(黑色)和白色(255)之间。我有一个与灰度图像大小相同的target矩阵。我需要从灰度图像中的一个随机像素开始,然后一次遍历图像一个像素(以深度优先搜索方式),然后将其值复制到target矩阵中的相应位置。我显然只需要对非白色像素执行此操作。我怎样才能做到这一点?我以为我可以获取灰度图像的连接组件并逐个遍历每个像素,但是找不到任何合适的连接组件实现。有任何想法吗?

例如,如果我的灰度图像是:

[[255,255,255,255,255,255,255]
[255,255, 0 ,10 ,255,255, 1 ]
[255,30 ,255,255,50 ,255, 9 ]
[51 ,20 ,255,255, 9 ,255,240]
[255,255,80 ,50 ,170,255, 20]
[255,255,255,255,255,255, 0 ]
[255,255,255,255,255,255, 69]]

然后可能的遍历是 [0,10,50,9,170,50,80,20,51,30],然后是 [1,9,240,20,0,69]以给出 [0,10,50,9,170,50,80,20,51,30,1,9,240,20,0,69]。不同对象之间的顺序无关紧要。

其他可能的遍历是: [1,9,240,20,0,69,0,10,50,9,170,50,80,20,51,30][1,9,240,20,0,69,0,10,50,9,170,50,80,20,30,51][1,9,240,20,0,69,10,50,9,170,50,80,20,30,0,51]
ETC。

最佳答案

您可以使用networkx:

from itertools import product, repeat
import numpy as np
import networkx as nx

arr = np.array(
[[255,255,255,255,255,255,255],
[255,255, 0 ,10 ,255,255, 1 ],
[255,30 ,255,255,50 ,255, 9 ],
[51 ,20 ,255,255, 9 ,255,240],
[255,255,80 ,50 ,170,255, 20],
[255,255,255,255,255,255, 0 ],
[255,255,255,255,255,255, 69]])

# generate edges
shift = list(product(*repeat([-1, 0, 1], 2)))
x_max, y_max = arr.shape
edges = []

for x, y in np.ndindex(arr.shape):
for x_delta, y_delta in shift:
x_neighb = x + x_delta
y_neighb = y + y_delta
if (0 <= x_neighb < x_max) and (0 <= y_neighb < y_max):
edge = (x, y), (x_neighb, y_neighb)
edges.append(edge)

# build graph
G = nx.from_edgelist(edges)

# draw graph
pos = {(x, y): (y, x_max-x) for x, y in G.nodes()}
nx.draw(G, with_labels=True, pos=pos, node_color='coral', node_size=1000)

enter image description here
# draw graph with numbers
labels = dict(np.ndenumerate(arr))
node_color = ['coral' if labels[n] == 255 else 'lightgrey' for n in G.nodes()]
nx.draw(G, with_labels=True, pos=pos, labels=labels, node_color=node_color, node_size=1000)

enter image description here
# build subgraph
select = np.argwhere(arr < 255)
G1 = G.subgraph(map(tuple, select))

# draw subgraph
pos = {(x, y): (y, x_max-x) for x, y in G1.nodes()}
labels1 = {n:labels[n] for n in G1.nodes()}
nx.draw(G1, with_labels=True, pos=pos, labels=labels1, node_color='lightgrey', node_size=1000)

enter image description here
# find connected components and DFS trees
for i in nx.connected_components(G1):
source = next(iter(i))
idx = nx.dfs_tree(G1, source=source)
print(arr[tuple(np.array(idx).T)])

输出:
[  0  10  50   9  50  80  20  30  51 170]
[ 9 1 240 20 0 69]

关于python - Python遍历灰度图像中的连接组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59524874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com