【发布时间】:2019-04-04 23:12:17
【问题描述】:
首先,如果这实际上是重复的,很抱歉 - 我在过去三个小时里一直在尝试解决这个问题,但未能找到任何解决方案。
问题
我使用列表将坐标表示为[x, y]。我想知道坐标列表是否不包含指定的坐标。例如,如果我有坐标列表[[3.3, 4.4], [5.5, 6.6]] 和坐标[1.1, 2.2],我想要返回True,因为坐标不在坐标列表中。
可能值得注意的是,坐标列表是使用 OpenCV 函数cv2.findContours()、cv2.minAreaRect() 和最后cv2.boxPoints() 生成的,这会生成列表列表。这些坐标存储在字典中并从那里访问;调用坐标的print() 可以得到[array([3.3, 4.4], dtype=float32), array([5.5, 6.6], dtype=float32)] 格式的坐标,而不是[[3.3, 4.4], [5.5, 6.6]] 格式,后者是在我用print() 找到坐标后直接给出的cv2.boxPoints()。
我尝试过的
我尝试使用this question 的答案,但收到错误ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()。
该尝试的代码如下所示:
for coordinate in box:
if coordinate not in specialCoordinates:
# operations go here
然后我尝试使用this question about a.all() 的答案,但我得到了同样的错误。
该尝试的代码如下所示:
for coordinate in box:
if not all(coordinate == special for special in specialCoordinates):
# operations go here
我也试过这个:
for coordinate in box:
if all(coordinate != special for special in specialCoordinates):
# operations go here
其他信息
当我在 Python 2.7 解释器中尝试以下操作时,上面提到的格式 if coordinate not in specialCoordinates 有效:
Win32 上的 Python 2.7.15(v2.7.15:ca079a3ea3,2018 年 4 月 30 日,16:30:26)[MSC v.1500 64 位 (AMD64)]
输入“帮助”、“版权”、“信用”或“许可”以了解更多信息。
>>> a = [[3.3, 4.4], [5.5, 6.6]]
>>> b = [1.1, 2.2]
>>> b 不在 a 中
是的
【问题讨论】:
-
欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您描述的问题。
-
您已经接近了,但我们需要您将其缩短为一个有代表性的示例,而不是一屏显示历史。
OpenCV不是问题,但numpy是。举一个简单的例子,其中包含程序内结构的常量,以及然后您希望我们修复的for coordinate in box循环。 -
如果您有一个数组列表,您可以使用
map(list, specialCoordinates)将其转换为列表列表 -
你说 python 2.7 可以工作,那么什么不工作?
标签: python python-2.7 opencv