Skip to content

Instantly share code, notes, and snippets.

@krosaen
Created December 22, 2017 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krosaen/f8e81a7dd0d932eec6ef57a12cdfb43e to your computer and use it in GitHub Desktop.
Save krosaen/f8e81a7dd0d932eec6ef57a12cdfb43e to your computer and use it in GitHub Desktop.
how to count number of points within a polygon
def count_kps_within_poly(polygon, keypoints):
from matplotlib.collections import PolyCollection
path = PolyCollection([polygon]).get_paths()[0]
return np.sum(path.contains_points(keypoints))
assert 2 == count_kps_within_poly(
[(0, 0), (100, 0), (200, 100), (100, 100), (0, 100)],
[(50, 50), (200, 0), (1, 1)]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment