我试着取最大的n个轮廓,去掉其他的。 但我在某些帧中得到了这个异常,而在其他帧中却没有! 尝试从列表中删除轮廓时发生异常
Traceback (most recent call last):
File "/Users/TheMaestro/Desktop/Max Planck/FishTracking/FishTracker/general_tests.py", line 93, in <module>
contours_chest = ImageProcessor.get_bigest_n_contours(contours_chest, 3)
File "/Users/TheMaestro/Desktop/Max Planck/Fish Tracking/FishTracker/Controllers/ImageProcessor.py", line 319, in get_bigest_n_contours
contours.remove(contours[i])
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
这是我的代码:
电话:
^{pr2}$功能:
def get_biggest_n_contours(contours, n):
contours = sorted(contours, key=get_area, reverse=True)
contours_count = len(contours)
if contours_count > n:
for i in range(n,contours_count):
contours.remove(contours[i])
i -= 1
return contours
我检查了前面的答案,但我不知道在哪里使用a.any或a.all,也不知道为什么要在我的案例中使用它们! 我删除了使用索引,所以我看不到一个导致歧义的比较!在
谢谢你
我不完全确定
contours
的形状,但我怀疑在调用sorted
之后,您创建了一个由numpy数组组成的python列表。在在这行
contours.remove(contours[i])
中,您尝试从numpy数组列表中删除一个元素。list.remove
方法对list
中的所有元素进行线性搜索,并将它们与要删除的元素进行比较。因此,您将在remove
方法中比较numpy数组和numpy数组,并在这个比较的布尔值上分支,这个值是不明确的。在不必使用
remove
(实际上只有在不知道要删除的元素的索引时才会使用它),您可以pop
该索引处的元素。在但在你的情况下似乎有更好的选择。如果我观察正确,您希望在
contours
中找到n
最大的条目。在python中,当数据被排序时,这是一个简单的任务(您已经做到了)。因此,可以在对数组进行排序后对其进行切片:切片会帮你做所有的工作 1{如果所有元素都小于^则返回它们 2如果确实存在
n
元素,则返回所有元素 三。如果有更多,只需返回第一个n
元素通常不应在迭代列表时将其从列表中移除,但这是不对的:
{{{cd2>中的下一个迭代{elmnt}将不具有实数效果。既然您想要第一个
^{pr2}$n
轮廓,只需执行以下操作:相关问题 更多 >
编程相关推荐