pythonopencvmser

OpenCV MSER detect areas from a screenshot - Python


I saw this example: OpenCV MSER detect text areas - Python

and I tried to use that code but it's not working. The error is:

hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] AttributeError: 'list' object has no attribute 'reshape'

Where does the variable p come from?


Solution

  • That whole construct [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions] is called a 'list comprehension'. You can read much more about them in numerous places.

    In the code you referred to regions is some iterrable, such as a list. This means that when you write for p in regions p assumes each of the values in regions, one at a time. So that's where p comes from.

    Since p is taking part in a list comprehension it can be used in an expression. In this case, the expression is cv2.convexHull(p.reshape(-1, 1, 2)). Thus the value of the entire construct is the value of cv2.convexHull(p.reshape(-1, 1, 2)) for each p in regions.