识别对象和标签不起作用 "built-in method all of ..."

identifying objects and labelling not working "built-in method all of ..."

基本上,我有一个 numpy 数组,我想在其中标记不同的对象,并为每个对象找到最大分配(原始)值和质心。

我已经成功地标记了我的数组,并且我尝试使用极值来找到我的最大值,但是我 运行 遇到了一个我不明白的错误。

import numpy as np
from scipy.ndimage import generate_binary_structure, label, find_objects, extrema

s = generate_binary_structure(2,2)
ft_object = np.asarray(label(ft2, structure = s)) #ft2 is originally a tuple which is why I perform np.asarray
print ft_object

由于这给了我一个元组,我再次转换为 numpy.ndarray 并执行极值函数以获得最大值。

ft_extrema = extrema(ft_object)

这会给出错误消息

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). 

所以我把它改成

ft_extrema = extrema(ft_object.all)

这行得通,但是当我 print ft_extrema 期待一些结果时,我在 return:

中得到了这个
(<built-in method all of numpy.ndarray object at 0x2b90d09ed0d0>, built-in method all of numpy.ndarray object at 0x2b90d09ed0d0>, [()], [()])

不完全确定如何解决这个问题或为什么要这样做,所以希望得到任何帮助或建议。

我也想使用 find_objects 分别拼接我的对象以分别为每个对象找到 center_of_mass 但我无法做到这一点。

请注意,根据文档 label() returns 一个元组和实际标签排在第一位,它们已经是一个 nparray。 我没有你的 ft2 对象,但让它成为例如

ft2 = np.array([[1,1,0,0,0], [0,0,0,0,0], [0,0,0,0,1]])

然后我将按以下方式编辑您的代码:

import numpy as np #same
from scipy.ndimage import generate_binary_structure, label, find_objects, extrema #same

s = generate_binary_structure(2,2)   #same
ft_object = label(ft2, structure = s)[0]   #here take only the first object, these are the labels
ft_extrema = extrema(ft2, ft_object)   #here you should give as an argument the input and the labels
print ft_extrema

输出为:

(1,1,(0,0),(0,0))

符合预期,因为 1 是最大值,最小值