识别对象和标签不起作用"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期待一些结果时,我得到了这个回报:

(<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()返回一个元组,实际的标签排在第一位,它们已经是一个 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 是最大值和最小值

相关内容

最新更新