OpenCV MSER 从屏幕截图中检测区域 - Python

OpenCV MSER detect areas from a screenshot - Python

我看到了这个例子: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] 称为 'list comprehension'。您可以在许多地方阅读更多关于它们的信息。

在你提到的代码中regions是一些可迭代的,比如一个列表。这意味着当您编写 for p in regions 时,p 会假定 regions 中的每个值,一次一个。这就是 p 的来源。

由于 p 参与列表理解,因此可以在表达式中使用。在本例中,表达式为 cv2.convexHull(p.reshape(-1, 1, 2))。因此,整个构造的值是 cv2.convexHull(p.reshape(-1, 1, 2)) 中每个 p 的值 regions.