【发布时间】:2018-06-17 22:34:19
【问题描述】:
我看到了这个例子:OpenCV MSER detect text areas - Python
我尝试使用该代码,但它不起作用。 错误是:
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
AttributeError: 'list' object has no attribute 'reshape'
变量p 来自哪里?
【问题讨论】:
我看到了这个例子:OpenCV MSER detect text areas - Python
我尝试使用该代码,但它不起作用。 错误是:
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
AttributeError: 'list' object has no attribute 'reshape'
变量p 来自哪里?
【问题讨论】:
整个构造 [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] 被称为“列表理解”。您可以在许多地方阅读更多关于它们的信息。
在您提到的代码中regions 是一些可迭代的,例如列表。这意味着当您编写for p in regions 时,p 假定regions 中的每个值,一次一个。这就是p 的来源。
由于p 参与列表推导,它可以在表达式中使用。在这种情况下,表达式为cv2.convexHull(p.reshape(-1, 1, 2))。因此,整个构造的值就是regions 中每个p 的cv2.convexHull(p.reshape(-1, 1, 2)) 的值。
【讨论】: