scikit-image
67 строк · 2.0 Кб
1from ._pnpoly import _grid_points_in_poly, _points_in_poly
2
3
4def grid_points_in_poly(shape, verts, binarize=True):
5"""Test whether points on a specified grid are inside a polygon.
6
7For each ``(r, c)`` coordinate on a grid, i.e. ``(0, 0)``, ``(0, 1)`` etc.,
8test whether that point lies inside a polygon.
9
10You can control the output type with the `binarize` flag. Please refer to its
11documentation for further details.
12
13Parameters
14----------
15shape : tuple (M, N)
16Shape of the grid.
17verts : (V, 2) array
18Specify the V vertices of the polygon, sorted either clockwise
19or anti-clockwise. The first point may (but does not need to be)
20duplicated.
21binarize: bool
22If `True`, the output of the function is a boolean mask.
23Otherwise, it is a labeled array. The labels are:
24O - outside, 1 - inside, 2 - vertex, 3 - edge.
25
26See Also
27--------
28points_in_poly
29
30Returns
31-------
32mask : (M, N) ndarray
33If `binarize` is True, the output is a boolean mask. True means the
34corresponding pixel falls inside the polygon.
35If `binarize` is False, the output is a labeled array, with pixels
36having a label between 0 and 3. The meaning of the values is:
37O - outside, 1 - inside, 2 - vertex, 3 - edge.
38
39"""
40output = _grid_points_in_poly(shape, verts)
41if binarize:
42output = output.astype(bool)
43return output
44
45
46def points_in_poly(points, verts):
47"""Test whether points lie inside a polygon.
48
49Parameters
50----------
51points : (K, 2) array
52Input points, ``(x, y)``.
53verts : (L, 2) array
54Vertices of the polygon, sorted either clockwise or anti-clockwise.
55The first point may (but does not need to be) duplicated.
56
57See Also
58--------
59grid_points_in_poly
60
61Returns
62-------
63mask : (K,) array of bool
64True if corresponding point is inside the polygon.
65
66"""
67return _points_in_poly(points, verts)
68