gpt4 book ai didi

python - 在 NumPy 中是否有一种矢量化的方法来查找标记区域内的最大值?

转载 作者:行者123 更新时间:2023-12-02 15:48:13 24 4
gpt4 key购买 nike

我有一个代表树高的二维数组,其中 0 是地面。我有另一个数组,它的大小始终相同,显示分段和标记的树,其中 0 标签表示地面,正整数值表示一棵独特的树。以下是部分数据:

heights = array([[37.5 , 41.82, 42.18, 42.18, 42.18, 39.23, 40.68, 40.71, 40.71,
40.19, 35.03, 41.41, 41.41, 41.41, 40.77, 32.23, 32.23, 32.23,
31.45, 25.6 , 25.63, 30.12, 30.78, 30.78, 30.92],
[37.5 , 37.5 , 41.82, 42.18, 41.78, 41.78, 40.68, 40.68, 40.68,
40.19, 41.04, 41.41, 41.41, 41.41, 41.03, 32.23, 32.23, 32.23,
31.25, 25.6 , 25.6 , 30.12, 30.12, 21.08, 30.88],
[37.5 , 37.5 , 34.61, 41.78, 41.78, 25.6 , 39.14, 40.68, 38.79,
38.79, 41.04, 41.04, 41.8 , 41.8 , 41.8 , 24.66, 24.66, 31.25,
25.63, 26.24, 26.2 , 25.2 , 24.93, 21.03, 21.03],
[34.53, 34.61, 34.61, 35.23, 35.23, 25.32, 25.32, 33.17, 33.17,
38.86, 39.4 , 40.31, 41.8 , 41.8 , 41.8 , 41.17, 25.37, 26.77,
27.32, 27.39, 27.39, 26.96, 25.2 , 28.68, 28.68],
[34.53, 34.52, 36.5 , 36.58, 36.67, 36.67, 25.15, 33.17, 38.65,
38.86, 39.4 , 39.53, 40.78, 41.17, 41.17, 0. , 26.77, 27.09,
27.39, 27.6 , 27.6 , 28. , 28.16, 28.68, 28.68],
[32.22, 36.45, 37.1 , 37.28, 37.28, 38.07, 30.98, 31.12, 38.65,
38.65, 39.12, 39.4 , 40.78, 40.78, 0. , 0. , 27.41, 27.72,
27.72, 28.49, 28.49, 28.16, 28.34, 28.87, 28.68],
[36.45, 37.1 , 37.1 , 37.28, 38.23, 38.23, 38.23, 33.61, 32.31,
38.65, 38.65, 38.62, 39.01, 33.75, 34.65, 34.65, 27.41, 27.72,
27.72, 28.49, 28.49, 28.49, 28.87, 30.31, 30.31],
[35.71, 36.45, 37.1 , 30.96, 38.23, 38.23, 38.23, 33.61, 33.28,
33.42, 33.5 , 33.5 , 33.51, 34.07, 34.65, 34.65, 27.36, 27.83,
27.83, 28.49, 28.49, 28.43, 28.87, 31.82, 31.68],
[14.44, 0. , 0. , 0. , 21.41, 32.98, 33.61, 33.61, 34.27,
34.8 , 34.8 , 33.5 , 33.4 , 34.07, 34.65, 34.65, 0. , 27.83,
27.83, 28.7 , 29.18, 29.18, 31.82, 31.82, 31.98],
[13.46, 0. , 0. , 21.41, 21.73, 31.36, 33.33, 33.33, 34.89,
34.99, 34.99, 32.72, 33.4 , 33.8 , 33.8 , 0. , 0. , 0. ,
28.7 , 28.7 , 29.64, 29.64, 31.82, 31.82, 35.82],
[13.46, 0. , 0. , 0. , 21.73, 31.36, 31.46, 35.81, 36.33,
36.33, 36.33, 32.72, 33.37, 33.71, 33.71, 0. , 0. , 0. ,
28.7 , 29.64, 29.64, 29.77, 29.77, 29.77, 35.95],
[ 0. , 0. , 0. , 0. , 0. , 24.07, 31.57, 35.9 , 36.33,
36.33, 36.33, 21.97, 32.72, 33.37, 33.37, 0. , 0. , 0. ,
28.36, 29.04, 29.64, 29.77, 29.77, 29.77, 35.95],
[ 0. , 0. , 0. , 0. , 22.09, 24.07, 23.92, 31.57, 35.9 ,
36.33, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
28.38, 29.53, 28.96, 28.96, 28.69, 29.19, 35.49],
[ 0. , 0. , 0. , 0. , 22.09, 22.09, 22.09, 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
29.53, 29.53, 29.82, 28.96, 28.73, 29.19, 29.19],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
29.53, 30.12, 30.12, 29.82, 28.73, 0. , 28.89],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 30.12, 30.12, 30.12, 28.94, 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 30.12, 30.12, 29.82, 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 28.65, 28.65, 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. , 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. ,
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. , 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. , 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. ]], dtype=float32)
labeled_trees = array([[33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 37, 37, 37, 37, 37, 37,
37, 37, 37, 37, 39, 39, 39, 39, 39],
[33, 33, 33, 33, 33, 33, 33, 33, 33, 37, 37, 37, 37, 37, 37, 37,
37, 37, 37, 37, 39, 39, 39, 39, 39],
[33, 33, 33, 33, 33, 33, 33, 33, 33, 37, 37, 37, 37, 37, 37, 37,
37, 37, 37, 39, 39, 39, 39, 39, 39],
[33, 33, 33, 33, 33, 33, 33, 33, 37, 37, 37, 37, 37, 37, 37, 37,
37, 37, 39, 39, 39, 39, 39, 39, 39],
[33, 33, 33, 33, 33, 33, 33, 37, 37, 37, 37, 37, 37, 37, 37, 0,
39, 39, 39, 39, 39, 39, 39, 39, 39],
[33, 33, 33, 33, 33, 33, 33, 37, 37, 37, 37, 37, 37, 37, 0, 0,
39, 39, 39, 39, 39, 39, 39, 39, 39],
[33, 33, 33, 33, 33, 33, 33, 33, 37, 37, 37, 37, 37, 37, 37, 37,
37, 39, 39, 39, 39, 39, 39, 39, 39],
[33, 33, 33, 33, 33, 33, 33, 33, 33, 37, 37, 37, 37, 37, 37, 37,
37, 39, 39, 39, 39, 39, 39, 39, 39],
[33, 0, 0, 0, 33, 33, 33, 33, 33, 33, 33, 33, 37, 37, 37, 37,
0, 39, 39, 39, 39, 39, 39, 39, 39],
[33, 0, 0, 33, 33, 33, 33, 33, 33, 33, 33, 33, 37, 37, 37, 0,
0, 0, 39, 39, 39, 39, 39, 39, 39],
[33, 0, 0, 0, 33, 33, 33, 33, 33, 33, 33, 33, 37, 37, 37, 0,
0, 0, 39, 39, 39, 39, 39, 39, 39],
[ 0, 0, 0, 0, 0, 33, 33, 33, 33, 33, 33, 33, 37, 37, 37, 0,
0, 0, 39, 39, 39, 39, 39, 39, 39],
[ 0, 0, 0, 0, 33, 33, 33, 33, 33, 33, 0, 0, 0, 0, 0, 0,
0, 0, 39, 39, 39, 39, 39, 39, 39],
[ 0, 0, 0, 0, 33, 33, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 39, 39, 39, 39, 39, 39, 39],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 39, 39, 39, 39, 39, 0, 39],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 39, 39, 39, 39, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 39, 39, 39, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 39, 39, 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, 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, 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, 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, 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]], dtype=int32)

我想找到每个标记区域内的最大高度。我已使用 for 循环成功完成此操作,但速度很慢。

max_heights = {}
for label in list(np.unique(labeled_trees))[1:]:
tree_height = np.amax(heights[labeled_trees == label])
max_heights[str(label)] = tree_height

# max_heights = {'33': 42.18, '37': 41.8, '39': 35.95}

是否有更快/矢量化/更有效的方法来查找 numpy 数组标记区域内的最大值?理想的输出是一个 bool 数组,其中每个最大值的位置都是 True

[编辑]

maximum_position scipy.ndimage 中的函数很有前途,但看起来它只返回像素等于局部最大值的第一个位置。我需要标记区域内的每个位置都等于其最大值。

最佳答案

下面是 np.maximum.reduceat 的一个更简单的用法:

idx = labeled_trees.argsort(None)
sorted_labeled_trees = labeled_trees.ravel()[idx]
sorted_heights = heights.ravel()[idx]
bins = np.flatnonzero(np.diff(sorted_labeled_trees) != 0) + 1
max_heights = np.maximum.reduceat(sorted_heights, bins)
max_trees = sorted_labeled_trees[bins]

如果你坚持要一本字典,你可以用 zip 制作一本:

result = dict(zip(max_trees, max_heights))

如果你想要一个最大值出现位置的掩码并且树的数量相对较少,你可以直接使用广播或多或少地计算掩码:

peak_mask = ((max_trees == labeled_trees[..., None]) & (max_height == heights[..., None])).any(-1)

如果树的数量不少,最好在标签上使用循环:

peak_mask = np.zeros(labeled_trees.shape, bool)
for t, h in zip(max_trees, max_height):
peak_mask |= (labeled_trees == t) & (heights == h)

关于python - 在 NumPy 中是否有一种矢量化的方法来查找标记区域内的最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73419437/

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